cthreads.pp 27 KB

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