cthreads.pp 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870
  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. Linux (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. {$ifdef dynpthreads}
  47. ,dl
  48. {$endif}
  49. ;
  50. {*****************************************************************************
  51. Generic overloaded
  52. *****************************************************************************}
  53. { Include OS specific parts. }
  54. {$i pthread.inc}
  55. Type PINTRTLEvent = ^TINTRTLEvent;
  56. TINTRTLEvent = record
  57. condvar: pthread_cond_t;
  58. mutex: pthread_mutex_t;
  59. isset: boolean;
  60. end;
  61. {*****************************************************************************
  62. Threadvar support
  63. *****************************************************************************}
  64. const
  65. threadvarblocksize : dword = 0;
  66. var
  67. TLSKey : pthread_key_t;
  68. procedure CInitThreadvar(var offset : dword;size : dword);
  69. begin
  70. {$ifdef cpusparc}
  71. threadvarblocksize:=align(threadvarblocksize,16);
  72. {$endif cpusparc}
  73. {$ifdef cpupowerpc}
  74. threadvarblocksize:=align(threadvarblocksize,8);
  75. {$endif cpupowerc}
  76. {$ifdef cpui386}
  77. threadvarblocksize:=align(threadvarblocksize,8);
  78. {$endif cpui386}
  79. {$ifdef cpuarm}
  80. threadvarblocksize:=align(threadvarblocksize,4);
  81. {$endif cpuarm}
  82. {$ifdef cpum68k}
  83. threadvarblocksize:=align(threadvarblocksize,2);
  84. {$endif cpum68k}
  85. {$ifdef cpux86_64}
  86. threadvarblocksize:=align(threadvarblocksize,16);
  87. {$endif cpux86_64}
  88. {$ifdef cpupowerpc64}
  89. threadvarblocksize:=align(threadvarblocksize,16);
  90. {$endif cpupowerpc64}
  91. offset:=threadvarblocksize;
  92. inc(threadvarblocksize,size);
  93. end;
  94. procedure CAllocateThreadVars;
  95. var
  96. dataindex : pointer;
  97. begin
  98. { we've to allocate the memory from system }
  99. { because the FPC heap management uses }
  100. { exceptions which use threadvars but }
  101. { these aren't allocated yet ... }
  102. { allocate room on the heap for the thread vars }
  103. DataIndex:=Pointer(Fpmmap(nil,threadvarblocksize,3,MAP_PRIVATE+MAP_ANONYMOUS,-1,0));
  104. FillChar(DataIndex^,threadvarblocksize,0);
  105. pthread_setspecific(tlskey,dataindex);
  106. end;
  107. function CRelocateThreadvar(offset : dword) : pointer;
  108. var
  109. P : Pointer;
  110. begin
  111. P:=pthread_getspecific(tlskey);
  112. if (P=Nil) then
  113. begin
  114. CAllocateThreadvars;
  115. // If this also goes wrong: bye bye threadvars...
  116. P:=pthread_getspecific(tlskey);
  117. end;
  118. CRelocateThreadvar:=P+Offset;
  119. end;
  120. procedure CReleaseThreadVars;
  121. begin
  122. Fpmunmap(pointer(pthread_getspecific(tlskey)),threadvarblocksize);
  123. end;
  124. { Include OS independent Threadvar initialization }
  125. {*****************************************************************************
  126. Thread starting
  127. *****************************************************************************}
  128. type
  129. pthreadinfo = ^tthreadinfo;
  130. tthreadinfo = record
  131. f : tthreadfunc;
  132. p : pointer;
  133. stklen : cardinal;
  134. end;
  135. function ThreadMain(param : pointer) : pointer;cdecl;
  136. var
  137. ti : tthreadinfo;
  138. {$ifdef DEBUG_MT}
  139. // in here, don't use write/writeln before having called
  140. // InitThread! I wonder if anyone ever debugged these routines,
  141. // because they will have crashed if DEBUG_MT was enabled!
  142. // this took me the good part of an hour to figure out
  143. // why it was crashing all the time!
  144. // this is kind of a workaround, we simply write(2) to fd 0
  145. s: string[100]; // not an ansistring
  146. {$endif DEBUG_MT}
  147. begin
  148. {$ifdef DEBUG_MT}
  149. s := 'New thread started, initing threadvars'#10;
  150. fpwrite(0,s[1],length(s));
  151. {$endif DEBUG_MT}
  152. { Allocate local thread vars, this must be the first thing,
  153. because the exception management and io depends on threadvars }
  154. CAllocateThreadVars;
  155. { Copy parameter to local data }
  156. {$ifdef DEBUG_MT}
  157. s := 'New thread started, initialising ...'#10;
  158. fpwrite(0,s[1],length(s));
  159. {$endif DEBUG_MT}
  160. ti:=pthreadinfo(param)^;
  161. dispose(pthreadinfo(param));
  162. { Initialize thread }
  163. InitThread(ti.stklen);
  164. { Start thread function }
  165. {$ifdef DEBUG_MT}
  166. writeln('Jumping to thread function');
  167. {$endif DEBUG_MT}
  168. ThreadMain:=pointer(ti.f(ti.p));
  169. DoneThread;
  170. pthread_exit(ThreadMain);
  171. end;
  172. function CBeginThread(sa : Pointer;stacksize : PtrUInt;
  173. ThreadFunction : tthreadfunc;p : pointer;
  174. creationFlags : dword; var ThreadId : TThreadId) : TThreadID;
  175. var
  176. ti : pthreadinfo;
  177. thread_attr : pthread_attr_t;
  178. begin
  179. {$ifdef DEBUG_MT}
  180. writeln('Creating new thread');
  181. {$endif DEBUG_MT}
  182. { Initialize multithreading if not done }
  183. if not IsMultiThread then
  184. begin
  185. if (InterLockedExchange(longint(IsMultiThread),ord(true)) = 0) then
  186. begin
  187. { We're still running in single thread mode, setup the TLS }
  188. pthread_key_create(@TLSKey,nil);
  189. InitThreadVars(@CRelocateThreadvar);
  190. end
  191. end;
  192. { the only way to pass data to the newly created thread
  193. in a MT safe way, is to use the heap }
  194. new(ti);
  195. ti^.f:=ThreadFunction;
  196. ti^.p:=p;
  197. ti^.stklen:=stacksize;
  198. { call pthread_create }
  199. {$ifdef DEBUG_MT}
  200. writeln('Starting new thread');
  201. {$endif DEBUG_MT}
  202. pthread_attr_init(@thread_attr);
  203. pthread_attr_setinheritsched(@thread_attr, PTHREAD_EXPLICIT_SCHED);
  204. // will fail under linux -- apparently unimplemented
  205. pthread_attr_setscope(@thread_attr, PTHREAD_SCOPE_PROCESS);
  206. // don't create detached, we need to be able to join (waitfor) on
  207. // the newly created thread!
  208. //pthread_attr_setdetachstate(@thread_attr, PTHREAD_CREATE_DETACHED);
  209. if pthread_create(ppthread_t(@threadid), @thread_attr, @ThreadMain,ti) <> 0 then
  210. begin
  211. dispose(ti);
  212. threadid := TThreadID(0);
  213. end;
  214. CBeginThread:=threadid;
  215. {$ifdef DEBUG_MT}
  216. writeln('BeginThread returning ',ptrint(CBeginThread));
  217. {$endif DEBUG_MT}
  218. end;
  219. procedure CEndThread(ExitCode : DWord);
  220. begin
  221. DoneThread;
  222. pthread_detach(pthread_t(pthread_self()));
  223. pthread_exit(pointer(ptrint(ExitCode)));
  224. end;
  225. function CSuspendThread (threadHandle : TThreadID) : dword;
  226. begin
  227. { pthread_kill(SIGSTOP) cannot be used, because posix-compliant
  228. implementations then freeze the entire process instead of only
  229. the target thread. Suspending a particular thread is not
  230. supported by posix nor by most *nix implementations, presumably
  231. because of concerns mentioned in E.4 at
  232. http://pauillac.inria.fr/~xleroy/linuxthreads/faq.html#E and in
  233. http://java.sun.com/j2se/1.4.2/docs/guide/misc/threadPrimitiveDeprecation.html
  234. }
  235. // result := pthread_kill(threadHandle,SIGSTOP);
  236. end;
  237. function CResumeThread (threadHandle : TThreadID) : dword;
  238. begin
  239. // result := pthread_kill(threadHandle,SIGCONT);
  240. end;
  241. procedure sched_yield; cdecl; external 'c' name 'sched_yield';
  242. procedure CThreadSwitch; {give time to other threads}
  243. begin
  244. { At least on Mac OS X, the pthread_yield_np calls through to this. }
  245. { Further, sched_yield is in POSIX and supported on FreeBSD 4+, }
  246. { Linux, Mac OS X and Solaris, while the thread-specific yield }
  247. { routines are called differently everywhere and non-standard. }
  248. sched_yield;
  249. end;
  250. function CKillThread (threadHandle : TThreadID) : dword;
  251. begin
  252. pthread_detach(pthread_t(threadHandle));
  253. CKillThread := pthread_cancel(pthread_t(threadHandle));
  254. end;
  255. function CWaitForThreadTerminate (threadHandle : TThreadID; TimeoutMs : longint) : dword; {0=no timeout}
  256. var
  257. LResultP: Pointer;
  258. begin
  259. pthread_join(pthread_t(threadHandle), @LResultP);
  260. CWaitForThreadTerminate := dword(LResultP);
  261. end;
  262. {$warning threadhandle can be larger than a dword}
  263. function CThreadSetPriority (threadHandle : TThreadID; Prio: longint): boolean; {-15..+15, 0=normal}
  264. begin
  265. {$Warning ThreadSetPriority needs to be implemented}
  266. end;
  267. {$warning threadhandle can be larger than a dword}
  268. function CThreadGetPriority (threadHandle : TThreadID): Integer;
  269. begin
  270. {$Warning ThreadGetPriority needs to be implemented}
  271. end;
  272. function CGetCurrentThreadId : TThreadID;
  273. begin
  274. CGetCurrentThreadId := TThreadID (pthread_self());
  275. end;
  276. {*****************************************************************************
  277. Delphi/Win32 compatibility
  278. *****************************************************************************}
  279. procedure CInitCriticalSection(var CS);
  280. var
  281. MAttr : pthread_mutexattr_t;
  282. res: longint;
  283. begin
  284. res:=pthread_mutexattr_init(@MAttr);
  285. if res=0 then
  286. begin
  287. res:=pthread_mutexattr_settype(@MAttr,longint(_PTHREAD_MUTEX_RECURSIVE));
  288. if res=0 then
  289. res := pthread_mutex_init(@CS,@MAttr)
  290. else
  291. { No recursive mutex support :/ }
  292. res := pthread_mutex_init(@CS,NIL);
  293. end
  294. else
  295. res:= pthread_mutex_init(@CS,NIL);
  296. pthread_mutexattr_destroy(@MAttr);
  297. if res <> 0 then
  298. runerror(6);
  299. end;
  300. procedure CEnterCriticalSection(var CS);
  301. begin
  302. if pthread_mutex_lock(@CS) <> 0 then
  303. runerror(6);
  304. end;
  305. procedure CLeaveCriticalSection(var CS);
  306. begin
  307. if pthread_mutex_unlock(@CS) <> 0 then
  308. runerror(6)
  309. end;
  310. procedure CDoneCriticalSection(var CS);
  311. begin
  312. { unlock as long as unlocking works to unlock it if it is recursive
  313. some Delphi code might call this function with a locked mutex }
  314. while pthread_mutex_unlock(@CS)=0 do
  315. ;
  316. if pthread_mutex_destroy(@CS) <> 0 then
  317. runerror(6);
  318. end;
  319. {*****************************************************************************
  320. Semaphore routines
  321. *****************************************************************************}
  322. procedure cSemaphoreWait(const FSem: Pointer);
  323. var
  324. res: cint;
  325. err: cint;
  326. {$if not defined(has_sem_init) and not defined(has_sem_open)}
  327. b: byte;
  328. {$endif}
  329. begin
  330. {$if defined(has_sem_init) or defined(has_sem_open)}
  331. repeat
  332. res:=sem_wait(PSemaphore(FSem));
  333. err:=fpgeterrno;
  334. until (res<>-1) or (err<>ESysEINTR);
  335. {$else}
  336. repeat
  337. res:=fpread(PFilDes(FSem)^[0], b, 1);
  338. err:=fpgeterrno;
  339. until (res<>-1) or ((err<>ESysEINTR) and (err<>ESysEAgain));
  340. {$endif}
  341. end;
  342. procedure cSemaphorePost(const FSem: Pointer);
  343. {$if defined(has_sem_init) or defined(has_sem_open)}
  344. begin
  345. sem_post(PSemaphore(FSem));
  346. end;
  347. {$else}
  348. var
  349. writeres: cint;
  350. err: cint;
  351. b : byte;
  352. begin
  353. b:=0;
  354. repeat
  355. writeres:=fpwrite(PFilDes(FSem)^[1], b, 1);
  356. err:=fpgeterrno;
  357. until (writeres<>-1) or ((err<>ESysEINTR) and (err<>ESysEAgain));
  358. end;
  359. {$endif}
  360. {$if defined(has_sem_open) and not defined(has_sem_init)}
  361. function cIntSemaphoreOpen(const name: pchar; initvalue: boolean): Pointer;
  362. var
  363. err: cint;
  364. begin
  365. repeat
  366. cIntSemaphoreOpen := sem_open(name,O_CREAT,0,ord(initvalue));
  367. err:=fpgeterrno;
  368. until (ptrint(cIntSemaphoreOpen) <> SEM_FAILED) or (err <> ESysEINTR);
  369. if (ptrint(cIntSemaphoreOpen) <> SEM_FAILED) then
  370. { immediately unlink so the semaphore will be destroyed when the }
  371. { the process exits }
  372. sem_unlink(name)
  373. else
  374. cIntSemaphoreOpen:=NIL;
  375. end;
  376. {$endif}
  377. function cIntSemaphoreInit(initvalue: boolean): Pointer;
  378. {$if defined(has_sem_open) and not defined(has_sem_init)}
  379. var
  380. tid: string[31];
  381. semname: string[63];
  382. err: cint;
  383. {$endif}
  384. begin
  385. {$ifdef has_sem_init}
  386. cIntSemaphoreInit := GetMem(SizeOf(TSemaphore));
  387. if sem_init(PSemaphore(cIntSemaphoreInit), 0, ord(initvalue)) <> 0 then
  388. begin
  389. FreeMem(cIntSemaphoreInit);
  390. cIntSemaphoreInit:=NIL;
  391. end;
  392. {$else}
  393. {$ifdef has_sem_open}
  394. { avoid a potential temporary nameclash with another process/thread }
  395. str(fpGetPid,semname);
  396. str(ptruint(pthread_self),tid);
  397. semname:='/FPC'+semname+'T'+tid+#0;
  398. cIntSemaphoreInit:=cIntSemaphoreOpen(@semname[1],initvalue);
  399. {$else}
  400. cIntSemaphoreInit := GetMem(SizeOf(TFilDes));
  401. if (fppipe(PFilDes(cIntSemaphoreInit)^) <> 0) then
  402. begin
  403. FreeMem(cIntSemaphoreInit);
  404. cIntSemaphoreInit:=nil;
  405. end
  406. else if initvalue then
  407. cSemaphorePost(cIntSemaphoreInit);
  408. {$endif}
  409. {$endif}
  410. end;
  411. function cSemaphoreInit: Pointer;
  412. begin
  413. cSemaphoreInit:=cIntSemaphoreInit(false);
  414. end;
  415. procedure cSemaphoreDestroy(const FSem: Pointer);
  416. begin
  417. {$ifdef has_sem_init}
  418. sem_destroy(PSemaphore(FSem));
  419. FreeMem(FSem);
  420. {$else}
  421. {$ifdef has_sem_open}
  422. sem_close(PSemaphore(FSem));
  423. {$else has_sem_init}
  424. fpclose(PFilDes(FSem)^[0]);
  425. fpclose(PFilDes(FSem)^[1]);
  426. FreeMem(FSem);
  427. {$endif}
  428. {$endif}
  429. end;
  430. type
  431. TPthreadMutex = pthread_mutex_t;
  432. Tbasiceventstate=record
  433. FSem: Pointer;
  434. FEventSection: TPthreadMutex;
  435. FManualReset: Boolean;
  436. end;
  437. plocaleventstate = ^tbasiceventstate;
  438. // peventstate=pointer;
  439. Const
  440. wrSignaled = 0;
  441. wrTimeout = 1;
  442. wrAbandoned= 2;
  443. wrError = 3;
  444. function IntBasicEventCreate(EventAttributes : Pointer; AManualReset,InitialState : Boolean;const Name : ansistring):pEventState;
  445. var
  446. MAttr : pthread_mutexattr_t;
  447. res : cint;
  448. begin
  449. new(plocaleventstate(result));
  450. plocaleventstate(result)^.FManualReset:=AManualReset;
  451. {$ifdef has_sem_init}
  452. plocaleventstate(result)^.FSem:=cIntSemaphoreInit(true);
  453. if plocaleventstate(result)^.FSem=nil then
  454. begin
  455. FreeMem(result);
  456. runerror(6);
  457. end;
  458. {$else}
  459. {$ifdef has_sem_open}
  460. plocaleventstate(result)^.FSem:=cIntSemaphoreOpen(PChar(Name),InitialState);
  461. if (plocaleventstate(result)^.FSem = NIL) then
  462. begin
  463. FreeMem(result);
  464. runerror(6);
  465. end;
  466. {$else}
  467. plocaleventstate(result)^.FSem:=cSemaphoreInit;
  468. if (plocaleventstate(result)^.FSem = NIL) then
  469. begin
  470. FreeMem(result);
  471. runerror(6);
  472. end;
  473. if InitialState then
  474. cSemaphorePost(plocaleventstate(result)^.FSem);
  475. {$endif}
  476. {$endif}
  477. // plocaleventstate(result)^.feventsection:=nil;
  478. res:=pthread_mutexattr_init(@MAttr);
  479. if res=0 then
  480. begin
  481. res:=pthread_mutexattr_settype(@MAttr,longint(_PTHREAD_MUTEX_RECURSIVE));
  482. if Res=0 then
  483. Res:=pthread_mutex_init(@plocaleventstate(result)^.feventsection,@MAttr)
  484. else
  485. res:=pthread_mutex_init(@plocaleventstate(result)^.feventsection,nil);
  486. end
  487. else
  488. res:=pthread_mutex_init(@plocaleventstate(result)^.feventsection,nil);
  489. pthread_mutexattr_destroy(@MAttr);
  490. if res <> 0 then
  491. begin
  492. cSemaphoreDestroy(plocaleventstate(result)^.FSem);
  493. FreeMem(result);
  494. runerror(6);
  495. end;
  496. end;
  497. procedure Intbasiceventdestroy(state:peventstate);
  498. begin
  499. cSemaphoreDestroy(plocaleventstate(state)^.FSem);
  500. FreeMem(state);
  501. end;
  502. procedure IntbasiceventResetEvent(state:peventstate);
  503. {$if defined(has_sem_init) or defined(has_sem_open)}
  504. var
  505. res: cint;
  506. err: cint;
  507. begin
  508. repeat
  509. res:=sem_trywait(psem_t(plocaleventstate(state)^.FSem));
  510. err:=fpgeterrno;
  511. until (res<>0) and ((res<>-1) or (err<>ESysEINTR));
  512. {$else has_sem_init or has_sem_open}
  513. var
  514. fds: TFDSet;
  515. tv : timeval;
  516. begin
  517. tv.tv_sec:=0;
  518. tv.tv_usec:=0;
  519. fpFD_ZERO(fds);
  520. fpFD_SET(PFilDes(plocaleventstate(state)^.FSem)^[0],fds);
  521. pthread_mutex_lock(@plocaleventstate(state)^.feventsection);
  522. Try
  523. while fpselect(PFilDes(plocaleventstate(state)^.FSem)^[0],@fds,nil,nil,@tv) > 0 do
  524. cSemaphoreWait(plocaleventstate(state)^.FSem);
  525. finally
  526. pthread_mutex_unlock(@plocaleventstate(state)^.feventsection);
  527. end;
  528. {$endif has_sem_init or has_sem_open}
  529. end;
  530. procedure IntbasiceventSetEvent(state:peventstate);
  531. Var
  532. {$if defined(has_sem_init) or defined(has_sem_open)}
  533. Value : Longint;
  534. res : cint;
  535. err : cint;
  536. {$else}
  537. fds: TFDSet;
  538. tv : timeval;
  539. {$endif}
  540. begin
  541. pthread_mutex_lock(@plocaleventstate(state)^.feventsection);
  542. Try
  543. {$if defined(has_sem_init) or defined(has_sem_open)}
  544. if (sem_getvalue(plocaleventstate(state)^.FSem,@value) <> -1) then
  545. begin
  546. if Value=0 then
  547. cSemaphorePost(plocaleventstate(state)^.FSem);
  548. end
  549. else if (fpgeterrno = ESysENOSYS) then
  550. { not yet implemented on Mac OS X 10.4.8 }
  551. begin
  552. repeat
  553. res:=sem_trywait(psem_t(plocaleventstate(state)^.FSem));
  554. err:=fpgeterrno;
  555. until ((res<>-1) or (err<>ESysEINTR));
  556. { now we've either decreased the semaphore by 1 (if it was }
  557. { not zero), or we've done nothing (if it was already zero) }
  558. { -> increase by 1 and we have the same result as }
  559. { increasing by 1 only if it was 0 }
  560. cSemaphorePost(plocaleventstate(state)^.FSem);
  561. end
  562. else
  563. runerror(6);
  564. {$else has_sem_init or has_sem_open}
  565. tv.tv_sec:=0;
  566. tv.tv_usec:=0;
  567. fpFD_ZERO(fds);
  568. fpFD_SET(PFilDes(plocaleventstate(state)^.FSem)^[0],fds);
  569. if fpselect(PFilDes(plocaleventstate(state)^.FSem)^[0],@fds,nil,nil,@tv)=0 then
  570. cSemaphorePost(plocaleventstate(state)^.FSem);
  571. {$endif has_sem_init or has_sem_open}
  572. finally
  573. pthread_mutex_unlock(@plocaleventstate(state)^.feventsection);
  574. end;
  575. end;
  576. function IntbasiceventWaitFor(Timeout : Cardinal;state:peventstate) : longint;
  577. begin
  578. If TimeOut<>Cardinal($FFFFFFFF) then
  579. result:=wrError
  580. else
  581. begin
  582. cSemaphoreWait(plocaleventstate(state)^.FSem);
  583. result:=wrSignaled;
  584. if plocaleventstate(state)^.FManualReset then
  585. begin
  586. pthread_mutex_lock(@plocaleventstate(state)^.feventsection);
  587. Try
  588. intbasiceventresetevent(State);
  589. cSemaphorePost(plocaleventstate(state)^.FSem);
  590. Finally
  591. pthread_mutex_unlock(@plocaleventstate(state)^.feventsection);
  592. end;
  593. end;
  594. end;
  595. end;
  596. function intRTLEventCreate: PRTLEvent;
  597. var p:pintrtlevent;
  598. begin
  599. new(p);
  600. pthread_cond_init(@p^.condvar, nil);
  601. pthread_mutex_init(@p^.mutex, nil);
  602. p^.isset:=false;
  603. result:=PRTLEVENT(p);
  604. end;
  605. procedure intRTLEventDestroy(AEvent: PRTLEvent);
  606. var p:pintrtlevent;
  607. begin
  608. p:=pintrtlevent(aevent);
  609. pthread_cond_destroy(@p^.condvar);
  610. pthread_mutex_destroy(@p^.mutex);
  611. dispose(p);
  612. end;
  613. procedure intRTLEventSetEvent(AEvent: PRTLEvent);
  614. var p:pintrtlevent;
  615. begin
  616. p:=pintrtlevent(aevent);
  617. pthread_mutex_lock(@p^.mutex);
  618. p^.isset:=true;
  619. pthread_cond_signal(@p^.condvar);
  620. pthread_mutex_unlock(@p^.mutex);
  621. end;
  622. procedure intRTLEventResetEvent(AEvent: PRTLEvent);
  623. var p:pintrtlevent;
  624. begin
  625. p:=pintrtlevent(aevent);
  626. pthread_mutex_lock(@p^.mutex);
  627. p^.isset:=false;
  628. pthread_mutex_unlock(@p^.mutex);
  629. end;
  630. procedure intRTLEventWaitFor(AEvent: PRTLEvent);
  631. var p:pintrtlevent;
  632. begin
  633. p:=pintrtlevent(aevent);
  634. pthread_mutex_lock(@p^.mutex);
  635. while not p^.isset do pthread_cond_wait(@p^.condvar, @p^.mutex);
  636. p^.isset:=false;
  637. pthread_mutex_unlock(@p^.mutex);
  638. end;
  639. procedure intRTLEventWaitForTimeout(AEvent: PRTLEvent;timeout : longint);
  640. var
  641. p : pintrtlevent;
  642. errres : cint;
  643. timespec : ttimespec;
  644. tnow : timeval;
  645. begin
  646. p:=pintrtlevent(aevent);
  647. fpgettimeofday(@tnow,nil);
  648. timespec.tv_sec:=tnow.tv_sec+(timeout div 1000);
  649. timespec.tv_nsec:=(timeout mod 1000)*1000000 + tnow.tv_usec*1000;
  650. if timespec.tv_nsec >= 1000000000 then
  651. begin
  652. inc(timespec.tv_sec);
  653. dec(timespec.tv_nsec, 1000000000);
  654. end;
  655. errres:=0;
  656. pthread_mutex_lock(@p^.mutex);
  657. while (not p^.isset) and
  658. (errres <> ESysETIMEDOUT) do
  659. begin
  660. errres:=pthread_cond_timedwait(@p^.condvar, @p^.mutex, @timespec);
  661. end;
  662. p^.isset:=false;
  663. pthread_mutex_unlock(@p^.mutex);
  664. end;
  665. type
  666. threadmethod = procedure of object;
  667. Function CInitThreads : Boolean;
  668. begin
  669. {$ifdef DEBUG_MT}
  670. Writeln('Entering InitThreads.');
  671. {$endif}
  672. {$ifndef dynpthreads}
  673. Result:=True;
  674. {$else}
  675. Result:=LoadPthreads;
  676. {$endif}
  677. ThreadID := TThreadID (pthread_self);
  678. {$ifdef DEBUG_MT}
  679. Writeln('InitThreads : ',Result);
  680. {$endif DEBUG_MT}
  681. end;
  682. Function CDoneThreads : Boolean;
  683. begin
  684. {$ifndef dynpthreads}
  685. Result:=True;
  686. {$else}
  687. Result:=UnloadPthreads;
  688. {$endif}
  689. end;
  690. Var
  691. CThreadManager : TThreadManager;
  692. Procedure SetCThreadManager;
  693. begin
  694. With CThreadManager do begin
  695. InitManager :=@CInitThreads;
  696. DoneManager :=@CDoneThreads;
  697. BeginThread :=@CBeginThread;
  698. EndThread :=@CEndThread;
  699. SuspendThread :=@CSuspendThread;
  700. ResumeThread :=@CResumeThread;
  701. KillThread :=@CKillThread;
  702. ThreadSwitch :=@CThreadSwitch;
  703. WaitForThreadTerminate :=@CWaitForThreadTerminate;
  704. ThreadSetPriority :=@CThreadSetPriority;
  705. ThreadGetPriority :=@CThreadGetPriority;
  706. GetCurrentThreadId :=@CGetCurrentThreadId;
  707. InitCriticalSection :=@CInitCriticalSection;
  708. DoneCriticalSection :=@CDoneCriticalSection;
  709. EnterCriticalSection :=@CEnterCriticalSection;
  710. LeaveCriticalSection :=@CLeaveCriticalSection;
  711. InitThreadVar :=@CInitThreadVar;
  712. RelocateThreadVar :=@CRelocateThreadVar;
  713. AllocateThreadVars :=@CAllocateThreadVars;
  714. ReleaseThreadVars :=@CReleaseThreadVars;
  715. BasicEventCreate :=@intBasicEventCreate;
  716. BasicEventDestroy :=@intBasicEventDestroy;
  717. BasicEventResetEvent :=@intBasicEventResetEvent;
  718. BasicEventSetEvent :=@intBasicEventSetEvent;
  719. BasiceventWaitFor :=@intBasiceventWaitFor;
  720. rtlEventCreate :=@intrtlEventCreate;
  721. rtlEventDestroy :=@intrtlEventDestroy;
  722. rtlEventSetEvent :=@intrtlEventSetEvent;
  723. rtlEventResetEvent :=@intrtlEventResetEvent;
  724. rtleventWaitForTimeout :=@intrtleventWaitForTimeout;
  725. rtleventWaitFor :=@intrtleventWaitFor;
  726. // semaphores
  727. SemaphoreInit :=@cSemaphoreInit;
  728. SemaphoreDestroy :=@cSemaphoreDestroy;
  729. SemaphoreWait :=@cSemaphoreWait;
  730. SemaphorePost :=@cSemaphorePost;
  731. end;
  732. SetThreadManager(CThreadManager);
  733. end;
  734. initialization
  735. if ThreadingAlreadyUsed then
  736. begin
  737. writeln('Threading has been used before cthreads was initialized.');
  738. writeln('Make cthreads one of the first units in your uses clause.');
  739. runerror(211);
  740. end;
  741. SetCThreadManager;
  742. finalization
  743. end.