cthreads.pp 33 KB

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