cthreads.pp 32 KB

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