cthreads.pp 32 KB

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