cthreads.pp 27 KB

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