2
0

systhrds.pp 13 KB

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