cthreads.pp 31 KB

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