cthreads.pp 30 KB

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