cthreads.pp 31 KB

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