cthreads.pp 32 KB

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