systhrds.pp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  1. {
  2. $Id$
  3. This file is part of the Free Pascal run time library.
  4. Copyright (c) 2002 by Peter Vreman,
  5. member of the Free Pascal development team.
  6. Linux (pthreads) threading support implementation
  7. See the file COPYING.FPC, included in this distribution,
  8. for details about the copyright.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  12. **********************************************************************}
  13. unit systhrds;
  14. interface
  15. {$S-}
  16. {$ifndef BSD}
  17. {$linklib c}
  18. {$linklib pthread}
  19. {$else}
  20. // Link reentrant libc with pthreads
  21. {$linklib c_r}
  22. {$endif}
  23. type
  24. PRTLCriticalSection = ^TRTLCriticalSection;
  25. TRTLCriticalSection = record
  26. m_spinlock : longint;
  27. m_count : longint;
  28. m_owner : pointer {pthread_t};
  29. m_kind : longint;
  30. m_waiting : record
  31. head,tail : pointer;
  32. end; {_pthread_queue}
  33. end;
  34. { Include generic thread interface }
  35. {$i threadh.inc}
  36. implementation
  37. Uses BaseUnix,unix;
  38. {*****************************************************************************
  39. Generic overloaded
  40. *****************************************************************************}
  41. {$i ostypes.inc}
  42. {$i ossysch.inc}
  43. { Include generic overloaded routines }
  44. {$i thread.inc}
  45. {$ifndef BSD}
  46. {$i pthread.inc}
  47. {$else}
  48. {$i ptypes.inc}
  49. CONST PTHREAD_EXPLICIT_SCHED = 0;
  50. PTHREAD_CREATE_DETACHED = 1;
  51. PTHREAD_SCOPE_PROCESS = 0;
  52. TYPE
  53. pthread_t = pointer;
  54. ppthread_t = ^pthread_t;
  55. pthread_key_t = cint;
  56. ppthread_key_t = ^pthread_key_t;
  57. pthread_mutex_t = pointer;
  58. ppthread_mutex_t= ^pthread_mutex_t;
  59. pthread_attr_t = pointer; // opague
  60. ppthread_attr_t = ^pthread_attr_t; // opague
  61. __destr_func_t = procedure (p :pointer);cdecl;
  62. __startroutine_t= function (p :pointer):pointer;cdecl;
  63. pthread_mutex_attr_t = pointer;
  64. ppthread_mutex_attr_t = ^pthread_mutex_t;
  65. function pthread_getspecific (t : pthread_key_t):pointer; cdecl; external;
  66. function pthread_setspecific (t : pthread_key_t;p:pointer):cint; cdecl; external;
  67. function pthread_key_create (p : ppthread_key_t;f: __destr_func_t):cint; cdecl;external;
  68. function pthread_attr_init (p : ppthread_key_t):cint; cdecl; external;
  69. function pthread_attr_setinheritsched(p : ppthread_attr_t;i:cint):cint; cdecl; external;
  70. function pthread_attr_setscope (p : ppthread_attr_t;i:cint):cint;cdecl;external;
  71. function pthread_attr_setdetachstate (p : ppthread_attr_t;i:cint):cint;cdecl;external;
  72. function pthread_create ( p: ppthread_t;attr : ppthread_attr_t;f:__startroutine_t;arg:pointer):cint;cdecl;external;
  73. procedure pthread_exit ( p: pointer); cdecl;external;
  74. function pthread_mutex_init (p:ppthread_mutex_t;o:ppthread_mutex_attr_t):cint; cdecl;external;
  75. function pthread_mutex_destroy (p:ppthread_mutex_attr_t):cint; cdecl;external;
  76. function pthread_mutex_lock (p:ppthread_mutex_attr_t):cint; cdecl;external;
  77. function pthread_mutex_unlock (p:ppthread_mutex_attr_t):cint; cdecl;external;
  78. {$endif}
  79. {*****************************************************************************
  80. System dependent memory allocation
  81. *****************************************************************************}
  82. {$ifndef BSD}
  83. Const
  84. { Constants for MMAP }
  85. MAP_PRIVATE =2;
  86. MAP_ANONYMOUS =$20;
  87. {$else}
  88. {$ifdef FreeBSD}
  89. CONST
  90. { Constants for MMAP. These are still private for *BSD }
  91. MAP_PRIVATE =2;
  92. MAP_ANONYMOUS =$1000;
  93. {$ELSE}
  94. {$ENTER ME}
  95. {$ENDIF}
  96. {$ENDIF}
  97. {*****************************************************************************
  98. Threadvar support
  99. *****************************************************************************}
  100. {$ifdef HASTHREADVAR}
  101. const
  102. threadvarblocksize : dword = 0;
  103. var
  104. TLSKey : pthread_key_t;
  105. procedure SysInitThreadvar(var offset : dword;size : dword);
  106. begin
  107. offset:=threadvarblocksize;
  108. inc(threadvarblocksize,size);
  109. end;
  110. function SysRelocateThreadvar(offset : dword) : pointer;
  111. begin
  112. SysRelocateThreadvar:=pthread_getspecific(tlskey)+Offset;
  113. end;
  114. procedure SysAllocateThreadVars;
  115. var
  116. dataindex : pointer;
  117. begin
  118. { we've to allocate the memory from system }
  119. { because the FPC heap management uses }
  120. { exceptions which use threadvars but }
  121. { these aren't allocated yet ... }
  122. { allocate room on the heap for the thread vars }
  123. DataIndex:=Pointer(Fpmmap(0,threadvarblocksize,3,MAP_PRIVATE+MAP_ANONYMOUS,-1,0));
  124. FillChar(DataIndex^,threadvarblocksize,0);
  125. pthread_setspecific(tlskey,dataindex);
  126. end;
  127. procedure SysReleaseThreadVars;
  128. begin
  129. {$ifdef ver1_0}
  130. Fpmunmap(longint(pthread_getspecific(tlskey)),threadvarblocksize);
  131. {$else}
  132. Fpmunmap(pointer(pthread_getspecific(tlskey)),threadvarblocksize);
  133. {$endif}
  134. end;
  135. { Include OS independent Threadvar initialization }
  136. {$i threadvr.inc}
  137. {$endif HASTHREADVAR}
  138. {*****************************************************************************
  139. Thread starting
  140. *****************************************************************************}
  141. type
  142. pthreadinfo = ^tthreadinfo;
  143. tthreadinfo = record
  144. f : tthreadfunc;
  145. p : pointer;
  146. stklen : cardinal;
  147. end;
  148. procedure DoneThread;
  149. begin
  150. { Release Threadvars }
  151. {$ifdef HASTHREADVAR}
  152. SysReleaseThreadVars;
  153. {$endif HASTHREADVAR}
  154. end;
  155. function ThreadMain(param : pointer) : pointer;cdecl;
  156. var
  157. ti : tthreadinfo;
  158. begin
  159. {$ifdef HASTHREADVAR}
  160. { Allocate local thread vars, this must be the first thing,
  161. because the exception management and io depends on threadvars }
  162. SysAllocateThreadVars;
  163. {$endif HASTHREADVAR}
  164. { Copy parameter to local data }
  165. {$ifdef DEBUG_MT}
  166. writeln('New thread started, initialising ...');
  167. {$endif DEBUG_MT}
  168. ti:=pthreadinfo(param)^;
  169. dispose(pthreadinfo(param));
  170. { Initialize thread }
  171. InitThread(ti.stklen);
  172. { Start thread function }
  173. {$ifdef DEBUG_MT}
  174. writeln('Jumping to thread function');
  175. {$endif DEBUG_MT}
  176. ThreadMain:=pointer(ti.f(ti.p));
  177. end;
  178. function BeginThread(sa : Pointer;stacksize : dword;
  179. ThreadFunction : tthreadfunc;p : pointer;
  180. creationFlags : dword; var ThreadId : DWord) : DWord;
  181. var
  182. ti : pthreadinfo;
  183. thread_attr : pthread_attr_t;
  184. begin
  185. {$ifdef DEBUG_MT}
  186. writeln('Creating new thread');
  187. {$endif DEBUG_MT}
  188. { Initialize multithreading if not done }
  189. if not IsMultiThread then
  190. begin
  191. {$ifdef HASTHREADVAR}
  192. { We're still running in single thread mode, setup the TLS }
  193. pthread_key_create(@TLSKey,nil);
  194. InitThreadVars(@SysRelocateThreadvar);
  195. {$endif HASTHREADVAR}
  196. IsMultiThread:=true;
  197. end;
  198. { the only way to pass data to the newly created thread
  199. in a MT safe way, is to use the heap }
  200. new(ti);
  201. ti^.f:=ThreadFunction;
  202. ti^.p:=p;
  203. ti^.stklen:=stacksize;
  204. { call pthread_create }
  205. {$ifdef DEBUG_MT}
  206. writeln('Starting new thread');
  207. {$endif DEBUG_MT}
  208. pthread_attr_init(@thread_attr);
  209. pthread_attr_setinheritsched(@thread_attr, PTHREAD_EXPLICIT_SCHED);
  210. pthread_attr_setscope(@thread_attr, PTHREAD_SCOPE_PROCESS);
  211. pthread_attr_setdetachstate(@thread_attr, PTHREAD_CREATE_DETACHED);
  212. pthread_create(@threadid, @thread_attr, @ThreadMain,ti);
  213. BeginThread:=threadid;
  214. end;
  215. procedure EndThread(ExitCode : DWord);
  216. begin
  217. DoneThread;
  218. pthread_exit(pointer(ExitCode));
  219. end;
  220. function SuspendThread (threadHandle : dword) : dword;
  221. begin
  222. {$Warning SuspendThread needs to be implemented}
  223. end;
  224. function ResumeThread (threadHandle : dword) : dword;
  225. begin
  226. {$Warning ResumeThread needs to be implemented}
  227. end;
  228. procedure ThreadSwitch; {give time to other threads}
  229. begin
  230. {extern int pthread_yield (void) __THROW;}
  231. {$Warning ThreadSwitch needs to be implemented}
  232. end;
  233. function KillThread (threadHandle : dword) : dword;
  234. begin
  235. {$Warning KillThread needs to be implemented}
  236. end;
  237. function WaitForThreadTerminate (threadHandle : dword; TimeoutMs : longint) : dword; {0=no timeout}
  238. begin
  239. {$Warning WaitForThreadTerminate needs to be implemented}
  240. end;
  241. function ThreadSetPriority (threadHandle : dword; Prio: longint): boolean; {-15..+15, 0=normal}
  242. begin
  243. {$Warning ThreadSetPriority needs to be implemented}
  244. end;
  245. function ThreadGetPriority (threadHandle : dword): Integer;
  246. begin
  247. {$Warning ThreadGetPriority needs to be implemented}
  248. end;
  249. function GetCurrentThreadHandle : dword;
  250. begin
  251. {$Warning ThreadGetPriority needs to be implemented}
  252. end;
  253. {*****************************************************************************
  254. Delphi/Win32 compatibility
  255. *****************************************************************************}
  256. procedure InitCriticalSection(var CS:TRTLCriticalSection);
  257. begin
  258. cs.m_spinlock:=0;
  259. cs.m_count:=0;
  260. cs.m_owner:=0;
  261. cs.m_kind:=1;
  262. cs.m_waiting.head:=0;
  263. cs.m_waiting.tail:=0;
  264. pthread_mutex_init(@CS,NIL);
  265. end;
  266. procedure EnterCriticalSection(var CS:TRTLCriticalSection);
  267. begin
  268. pthread_mutex_lock(@CS);
  269. end;
  270. procedure LeaveCriticalSection(var CS:TRTLCriticalSection);
  271. begin
  272. pthread_mutex_unlock(@CS);
  273. end;
  274. procedure DoneCriticalSection(var CS:TRTLCriticalSection);
  275. begin
  276. pthread_mutex_destroy(@CS);
  277. end;
  278. {*****************************************************************************
  279. Heap Mutex Protection
  280. *****************************************************************************}
  281. var
  282. HeapMutex : pthread_mutex_t;
  283. procedure PThreadHeapMutexInit;
  284. begin
  285. pthread_mutex_init(@heapmutex,nil);
  286. end;
  287. procedure PThreadHeapMutexDone;
  288. begin
  289. pthread_mutex_destroy(@heapmutex);
  290. end;
  291. procedure PThreadHeapMutexLock;
  292. begin
  293. pthread_mutex_lock(@heapmutex);
  294. end;
  295. procedure PThreadHeapMutexUnlock;
  296. begin
  297. pthread_mutex_unlock(@heapmutex);
  298. end;
  299. const
  300. PThreadMemoryMutexManager : TMemoryMutexManager = (
  301. MutexInit : @PThreadHeapMutexInit;
  302. MutexDone : @PThreadHeapMutexDone;
  303. MutexLock : @PThreadHeapMutexLock;
  304. MutexUnlock : @PThreadHeapMutexUnlock;
  305. );
  306. procedure InitHeapMutexes;
  307. begin
  308. SetMemoryMutexManager(PThreadMemoryMutexManager);
  309. end;
  310. initialization
  311. InitHeapMutexes;
  312. end.
  313. {
  314. $Log$
  315. Revision 1.13 2003-09-20 12:38:29 marco
  316. * FCL now compiles for FreeBSD with new 1.1. Now Linux.
  317. Revision 1.12 2003/09/16 13:17:03 marco
  318. * Wat cleanup, ouwe syscalls nu via baseunix e.d.
  319. Revision 1.11 2003/09/16 13:00:02 marco
  320. * small BSD gotcha removed (typing mmap params)
  321. Revision 1.10 2003/09/15 20:08:49 marco
  322. * small fixes. FreeBSD now cycles
  323. Revision 1.9 2003/09/14 20:15:01 marco
  324. * Unix reform stage two. Remove all calls from Unix that exist in Baseunix.
  325. Revision 1.8 2003/03/27 17:14:27 armin
  326. * more platform independent thread routines, needs to be implemented for unix
  327. Revision 1.7 2003/01/05 19:11:32 marco
  328. * small changes originating from introduction of Baseunix to FreeBSD
  329. Revision 1.6 2002/11/11 21:41:06 marco
  330. * syscall.inc -> syscallo.inc
  331. Revision 1.5 2002/10/31 13:45:21 carl
  332. * threadvar.inc -> threadvr.inc
  333. Revision 1.4 2002/10/26 18:27:52 marco
  334. * First series POSIX calls commits. Including getcwd.
  335. Revision 1.3 2002/10/18 18:05:06 marco
  336. * $I pthread.inc instead of pthreads.inc
  337. Revision 1.2 2002/10/18 12:19:59 marco
  338. * Fixes to get the generic *BSD RTL compiling again + fixes for thread
  339. support. Still problems left in fexpand. (inoutres?) Therefore fixed
  340. sysposix not yet commited
  341. Revision 1.1 2002/10/16 06:22:56 michael
  342. Threads renamed from threads to systhrds
  343. Revision 1.1 2002/10/14 19:39:17 peter
  344. * threads unit added for thread support
  345. }