cthreads.pp 33 KB

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