cthreads.pp 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099
  1. {
  2. This file is part of the Free Pascal run time library.
  3. Copyright (c) 2002 by Peter Vreman,
  4. member of the Free Pascal development team.
  5. pthreads threading support implementation
  6. See the file COPYING.FPC, included in this distribution,
  7. for details about the copyright.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  11. **********************************************************************}
  12. {$mode objfpc}
  13. {$ifdef linux}
  14. {$define dynpthreads} // Useless on BSD, since they are in libc
  15. {$endif}
  16. { sem_init is best, since it does not consume any file descriptors. }
  17. { sem_open is second best, since it consumes only one file descriptor }
  18. { per semaphore. }
  19. { If neither is available, pipe is used as fallback, which consumes 2 }
  20. { file descriptors per semaphore. }
  21. { Darwin doesn't support nameless semaphores in at least }
  22. { Mac OS X 10.4.8/Darwin 8.8 }
  23. {$ifndef darwin}
  24. {$define has_sem_init}
  25. {$define has_sem_getvalue}
  26. {$else }
  27. {$ifdef darwin}
  28. {$define has_sem_open}
  29. {$endif}
  30. {$endif}
  31. {$ifdef linux}
  32. {$define has_sem_timedwait}
  33. {$endif}
  34. unit cthreads;
  35. interface
  36. {$S-}
  37. {$ifndef dynpthreads} // If you have problems compiling this on FreeBSD 5.x
  38. {$linklib c} // try adding -Xf
  39. {$ifndef Darwin}
  40. {$ifndef haiku}
  41. {$linklib pthread}
  42. {$endif haiku}
  43. {$endif darwin}
  44. {$endif}
  45. Procedure SetCThreadManager;
  46. implementation
  47. Uses
  48. BaseUnix,
  49. unix,
  50. unixtype,
  51. initc
  52. {$ifdef dynpthreads}
  53. ,dl
  54. {$endif}
  55. ;
  56. {*****************************************************************************
  57. System unit import
  58. *****************************************************************************}
  59. procedure fpc_threaderror; [external name 'FPC_THREADERROR'];
  60. {*****************************************************************************
  61. Generic overloaded
  62. *****************************************************************************}
  63. { Include OS specific parts. }
  64. {$i pthread.inc}
  65. Type PINTRTLEvent = ^TINTRTLEvent;
  66. TINTRTLEvent = record
  67. condvar: pthread_cond_t;
  68. mutex: pthread_mutex_t;
  69. isset: boolean;
  70. end;
  71. TTryWaitResult = (tw_error, tw_semwasunlocked, tw_semwaslocked);
  72. {*****************************************************************************
  73. Threadvar support
  74. *****************************************************************************}
  75. const
  76. threadvarblocksize : dword = 0;
  77. var
  78. TLSKey : pthread_key_t;
  79. procedure CInitThreadvar(var offset : dword;size : dword);
  80. begin
  81. {$ifdef cpusparc}
  82. threadvarblocksize:=align(threadvarblocksize,16);
  83. {$endif cpusparc}
  84. {$ifdef cpupowerpc}
  85. threadvarblocksize:=align(threadvarblocksize,8);
  86. {$endif cpupowerc}
  87. {$ifdef cpui386}
  88. threadvarblocksize:=align(threadvarblocksize,8);
  89. {$endif cpui386}
  90. {$ifdef cpuarm}
  91. threadvarblocksize:=align(threadvarblocksize,4);
  92. {$endif cpuarm}
  93. {$ifdef cpum68k}
  94. threadvarblocksize:=align(threadvarblocksize,2);
  95. {$endif cpum68k}
  96. {$ifdef cpux86_64}
  97. threadvarblocksize:=align(threadvarblocksize,16);
  98. {$endif cpux86_64}
  99. {$ifdef cpupowerpc64}
  100. threadvarblocksize:=align(threadvarblocksize,16);
  101. {$endif cpupowerpc64}
  102. offset:=threadvarblocksize;
  103. inc(threadvarblocksize,size);
  104. end;
  105. procedure CAllocateThreadVars;
  106. var
  107. dataindex : pointer;
  108. begin
  109. { we've to allocate the memory from system }
  110. { because the FPC heap management uses }
  111. { exceptions which use threadvars but }
  112. { these aren't allocated yet ... }
  113. { allocate room on the heap for the thread vars }
  114. DataIndex:=Pointer(Fpmmap(nil,threadvarblocksize,3,MAP_PRIVATE+MAP_ANONYMOUS,-1,0));
  115. FillChar(DataIndex^,threadvarblocksize,0);
  116. pthread_setspecific(tlskey,dataindex);
  117. end;
  118. function CRelocateThreadvar(offset : dword) : pointer;
  119. var
  120. P : Pointer;
  121. begin
  122. P:=pthread_getspecific(tlskey);
  123. if (P=Nil) then
  124. begin
  125. CAllocateThreadvars;
  126. // If this also goes wrong: bye bye threadvars...
  127. P:=pthread_getspecific(tlskey);
  128. end;
  129. CRelocateThreadvar:=P+Offset;
  130. end;
  131. procedure CReleaseThreadVars;
  132. begin
  133. Fpmunmap(pointer(pthread_getspecific(tlskey)),threadvarblocksize);
  134. end;
  135. { Include OS independent Threadvar initialization }
  136. {*****************************************************************************
  137. Thread starting
  138. *****************************************************************************}
  139. type
  140. pthreadinfo = ^tthreadinfo;
  141. tthreadinfo = record
  142. f : tthreadfunc;
  143. p : pointer;
  144. stklen : cardinal;
  145. end;
  146. function ThreadMain(param : pointer) : pointer;cdecl;
  147. var
  148. ti : tthreadinfo;
  149. nset: tsigset;
  150. {$if defined(linux) and not defined(FPC_USE_LIBC)}
  151. nlibcset: tlibc_sigset;
  152. {$endif linux/no FPC_USE_LIBC}
  153. {$ifdef DEBUG_MT}
  154. // in here, don't use write/writeln before having called
  155. // InitThread! I wonder if anyone ever debugged these routines,
  156. // because they will have crashed if DEBUG_MT was enabled!
  157. // this took me the good part of an hour to figure out
  158. // why it was crashing all the time!
  159. // this is kind of a workaround, we simply write(2) to fd 0
  160. s: string[100]; // not an ansistring
  161. {$endif DEBUG_MT}
  162. begin
  163. {$ifdef DEBUG_MT}
  164. s := 'New thread started, initing threadvars'#10;
  165. fpwrite(0,s[1],length(s));
  166. {$endif DEBUG_MT}
  167. { unblock all signals we are interested in (may be blocked by }
  168. { default in new threads on some OSes, see #9073) }
  169. fpsigemptyset(nset);
  170. fpsigaddset(nset,SIGSEGV);
  171. fpsigaddset(nset,SIGBUS);
  172. fpsigaddset(nset,SIGFPE);
  173. fpsigaddset(nset,SIGILL);
  174. {$if defined(linux) and not defined(FPC_USE_LIBC)}
  175. { sigset_t has a different size for linux/kernel and linux/libc }
  176. fillchar(nlibcset,sizeof(nlibcset),0);
  177. if (sizeof(nlibcset)>sizeof(nset)) then
  178. move(nset,nlibcset,sizeof(nset))
  179. else
  180. move(nset,nlibcset,sizeof(nlibcset));
  181. pthread_sigmask(SIG_UNBLOCK,@nlibcset,nil);
  182. {$else linux}
  183. pthread_sigmask(SIG_UNBLOCK,@nset,nil);
  184. {$endif linux}
  185. { Allocate local thread vars, this must be the first thing,
  186. because the exception management and io depends on threadvars }
  187. CAllocateThreadVars;
  188. { Copy parameter to local data }
  189. {$ifdef DEBUG_MT}
  190. s := 'New thread started, initialising ...'#10;
  191. fpwrite(0,s[1],length(s));
  192. {$endif DEBUG_MT}
  193. ti:=pthreadinfo(param)^;
  194. dispose(pthreadinfo(param));
  195. { Initialize thread }
  196. InitThread(ti.stklen);
  197. { Start thread function }
  198. {$ifdef DEBUG_MT}
  199. writeln('Jumping to thread function');
  200. {$endif DEBUG_MT}
  201. ThreadMain:=pointer(ti.f(ti.p));
  202. DoneThread;
  203. pthread_exit(ThreadMain);
  204. end;
  205. function CBeginThread(sa : Pointer;stacksize : PtrUInt;
  206. ThreadFunction : tthreadfunc;p : pointer;
  207. creationFlags : dword; var ThreadId : TThreadId) : TThreadID;
  208. var
  209. ti : pthreadinfo;
  210. thread_attr : pthread_attr_t;
  211. begin
  212. {$ifdef DEBUG_MT}
  213. writeln('Creating new thread');
  214. {$endif DEBUG_MT}
  215. { Initialize multithreading if not done }
  216. if not IsMultiThread then
  217. begin
  218. if (InterLockedExchange(longint(IsMultiThread),ord(true)) = 0) then
  219. begin
  220. { We're still running in single thread mode, setup the TLS }
  221. pthread_key_create(@TLSKey,nil);
  222. InitThreadVars(@CRelocateThreadvar);
  223. end
  224. end;
  225. { the only way to pass data to the newly created thread
  226. in a MT safe way, is to use the heap }
  227. new(ti);
  228. ti^.f:=ThreadFunction;
  229. ti^.p:=p;
  230. ti^.stklen:=stacksize;
  231. { call pthread_create }
  232. {$ifdef DEBUG_MT}
  233. writeln('Starting new thread');
  234. {$endif DEBUG_MT}
  235. pthread_attr_init(@thread_attr);
  236. {$ifndef HAIKU}
  237. pthread_attr_setinheritsched(@thread_attr, PTHREAD_EXPLICIT_SCHED);
  238. {$endif}
  239. // will fail under linux -- apparently unimplemented
  240. pthread_attr_setscope(@thread_attr, PTHREAD_SCOPE_PROCESS);
  241. // don't create detached, we need to be able to join (waitfor) on
  242. // the newly created thread!
  243. //pthread_attr_setdetachstate(@thread_attr, PTHREAD_CREATE_DETACHED);
  244. // set the stack size
  245. if (pthread_attr_setstacksize(@thread_attr, stacksize)<>0) or
  246. // and create the thread
  247. (pthread_create(ppthread_t(@threadid), @thread_attr, @ThreadMain,ti) <> 0) then
  248. begin
  249. dispose(ti);
  250. threadid := TThreadID(0);
  251. end;
  252. CBeginThread:=threadid;
  253. {$ifdef DEBUG_MT}
  254. writeln('BeginThread returning ',ptrint(CBeginThread));
  255. {$endif DEBUG_MT}
  256. end;
  257. procedure CEndThread(ExitCode : DWord);
  258. begin
  259. DoneThread;
  260. pthread_detach(pthread_t(pthread_self()));
  261. pthread_exit(pointer(ptrint(ExitCode)));
  262. end;
  263. function CSuspendThread (threadHandle : TThreadID) : dword;
  264. begin
  265. { pthread_kill(SIGSTOP) cannot be used, because posix-compliant
  266. implementations then freeze the entire process instead of only
  267. the target thread. Suspending a particular thread is not
  268. supported by posix nor by most *nix implementations, presumably
  269. because of concerns mentioned in E.4 at
  270. http://pauillac.inria.fr/~xleroy/linuxthreads/faq.html#E and in
  271. http://java.sun.com/j2se/1.4.2/docs/guide/misc/threadPrimitiveDeprecation.html
  272. }
  273. // result := pthread_kill(threadHandle,SIGSTOP);
  274. end;
  275. function CResumeThread (threadHandle : TThreadID) : dword;
  276. begin
  277. // result := pthread_kill(threadHandle,SIGCONT);
  278. end;
  279. procedure sched_yield; cdecl; external 'c' name 'sched_yield';
  280. procedure CThreadSwitch; {give time to other threads}
  281. begin
  282. { At least on Mac OS X, the pthread_yield_np calls through to this. }
  283. { Further, sched_yield is in POSIX and supported on FreeBSD 4+, }
  284. { Linux, Mac OS X and Solaris, while the thread-specific yield }
  285. { routines are called differently everywhere and non-standard. }
  286. sched_yield;
  287. end;
  288. function CKillThread (threadHandle : TThreadID) : dword;
  289. begin
  290. pthread_detach(pthread_t(threadHandle));
  291. CKillThread := pthread_cancel(pthread_t(threadHandle));
  292. end;
  293. function CWaitForThreadTerminate (threadHandle : TThreadID; TimeoutMs : longint) : dword; {0=no timeout}
  294. var
  295. LResultP: Pointer;
  296. begin
  297. pthread_join(pthread_t(threadHandle), @LResultP);
  298. CWaitForThreadTerminate := dword(LResultP);
  299. end;
  300. function CThreadSetPriority (threadHandle : TThreadID; Prio: longint): boolean; {-15..+15, 0=normal}
  301. begin
  302. {$Warning ThreadSetPriority needs to be implemented}
  303. end;
  304. function CThreadGetPriority (threadHandle : TThreadID): Integer;
  305. begin
  306. {$Warning ThreadGetPriority needs to be implemented}
  307. end;
  308. function CGetCurrentThreadId : TThreadID;
  309. begin
  310. CGetCurrentThreadId := TThreadID (pthread_self());
  311. end;
  312. {*****************************************************************************
  313. Delphi/Win32 compatibility
  314. *****************************************************************************}
  315. procedure CInitCriticalSection(var CS);
  316. var
  317. MAttr : pthread_mutexattr_t;
  318. res: longint;
  319. begin
  320. res:=pthread_mutexattr_init(@MAttr);
  321. if res=0 then
  322. begin
  323. res:=pthread_mutexattr_settype(@MAttr,longint(_PTHREAD_MUTEX_RECURSIVE));
  324. if res=0 then
  325. res := pthread_mutex_init(@CS,@MAttr)
  326. else
  327. { No recursive mutex support :/ }
  328. res := pthread_mutex_init(@CS,NIL);
  329. end
  330. else
  331. res:= pthread_mutex_init(@CS,NIL);
  332. pthread_mutexattr_destroy(@MAttr);
  333. if res <> 0 then
  334. fpc_threaderror;
  335. end;
  336. procedure CEnterCriticalSection(var CS);
  337. begin
  338. if pthread_mutex_lock(@CS) <> 0 then
  339. fpc_threaderror
  340. end;
  341. procedure CLeaveCriticalSection(var CS);
  342. begin
  343. if pthread_mutex_unlock(@CS) <> 0 then
  344. fpc_threaderror
  345. end;
  346. procedure CDoneCriticalSection(var CS);
  347. begin
  348. { unlock as long as unlocking works to unlock it if it is recursive
  349. some Delphi code might call this function with a locked mutex }
  350. while pthread_mutex_unlock(@CS)=0 do
  351. ;
  352. if pthread_mutex_destroy(@CS) <> 0 then
  353. fpc_threaderror;
  354. end;
  355. {*****************************************************************************
  356. Semaphore routines
  357. *****************************************************************************}
  358. procedure cSemaphoreWait(const FSem: Pointer);
  359. var
  360. res: cint;
  361. err: cint;
  362. {$if not defined(has_sem_init) and not defined(has_sem_open)}
  363. b: byte;
  364. {$endif}
  365. begin
  366. {$if defined(has_sem_init) or defined(has_sem_open)}
  367. repeat
  368. res:=sem_wait(PSemaphore(FSem));
  369. err:=fpgetCerrno;
  370. until (res<>-1) or (err<>ESysEINTR);
  371. {$else}
  372. repeat
  373. res:=fpread(PFilDes(FSem)^[0], b, 1);
  374. err:=fpgeterrno;
  375. until (res<>-1) or ((err<>ESysEINTR) and (err<>ESysEAgain));
  376. {$endif}
  377. end;
  378. {$if defined(has_sem_timedwait)}
  379. function cSemaphoreTimedWait(const FSem: Pointer; const Timeout: ttimespec): cint;
  380. var
  381. res: cint;
  382. err: cint;
  383. begin
  384. repeat
  385. res:=sem_timedwait(PSemaphore(FSem), @Timeout);
  386. if res=0 then exit(0);
  387. err:=fpgetCerrno;
  388. until err<>ESysEINTR;
  389. result:=err;
  390. end;
  391. {$endif}
  392. procedure cSemaphorePost(const FSem: Pointer);
  393. {$if defined(has_sem_init) or defined(has_sem_open)}
  394. begin
  395. sem_post(PSemaphore(FSem));
  396. end;
  397. {$else}
  398. var
  399. writeres: cint;
  400. err: cint;
  401. b : byte;
  402. begin
  403. b:=0;
  404. repeat
  405. writeres:=fpwrite(PFilDes(FSem)^[1], b, 1);
  406. err:=fpgeterrno;
  407. until (writeres<>-1) or ((err<>ESysEINTR) and (err<>ESysEAgain));
  408. end;
  409. {$endif}
  410. function cSemaphoreTryWait(const FSem: pointer): TTryWaitResult;
  411. var
  412. res: cint;
  413. err: cint;
  414. {$if defined(has_sem_init) or defined(has_sem_open)}
  415. begin
  416. repeat
  417. res:=sem_trywait(FSem);
  418. err:=fpgetCerrno;
  419. until (res<>-1) or (err<>ESysEINTR);
  420. if (res=0) then
  421. result:=tw_semwasunlocked
  422. else if (err=ESysEAgain) then
  423. result:=tw_semwaslocked
  424. else
  425. result:=tw_error;
  426. {$else has_sem_init or has_sem_open}
  427. var
  428. fds: TFDSet;
  429. tv : timeval;
  430. begin
  431. tv.tv_sec:=0;
  432. tv.tv_usec:=0;
  433. fpFD_ZERO(fds);
  434. fpFD_SET(PFilDes(FSem)^[0],fds);
  435. repeat
  436. res:=fpselect(PFilDes(FSem)^[0]+1,@fds,nil,nil,@tv);
  437. err:=fpgeterrno;
  438. until (res>=0) or ((res=-1) and (err<>ESysEIntr));
  439. if (res>0) then
  440. begin
  441. cSemaphoreWait(FSem);
  442. result:=tw_semwasunlocked
  443. end
  444. else if (res=0) then
  445. result:=tw_semwaslocked
  446. else
  447. result:=tw_error;
  448. {$endif has_sem_init or has_sem_open}
  449. end;
  450. {$if defined(has_sem_open) and not defined(has_sem_init)}
  451. function cIntSemaphoreOpen(const name: pchar; initvalue: boolean): Pointer;
  452. var
  453. err: cint;
  454. begin
  455. repeat
  456. cIntSemaphoreOpen := sem_open(name,O_CREAT,0,ord(initvalue));
  457. err:=fpgetCerrno;
  458. until (ptrint(cIntSemaphoreOpen) <> SEM_FAILED) or (err <> ESysEINTR);
  459. if (ptrint(cIntSemaphoreOpen) <> SEM_FAILED) then
  460. { immediately unlink so the semaphore will be destroyed when the }
  461. { the process exits }
  462. sem_unlink(name)
  463. else
  464. cIntSemaphoreOpen:=NIL;
  465. end;
  466. {$endif}
  467. function cIntSemaphoreInit(initvalue: boolean): Pointer;
  468. {$if defined(has_sem_open) and not defined(has_sem_init)}
  469. var
  470. tid: string[31];
  471. semname: string[63];
  472. err: cint;
  473. {$endif}
  474. begin
  475. {$ifdef has_sem_init}
  476. cIntSemaphoreInit := GetMem(SizeOf(TSemaphore));
  477. if sem_init(PSemaphore(cIntSemaphoreInit), 0, ord(initvalue)) <> 0 then
  478. begin
  479. FreeMem(cIntSemaphoreInit);
  480. cIntSemaphoreInit:=NIL;
  481. end;
  482. {$else}
  483. {$ifdef has_sem_open}
  484. { avoid a potential temporary nameclash with another process/thread }
  485. str(fpGetPid,semname);
  486. str(ptruint(pthread_self),tid);
  487. semname:='/FPC'+semname+'T'+tid+#0;
  488. cIntSemaphoreInit:=cIntSemaphoreOpen(@semname[1],initvalue);
  489. {$else}
  490. cIntSemaphoreInit := GetMem(SizeOf(TFilDes));
  491. if (fppipe(PFilDes(cIntSemaphoreInit)^) <> 0) then
  492. begin
  493. FreeMem(cIntSemaphoreInit);
  494. cIntSemaphoreInit:=nil;
  495. end
  496. else if initvalue then
  497. cSemaphorePost(cIntSemaphoreInit);
  498. {$endif}
  499. {$endif}
  500. end;
  501. function cSemaphoreInit: Pointer;
  502. begin
  503. cSemaphoreInit:=cIntSemaphoreInit(false);
  504. end;
  505. procedure cSemaphoreDestroy(const FSem: Pointer);
  506. begin
  507. {$ifdef has_sem_init}
  508. sem_destroy(PSemaphore(FSem));
  509. FreeMem(FSem);
  510. {$else}
  511. {$ifdef has_sem_open}
  512. sem_close(PSemaphore(FSem));
  513. {$else has_sem_init}
  514. fpclose(PFilDes(FSem)^[0]);
  515. fpclose(PFilDes(FSem)^[1]);
  516. FreeMem(FSem);
  517. {$endif}
  518. {$endif}
  519. end;
  520. type
  521. TPthreadMutex = pthread_mutex_t;
  522. Tbasiceventstate=record
  523. FSem: Pointer;
  524. FEventSection: TPthreadMutex;
  525. FWaiters: longint;
  526. FManualReset,
  527. FDestroying: Boolean;
  528. end;
  529. plocaleventstate = ^tbasiceventstate;
  530. // peventstate=pointer;
  531. Const
  532. wrSignaled = 0;
  533. wrTimeout = 1;
  534. wrAbandoned= 2;
  535. wrError = 3;
  536. function IntBasicEventCreate(EventAttributes : Pointer; AManualReset,InitialState : Boolean;const Name : ansistring):pEventState;
  537. var
  538. MAttr : pthread_mutexattr_t;
  539. res : cint;
  540. begin
  541. new(plocaleventstate(result));
  542. plocaleventstate(result)^.FManualReset:=AManualReset;
  543. plocaleventstate(result)^.FWaiters:=0;
  544. plocaleventstate(result)^.FDestroying:=False;
  545. {$ifdef has_sem_init}
  546. plocaleventstate(result)^.FSem:=cIntSemaphoreInit(initialstate);
  547. if plocaleventstate(result)^.FSem=nil then
  548. begin
  549. FreeMem(result);
  550. fpc_threaderror;
  551. end;
  552. {$else}
  553. {$ifdef has_sem_open}
  554. plocaleventstate(result)^.FSem:=cIntSemaphoreOpen(PChar(Name),InitialState);
  555. if (plocaleventstate(result)^.FSem = NIL) then
  556. begin
  557. FreeMem(result);
  558. fpc_threaderror;
  559. end;
  560. {$else}
  561. plocaleventstate(result)^.FSem:=cSemaphoreInit;
  562. if (plocaleventstate(result)^.FSem = NIL) then
  563. begin
  564. FreeMem(result);
  565. fpc_threaderror;
  566. end;
  567. if InitialState then
  568. cSemaphorePost(plocaleventstate(result)^.FSem);
  569. {$endif}
  570. {$endif}
  571. // plocaleventstate(result)^.feventsection:=nil;
  572. res:=pthread_mutexattr_init(@MAttr);
  573. if res=0 then
  574. begin
  575. res:=pthread_mutexattr_settype(@MAttr,longint(_PTHREAD_MUTEX_RECURSIVE));
  576. if Res=0 then
  577. Res:=pthread_mutex_init(@plocaleventstate(result)^.feventsection,@MAttr)
  578. else
  579. res:=pthread_mutex_init(@plocaleventstate(result)^.feventsection,nil);
  580. end
  581. else
  582. res:=pthread_mutex_init(@plocaleventstate(result)^.feventsection,nil);
  583. pthread_mutexattr_destroy(@MAttr);
  584. if res <> 0 then
  585. begin
  586. cSemaphoreDestroy(plocaleventstate(result)^.FSem);
  587. FreeMem(result);
  588. fpc_threaderror;
  589. end;
  590. end;
  591. procedure Intbasiceventdestroy(state:peventstate);
  592. var
  593. i: longint;
  594. begin
  595. { safely mark that we are destroying this event }
  596. pthread_mutex_lock(@plocaleventstate(state)^.feventsection);
  597. plocaleventstate(state)^.FDestroying:=true;
  598. { wake up everyone who is waiting }
  599. for i := 1 to plocaleventstate(state)^.FWaiters do
  600. cSemaphorePost(plocaleventstate(state)^.FSem);
  601. pthread_mutex_unlock(@plocaleventstate(state)^.feventsection);
  602. { now wait until they've finished their business }
  603. while (plocaleventstate(state)^.FWaiters <> 0) do
  604. cThreadSwitch;
  605. { and clean up }
  606. cSemaphoreDestroy(plocaleventstate(state)^.FSem);
  607. dispose(plocaleventstate(state));
  608. end;
  609. procedure IntbasiceventResetEvent(state:peventstate);
  610. begin
  611. {$if not defined(has_sem_init) and not defined(has_sem_open)}
  612. pthread_mutex_lock(@plocaleventstate(state)^.feventsection);
  613. try
  614. {$endif}
  615. while (cSemaphoreTryWait(plocaleventstate(state)^.FSem) = tw_semwasunlocked) do
  616. ;
  617. {$if not defined(has_sem_init) and not defined(has_sem_open)}
  618. finally
  619. pthread_mutex_unlock(@plocaleventstate(state)^.feventsection);
  620. end;
  621. {$endif}
  622. end;
  623. procedure IntbasiceventSetEvent(state:peventstate);
  624. Var
  625. res : cint;
  626. err : cint;
  627. {$if defined(has_sem_init) or defined(has_sem_open)}
  628. Value : Longint;
  629. {$else}
  630. fds: TFDSet;
  631. tv : timeval;
  632. {$endif}
  633. begin
  634. pthread_mutex_lock(@plocaleventstate(state)^.feventsection);
  635. Try
  636. {$if defined(has_sem_init) or defined(has_sem_open)}
  637. if (sem_getvalue(plocaleventstate(state)^.FSem,@value) <> -1) then
  638. begin
  639. if Value=0 then
  640. cSemaphorePost(plocaleventstate(state)^.FSem);
  641. end
  642. else if (fpgetCerrno = ESysENOSYS) then
  643. { not yet implemented on Mac OS X 10.4.8 }
  644. begin
  645. repeat
  646. res:=sem_trywait(psem_t(plocaleventstate(state)^.FSem));
  647. err:=fpgetCerrno;
  648. until ((res<>-1) or (err<>ESysEINTR));
  649. { now we've either decreased the semaphore by 1 (if it was }
  650. { not zero), or we've done nothing (if it was already zero) }
  651. { -> increase by 1 and we have the same result as }
  652. { increasing by 1 only if it was 0 }
  653. cSemaphorePost(plocaleventstate(state)^.FSem);
  654. end
  655. else
  656. fpc_threaderror;
  657. {$else has_sem_init or has_sem_open}
  658. tv.tv_sec:=0;
  659. tv.tv_usec:=0;
  660. fpFD_ZERO(fds);
  661. fpFD_SET(PFilDes(plocaleventstate(state)^.FSem)^[0],fds);
  662. repeat
  663. res:=fpselect(PFilDes(plocaleventstate(state)^.FSem)^[0]+1,@fds,nil,nil,@tv);
  664. err:=fpgeterrno;
  665. until (res>=0) or ((res=-1) and (err<>ESysEIntr));
  666. if (res=0) then
  667. cSemaphorePost(plocaleventstate(state)^.FSem);
  668. {$endif has_sem_init or has_sem_open}
  669. finally
  670. pthread_mutex_unlock(@plocaleventstate(state)^.feventsection);
  671. end;
  672. end;
  673. function IntbasiceventWaitFor(Timeout : Cardinal;state:peventstate) : longint;
  674. var
  675. i, loopcnt: cardinal;
  676. timespec, timetemp, timeleft: ttimespec;
  677. nanores, nanoerr: cint;
  678. twres: TTryWaitResult;
  679. lastloop: boolean;
  680. begin
  681. { safely check whether we are being destroyed, if so immediately return. }
  682. { otherwise (under the same mutex) increase the number of waiters }
  683. pthread_mutex_lock(@plocaleventstate(state)^.feventsection);
  684. if (plocaleventstate(state)^.FDestroying) then
  685. begin
  686. pthread_mutex_unlock(@plocaleventstate(state)^.feventsection);
  687. result := wrAbandoned;
  688. exit;
  689. end;
  690. inc(plocaleventstate(state)^.FWaiters);
  691. pthread_mutex_unlock(@plocaleventstate(state)^.feventsection);
  692. if TimeOut=Cardinal($FFFFFFFF) then
  693. begin
  694. { if no timeout, just wait until we are woken up }
  695. cSemaphoreWait(plocaleventstate(state)^.FSem);
  696. if not(plocaleventstate(state)^.FDestroying) then
  697. result:=wrSignaled
  698. else
  699. result:=wrAbandoned;
  700. end
  701. else
  702. begin
  703. {$ifdef has_sem_timedwait}
  704. fpgettimeofday(@timespec,nil);
  705. inc(timespec.tv_nsec, (timeout mod 1000) * 1000000);
  706. inc(timespec.tv_sec, timeout div 1000);
  707. if timespec.tv_nsec > 1000000000 then
  708. begin
  709. dec(timespec.tv_nsec, 1000000000);
  710. inc(timespec.tv_sec);
  711. end;
  712. nanores := cSemaphoreTimedWait(plocaleventstate(state)^.FSem, timespec);
  713. if nanores = 0 then
  714. result := wrSignaled
  715. else if nanores = ESysETIMEDOUT then
  716. result := wrTimeout
  717. else
  718. result := wrError;
  719. {$else}
  720. timespec.tv_sec:=0;
  721. { 50 miliseconds or less -> wait once for this duration }
  722. if (timeout <= 50) then
  723. loopcnt:=1
  724. { otherwise wake up every 50 msecs to check }
  725. { (we'll wait a little longer in total because }
  726. { we don't take into account the overhead) }
  727. else
  728. begin
  729. loopcnt := timeout div 50;
  730. timespec.tv_nsec:=50*1000000;
  731. end;
  732. result := wrTimeOut;
  733. nanores := 0;
  734. for i := 1 to loopcnt do
  735. begin
  736. { in the last iteration, wait for the amount of time left }
  737. if (i = loopcnt) then
  738. timespec.tv_nsec:=(timeout mod 50) * 1000000;
  739. timetemp:=timespec;
  740. lastloop:=false;
  741. { every time our sleep is interrupted for whatever reason, }
  742. { also check whether the semaphore has been posted in the }
  743. { mean time }
  744. repeat
  745. {$if not defined(has_sem_init) and not defined(has_sem_open)}
  746. pthread_mutex_lock(@plocaleventstate(state)^.feventsection);
  747. try
  748. {$endif}
  749. twres := cSemaphoreTryWait(plocaleventstate(state)^.FSem);
  750. {$if not defined(has_sem_init) and not defined(has_sem_open)}
  751. finally
  752. pthread_mutex_unlock(@plocaleventstate(state)^.feventsection);
  753. end;
  754. {$endif}
  755. case twres of
  756. tw_error:
  757. begin
  758. result := wrError;
  759. break;
  760. end;
  761. tw_semwasunlocked:
  762. begin
  763. result := wrSignaled;
  764. break;
  765. end;
  766. end;
  767. if (lastloop) then
  768. break;
  769. nanores:=fpnanosleep(@timetemp,@timeleft);
  770. nanoerr:=fpgeterrno;
  771. timetemp:=timeleft;
  772. lastloop:=(i=loopcnt);
  773. { loop until 1) we slept complete interval (except if last for-loop }
  774. { in which case we try to lock once more); 2) an error occurred; }
  775. { 3) we're being destroyed }
  776. until ((nanores=0) and not lastloop) or ((nanores<>0) and (nanoerr<>ESysEINTR)) or plocaleventstate(state)^.FDestroying;
  777. { adjust result being destroyed or error (in this order, since }
  778. { if we're being destroyed the "error" could be ESysEINTR, which }
  779. { is not a real error }
  780. if plocaleventstate(state)^.FDestroying then
  781. result := wrAbandoned
  782. else if (nanores <> 0) then
  783. result := wrError;
  784. { break out of greater loop when we got the lock, when an error }
  785. { occurred, or when we are being destroyed }
  786. if (result<>wrTimeOut) then
  787. break;
  788. end;
  789. {$endif}
  790. end;
  791. if (result=wrSignaled) then
  792. begin
  793. if plocaleventstate(state)^.FManualReset then
  794. begin
  795. pthread_mutex_lock(@plocaleventstate(state)^.feventsection);
  796. Try
  797. intbasiceventresetevent(State);
  798. cSemaphorePost(plocaleventstate(state)^.FSem);
  799. Finally
  800. pthread_mutex_unlock(@plocaleventstate(state)^.feventsection);
  801. end;
  802. end;
  803. end;
  804. { don't put this above the previous if-block, because otherwise }
  805. { we can get errors in case an object is destroyed between the }
  806. { end of the wait/sleep loop and the signalling above. }
  807. { The pthread_mutex_unlock above takes care of the memory barrier }
  808. interlockeddecrement(plocaleventstate(state)^.FWaiters);
  809. end;
  810. function intRTLEventCreate: PRTLEvent;
  811. var p:pintrtlevent;
  812. begin
  813. new(p);
  814. pthread_cond_init(@p^.condvar, nil);
  815. pthread_mutex_init(@p^.mutex, nil);
  816. p^.isset:=false;
  817. result:=PRTLEVENT(p);
  818. end;
  819. procedure intRTLEventDestroy(AEvent: PRTLEvent);
  820. var p:pintrtlevent;
  821. begin
  822. p:=pintrtlevent(aevent);
  823. pthread_cond_destroy(@p^.condvar);
  824. pthread_mutex_destroy(@p^.mutex);
  825. dispose(p);
  826. end;
  827. procedure intRTLEventSetEvent(AEvent: PRTLEvent);
  828. var p:pintrtlevent;
  829. begin
  830. p:=pintrtlevent(aevent);
  831. pthread_mutex_lock(@p^.mutex);
  832. p^.isset:=true;
  833. pthread_cond_signal(@p^.condvar);
  834. pthread_mutex_unlock(@p^.mutex);
  835. end;
  836. procedure intRTLEventResetEvent(AEvent: PRTLEvent);
  837. var p:pintrtlevent;
  838. begin
  839. p:=pintrtlevent(aevent);
  840. pthread_mutex_lock(@p^.mutex);
  841. p^.isset:=false;
  842. pthread_mutex_unlock(@p^.mutex);
  843. end;
  844. procedure intRTLEventWaitFor(AEvent: PRTLEvent);
  845. var p:pintrtlevent;
  846. begin
  847. p:=pintrtlevent(aevent);
  848. pthread_mutex_lock(@p^.mutex);
  849. while not p^.isset do pthread_cond_wait(@p^.condvar, @p^.mutex);
  850. p^.isset:=false;
  851. pthread_mutex_unlock(@p^.mutex);
  852. end;
  853. procedure intRTLEventWaitForTimeout(AEvent: PRTLEvent;timeout : longint);
  854. var
  855. p : pintrtlevent;
  856. errres : cint;
  857. timespec : ttimespec;
  858. tnow : timeval;
  859. begin
  860. p:=pintrtlevent(aevent);
  861. fpgettimeofday(@tnow,nil);
  862. timespec.tv_sec:=tnow.tv_sec+(timeout div 1000);
  863. timespec.tv_nsec:=(timeout mod 1000)*1000000 + tnow.tv_usec*1000;
  864. if timespec.tv_nsec >= 1000000000 then
  865. begin
  866. inc(timespec.tv_sec);
  867. dec(timespec.tv_nsec, 1000000000);
  868. end;
  869. errres:=0;
  870. pthread_mutex_lock(@p^.mutex);
  871. while (not p^.isset) and
  872. (errres <> ESysETIMEDOUT) do
  873. begin
  874. errres:=pthread_cond_timedwait(@p^.condvar, @p^.mutex, @timespec);
  875. end;
  876. p^.isset:=false;
  877. pthread_mutex_unlock(@p^.mutex);
  878. end;
  879. type
  880. threadmethod = procedure of object;
  881. Function CInitThreads : Boolean;
  882. begin
  883. {$ifdef DEBUG_MT}
  884. Writeln('Entering InitThreads.');
  885. {$endif}
  886. {$ifndef dynpthreads}
  887. Result:=True;
  888. {$else}
  889. Result:=LoadPthreads;
  890. {$endif}
  891. ThreadID := TThreadID (pthread_self);
  892. {$ifdef DEBUG_MT}
  893. Writeln('InitThreads : ',Result);
  894. {$endif DEBUG_MT}
  895. end;
  896. Function CDoneThreads : Boolean;
  897. begin
  898. {$ifndef dynpthreads}
  899. Result:=True;
  900. {$else}
  901. Result:=UnloadPthreads;
  902. {$endif}
  903. end;
  904. Var
  905. CThreadManager : TThreadManager;
  906. Procedure SetCThreadManager;
  907. begin
  908. With CThreadManager do begin
  909. InitManager :=@CInitThreads;
  910. DoneManager :=@CDoneThreads;
  911. BeginThread :=@CBeginThread;
  912. EndThread :=@CEndThread;
  913. SuspendThread :=@CSuspendThread;
  914. ResumeThread :=@CResumeThread;
  915. KillThread :=@CKillThread;
  916. ThreadSwitch :=@CThreadSwitch;
  917. WaitForThreadTerminate :=@CWaitForThreadTerminate;
  918. ThreadSetPriority :=@CThreadSetPriority;
  919. ThreadGetPriority :=@CThreadGetPriority;
  920. GetCurrentThreadId :=@CGetCurrentThreadId;
  921. InitCriticalSection :=@CInitCriticalSection;
  922. DoneCriticalSection :=@CDoneCriticalSection;
  923. EnterCriticalSection :=@CEnterCriticalSection;
  924. LeaveCriticalSection :=@CLeaveCriticalSection;
  925. InitThreadVar :=@CInitThreadVar;
  926. RelocateThreadVar :=@CRelocateThreadVar;
  927. AllocateThreadVars :=@CAllocateThreadVars;
  928. ReleaseThreadVars :=@CReleaseThreadVars;
  929. BasicEventCreate :=@intBasicEventCreate;
  930. BasicEventDestroy :=@intBasicEventDestroy;
  931. BasicEventResetEvent :=@intBasicEventResetEvent;
  932. BasicEventSetEvent :=@intBasicEventSetEvent;
  933. BasiceventWaitFor :=@intBasiceventWaitFor;
  934. rtlEventCreate :=@intrtlEventCreate;
  935. rtlEventDestroy :=@intrtlEventDestroy;
  936. rtlEventSetEvent :=@intrtlEventSetEvent;
  937. rtlEventResetEvent :=@intrtlEventResetEvent;
  938. rtleventWaitForTimeout :=@intrtleventWaitForTimeout;
  939. rtleventWaitFor :=@intrtleventWaitFor;
  940. // semaphores
  941. SemaphoreInit :=@cSemaphoreInit;
  942. SemaphoreDestroy :=@cSemaphoreDestroy;
  943. SemaphoreWait :=@cSemaphoreWait;
  944. SemaphorePost :=@cSemaphorePost;
  945. end;
  946. SetThreadManager(CThreadManager);
  947. end;
  948. initialization
  949. if ThreadingAlreadyUsed then
  950. begin
  951. writeln('Threading has been used before cthreads was initialized.');
  952. writeln('Make cthreads one of the first units in your uses clause.');
  953. runerror(211);
  954. end;
  955. SetCThreadManager;
  956. finalization
  957. end.