cthreads.pp 28 KB

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