systhrds.pp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430
  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. Win32 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. {$mode objfpc}
  14. unit systhrds;
  15. interface
  16. {$S-}
  17. type
  18. { the fields of this record are os dependent }
  19. { and they shouldn't be used in a program }
  20. { only the type TCriticalSection is important }
  21. PRTLCriticalSection = ^TRTLCriticalSection;
  22. TRTLCriticalSection = packed record
  23. DebugInfo : pointer;
  24. LockCount : longint;
  25. RecursionCount : longint;
  26. OwningThread : DWord;
  27. LockSemaphore : DWord;
  28. Reserved : DWord;
  29. end;
  30. { Include generic thread interface }
  31. {$i threadh.inc}
  32. implementation
  33. function SysGetCurrentThreadId : dword;forward;
  34. {*****************************************************************************
  35. Generic overloaded
  36. *****************************************************************************}
  37. { Include generic overloaded routines }
  38. {$i thread.inc}
  39. {*****************************************************************************
  40. Local WINApi imports
  41. *****************************************************************************}
  42. const
  43. { GlobalAlloc, GlobalFlags }
  44. GMEM_FIXED = 0;
  45. GMEM_ZEROINIT = 64;
  46. function TlsAlloc : DWord;
  47. stdcall;external 'kernel32' name 'TlsAlloc';
  48. function TlsGetValue(dwTlsIndex : DWord) : pointer;
  49. stdcall;external 'kernel32' name 'TlsGetValue';
  50. function TlsSetValue(dwTlsIndex : DWord;lpTlsValue : pointer) : LongBool;
  51. stdcall;external 'kernel32' name 'TlsSetValue';
  52. function TlsFree(dwTlsIndex : DWord) : LongBool;
  53. stdcall;external 'kernel32' name 'TlsFree';
  54. function CreateThread(lpThreadAttributes : pointer;
  55. dwStackSize : DWord; lpStartAddress : pointer;lpParameter : pointer;
  56. dwCreationFlags : DWord;var lpThreadId : DWord) : Dword;
  57. stdcall;external 'kernel32' name 'CreateThread';
  58. procedure ExitThread(dwExitCode : DWord);
  59. stdcall;external 'kernel32' name 'ExitThread';
  60. function GlobalAlloc(uFlags:DWord; dwBytes:DWORD):Pointer;
  61. stdcall;external 'kernel32' name 'GlobalAlloc';
  62. function GlobalFree(hMem : Pointer):Pointer; stdcall;external 'kernel32' name 'GlobalFree';
  63. procedure Sleep(dwMilliseconds: DWord); stdcall;external 'kernel32' name 'Sleep';
  64. function WinSuspendThread (threadHandle : dword) : dword; stdcall;external 'kernel32' name 'SuspendThread';
  65. function WinResumeThread (threadHandle : dword) : dword; stdcall;external 'kernel32' name 'ResumeThread';
  66. function TerminateThread (threadHandle : dword; var exitCode : dword) : boolean; stdcall;external 'kernel32' name 'TerminateThread';
  67. function GetLastError : dword; stdcall;external 'kernel32' name 'GetLastError';
  68. function WaitForSingleObject (hHandle,Milliseconds: dword): dword; stdcall;external 'kernel32' name 'WaitForSingleObject';
  69. function WinThreadSetPriority (threadHandle : dword; Prio: longint): boolean; stdcall;external 'kernel32' name 'SetThreadPriority';
  70. function WinThreadGetPriority (threadHandle : dword): Integer; stdcall;external 'kernel32' name 'GetThreadPriority';
  71. function WinGetCurrentThreadId : dword; stdcall;external 'kernel32' name 'GetCurrentThread';
  72. {*****************************************************************************
  73. Threadvar support
  74. *****************************************************************************}
  75. {$ifdef HASTHREADVAR}
  76. const
  77. threadvarblocksize : dword = 0;
  78. var
  79. TLSKey : Dword;
  80. procedure SysInitThreadvar(var offset : dword;size : dword);
  81. begin
  82. offset:=threadvarblocksize;
  83. inc(threadvarblocksize,size);
  84. end;
  85. function SysRelocateThreadvar(offset : dword) : pointer;
  86. begin
  87. SysRelocateThreadvar:=TlsGetValue(tlskey)+Offset;
  88. end;
  89. procedure SysAllocateThreadVars;
  90. var
  91. dataindex : pointer;
  92. begin
  93. { we've to allocate the memory from system }
  94. { because the FPC heap management uses }
  95. { exceptions which use threadvars but }
  96. { these aren't allocated yet ... }
  97. { allocate room on the heap for the thread vars }
  98. dataindex:=pointer(GlobalAlloc(GMEM_FIXED or GMEM_ZEROINIT,threadvarblocksize));
  99. TlsSetValue(tlskey,dataindex);
  100. end;
  101. procedure SysReleaseThreadVars;
  102. begin
  103. GlobalFree(TlsGetValue(tlskey));
  104. end;
  105. { Include OS independent Threadvar initialization }
  106. {$i threadvr.inc}
  107. {$endif HASTHREADVAR}
  108. {*****************************************************************************
  109. Thread starting
  110. *****************************************************************************}
  111. type
  112. pthreadinfo = ^tthreadinfo;
  113. tthreadinfo = record
  114. f : tthreadfunc;
  115. p : pointer;
  116. stklen : cardinal;
  117. end;
  118. procedure DoneThread;
  119. begin
  120. { Release Threadvars }
  121. {$ifdef HASTHREADVAR}
  122. SysReleaseThreadVars;
  123. {$endif HASTHREADVAR}
  124. end;
  125. function ThreadMain(param : pointer) : pointer;cdecl;
  126. var
  127. ti : tthreadinfo;
  128. begin
  129. {$ifdef HASTHREADVAR}
  130. { Allocate local thread vars, this must be the first thing,
  131. because the exception management and io depends on threadvars }
  132. SysAllocateThreadVars;
  133. {$endif HASTHREADVAR}
  134. { Copy parameter to local data }
  135. {$ifdef DEBUG_MT}
  136. writeln('New thread started, initialising ...');
  137. {$endif DEBUG_MT}
  138. ti:=pthreadinfo(param)^;
  139. dispose(pthreadinfo(param));
  140. { Initialize thread }
  141. InitThread(ti.stklen);
  142. { Start thread function }
  143. {$ifdef DEBUG_MT}
  144. writeln('Jumping to thread function');
  145. {$endif DEBUG_MT}
  146. ThreadMain:=pointer(ti.f(ti.p));
  147. end;
  148. function SysBeginThread(sa : Pointer;stacksize : dword;
  149. ThreadFunction : tthreadfunc;p : pointer;
  150. creationFlags : dword; var ThreadId : DWord) : DWord;
  151. var
  152. ti : pthreadinfo;
  153. begin
  154. {$ifdef DEBUG_MT}
  155. writeln('Creating new thread');
  156. {$endif DEBUG_MT}
  157. { Initialize multithreading if not done }
  158. if not IsMultiThread then
  159. begin
  160. {$ifdef HASTHREADVAR}
  161. { We're still running in single thread mode, setup the TLS }
  162. TLSKey:=TlsAlloc;
  163. InitThreadVars(@SysRelocateThreadvar);
  164. {$endif HASTHREADVAR}
  165. IsMultiThread:=true;
  166. end;
  167. { the only way to pass data to the newly created thread
  168. in a MT safe way, is to use the heap }
  169. new(ti);
  170. ti^.f:=ThreadFunction;
  171. ti^.p:=p;
  172. ti^.stklen:=stacksize;
  173. { call pthread_create }
  174. {$ifdef DEBUG_MT}
  175. writeln('Starting new thread');
  176. {$endif DEBUG_MT}
  177. SysBeginThread:=CreateThread(sa,stacksize,@ThreadMain,ti,creationflags,threadid);
  178. end;
  179. procedure SysEndThread(ExitCode : DWord);
  180. begin
  181. DoneThread;
  182. ExitThread(ExitCode);
  183. end;
  184. procedure SysThreadSwitch;
  185. begin
  186. Sleep(0);
  187. end;
  188. function SysSuspendThread (threadHandle : dword) : dword;
  189. begin
  190. SysSuspendThread:=WinSuspendThread(threadHandle);
  191. end;
  192. function SysResumeThread (threadHandle : dword) : dword;
  193. begin
  194. SysResumeThread:=WinResumeThread(threadHandle);
  195. end;
  196. function SysKillThread (threadHandle : dword) : dword;
  197. var exitCode : dword;
  198. begin
  199. if not TerminateThread (threadHandle, exitCode) then
  200. SysKillThread := GetLastError
  201. else
  202. SysKillThread := 0;
  203. end;
  204. function SysWaitForThreadTerminate (threadHandle : dword; TimeoutMs : longint) : dword;
  205. begin
  206. if timeoutMs = 0 then dec (timeoutMs); // $ffffffff is INFINITE
  207. SysWaitForThreadTerminate := WaitForSingleObject(threadHandle, TimeoutMs);
  208. end;
  209. function SysThreadSetPriority (threadHandle : dword; Prio: longint): boolean; {-15..+15, 0=normal}
  210. begin
  211. SysThreadSetPriority:=WinThreadSetPriority(threadHandle,Prio);
  212. end;
  213. function SysThreadGetPriority (threadHandle : dword): Integer;
  214. begin
  215. SysThreadGetPriority:=WinThreadGetPriority(threadHandle);
  216. end;
  217. function SysGetCurrentThreadId : dword;
  218. begin
  219. SysGetCurrentThreadId:=WinGetCurrentThreadId;
  220. end;
  221. {*****************************************************************************
  222. Delphi/Win32 compatibility
  223. *****************************************************************************}
  224. procedure WinInitCriticalSection(var cs : TRTLCriticalSection);
  225. stdcall;external 'kernel32' name 'InitializeCriticalSection';
  226. procedure WinDoneCriticalSection(var cs : TRTLCriticalSection);
  227. stdcall;external 'kernel32' name 'DeleteCriticalSection';
  228. procedure WinEnterCriticalSection(var cs : TRTLCriticalSection);
  229. stdcall;external 'kernel32' name 'EnterCriticalSection';
  230. procedure WinLeaveCriticalSection(var cs : TRTLCriticalSection);
  231. stdcall;external 'kernel32' name 'LeaveCriticalSection';
  232. procedure SySInitCriticalSection(var cs);
  233. begin
  234. WinInitCriticalSection(PRTLCriticalSection(@cs)^);
  235. end;
  236. procedure SysDoneCriticalSection(var cs);
  237. begin
  238. WinDoneCriticalSection(PRTLCriticalSection(@cs)^);
  239. end;
  240. procedure SysEnterCriticalSection(var cs);
  241. begin
  242. WinEnterCriticalSection(PRTLCriticalSection(@cs)^);
  243. end;
  244. procedure SySLeaveCriticalSection(var cs);
  245. begin
  246. WinLeaveCriticalSection(PRTLCriticalSection(@cs)^);
  247. end;
  248. {*****************************************************************************
  249. Heap Mutex Protection
  250. *****************************************************************************}
  251. var
  252. HeapMutex : TRTLCriticalSection;
  253. procedure Win32HeapMutexInit;
  254. begin
  255. InitCriticalSection(heapmutex);
  256. end;
  257. procedure Win32HeapMutexDone;
  258. begin
  259. DoneCriticalSection(heapmutex);
  260. end;
  261. procedure Win32HeapMutexLock;
  262. begin
  263. EnterCriticalSection(heapmutex);
  264. end;
  265. procedure Win32HeapMutexUnlock;
  266. begin
  267. LeaveCriticalSection(heapmutex);
  268. end;
  269. const
  270. Win32MemoryMutexManager : TMemoryMutexManager = (
  271. MutexInit : @Win32HeapMutexInit;
  272. MutexDone : @Win32HeapMutexDone;
  273. MutexLock : @Win32HeapMutexLock;
  274. MutexUnlock : @Win32HeapMutexUnlock;
  275. );
  276. procedure InitHeapMutexes;
  277. begin
  278. SetMemoryMutexManager(Win32MemoryMutexManager);
  279. end;
  280. Var
  281. WinThreadManager : TThreadManager;
  282. Procedure SetWinThreadManager;
  283. begin
  284. With WinThreadManager do
  285. begin
  286. InitManager :=Nil;
  287. DoneManager :=Nil;
  288. BeginThread :=@SysBeginThread;
  289. EndThread :=@SysEndThread;
  290. SuspendThread :=@SysSuspendThread;
  291. ResumeThread :=@SysResumeThread;
  292. KillThread :=@SysKillThread;
  293. ThreadSwitch :=@SysThreadSwitch;
  294. WaitForThreadTerminate :=@SysWaitForThreadTerminate;
  295. ThreadSetPriority :=@SysThreadSetPriority;
  296. ThreadGetPriority :=@SysThreadGetPriority;
  297. GetCurrentThreadId :=@SysGetCurrentThreadId;
  298. InitCriticalSection :=@SysInitCriticalSection;
  299. DoneCriticalSection :=@SysDoneCriticalSection;
  300. EnterCriticalSection :=@SysEnterCriticalSection;
  301. LeaveCriticalSection :=@SysLeaveCriticalSection;
  302. {$ifdef HASTHREADVAR}
  303. InitThreadVar :=@SysInitThreadVar;
  304. RelocateThreadVar :=@SysRelocateThreadVar;
  305. AllocateThreadVars :=@SysAllocateThreadVars;
  306. ReleaseThreadVars :=@SysReleaseThreadVars;
  307. {$endif HASTHREADVAR}
  308. end;
  309. SetThreadManager(WinThreadManager);
  310. InitHeapMutexes;
  311. end;
  312. initialization
  313. SetWinThreadManager;
  314. end.
  315. {
  316. $Log$
  317. Revision 1.10 2004-01-21 14:15:42 florian
  318. * fixed win32 compilation
  319. Revision 1.9 2003/11/29 17:34:53 michael
  320. + Removed dummy variable from SetCthreadManager
  321. Revision 1.8 2003/11/27 10:28:41 michael
  322. + Patch from peter to fix make cycle
  323. Revision 1.7 2003/11/26 20:10:59 michael
  324. + New threadmanager implementation
  325. Revision 1.6 2003/10/01 21:00:09 peter
  326. * GetCurrentThreadHandle renamed to GetCurrentThreadId
  327. Revision 1.5 2003/09/17 15:06:36 peter
  328. * stdcall patch
  329. Revision 1.4 2003/03/27 17:14:27 armin
  330. * more platform independent thread routines, needs to be implemented for unix
  331. Revision 1.3 2003/03/24 16:12:01 jonas
  332. * BeginThread() now returns the thread handle instead of the threadid
  333. (needed because you have to free the handle after your thread is
  334. finished, and the threadid is already returned via a var-parameter)
  335. Revision 1.2 2002/10/31 13:45:44 carl
  336. * threadvar.inc -> threadvr.inc
  337. Revision 1.1 2002/10/16 06:27:30 michael
  338. + Renamed thread unit to systhrds
  339. Revision 1.1 2002/10/14 19:39:18 peter
  340. * threads unit added for thread support
  341. }