systhrd.inc 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477
  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. Linux (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. {*****************************************************************************
  13. Local WINApi imports
  14. *****************************************************************************}
  15. const
  16. { LocalAlloc flags }
  17. LMEM_FIXED = 0;
  18. LMEM_ZEROINIT = 64;
  19. {$ifndef WINCE}
  20. function TlsAlloc : DWord;
  21. stdcall;external KernelDLL name 'TlsAlloc';
  22. function TlsFree(dwTlsIndex : DWord) : LongBool;
  23. stdcall;external KernelDLL name 'TlsFree';
  24. {$endif WINCE}
  25. function TlsGetValue(dwTlsIndex : DWord) : pointer;
  26. {$ifdef wince}cdecl{$else}stdcall{$endif};external KernelDLL name 'TlsGetValue';
  27. function TlsSetValue(dwTlsIndex : DWord;lpTlsValue : pointer) : LongBool;
  28. {$ifdef wince}cdecl{$else}stdcall{$endif};external KernelDLL name 'TlsSetValue';
  29. function CreateThread(lpThreadAttributes : pointer;
  30. dwStackSize : SIZE_T; lpStartAddress : pointer;lpParameter : pointer;
  31. dwCreationFlags : DWord;var lpThreadId : DWord) : THandle;
  32. {$ifdef wince}cdecl{$else}stdcall{$endif};external KernelDLL name 'CreateThread';
  33. procedure ExitThread(dwExitCode : DWord);
  34. {$ifdef wince}cdecl{$else}stdcall{$endif};external KernelDLL name 'ExitThread';
  35. function LocalAlloc(uFlags:DWord; dwBytes:DWORD):Pointer;
  36. {$ifdef wince}cdecl{$else}stdcall{$endif};external KernelDLL name 'LocalAlloc';
  37. function LocalFree(hMem : Pointer):Pointer; {$ifdef wince}cdecl{$else}stdcall{$endif};external KernelDLL name 'LocalFree';
  38. procedure Sleep(dwMilliseconds: DWord); {$ifdef wince}cdecl{$else}stdcall{$endif};external KernelDLL name 'Sleep';
  39. function WinSuspendThread (threadHandle : THandle) : dword; {$ifdef wince}cdecl{$else}stdcall{$endif};external KernelDLL name 'SuspendThread';
  40. function WinResumeThread (threadHandle : THandle) : dword; {$ifdef wince}cdecl{$else}stdcall{$endif};external KernelDLL name 'ResumeThread';
  41. function TerminateThread (threadHandle : THandle; var exitCode : dword) : boolean; {$ifdef wince}cdecl{$else}stdcall{$endif};external KernelDLL name 'TerminateThread';
  42. function WaitForSingleObject (hHandle : THandle;Milliseconds: dword): dword; {$ifdef wince}cdecl{$else}stdcall{$endif};external KernelDLL name 'WaitForSingleObject';
  43. function WinThreadSetPriority (threadHandle : THandle; Prio: longint): boolean; {$ifdef wince}cdecl{$else}stdcall{$endif};external KernelDLL name 'SetThreadPriority';
  44. function WinThreadGetPriority (threadHandle : THandle): LongInt; {$ifdef wince}cdecl{$else}stdcall{$endif};external KernelDLL name 'GetThreadPriority';
  45. {$ifndef WINCE}
  46. function CreateEvent(lpEventAttributes:pointer;bManualReset:longbool;bInitialState:longbool;lpName:pchar): THandle; stdcall; external KernelDLL name 'CreateEventA';
  47. function ResetEvent(hEvent:THandle):LONGBOOL; stdcall; external KernelDLL name 'ResetEvent';
  48. function SetEvent(hEvent:THandle):LONGBOOL; stdcall; external KernelDLL name 'SetEvent';
  49. {$endif WINCE}
  50. CONST
  51. WAIT_OBJECT_0 = 0;
  52. WAIT_ABANDONED_0 = $80;
  53. WAIT_TIMEOUT = $102;
  54. WAIT_IO_COMPLETION = $c0;
  55. WAIT_ABANDONED = $80;
  56. WAIT_FAILED = $ffffffff;
  57. {*****************************************************************************
  58. Threadvar support
  59. *****************************************************************************}
  60. const
  61. threadvarblocksize : dword = 0;
  62. const
  63. TLSKey : DWord = $ffffffff;
  64. procedure SysInitThreadvar(var offset : dword;size : dword);
  65. begin
  66. offset:=threadvarblocksize;
  67. {$ifdef CPUARM}
  68. // Data must be allocated at 4 bytes boundary for ARM
  69. size:=(size + 3) and not dword(3);
  70. {$endif CPUARM}
  71. inc(threadvarblocksize,size);
  72. end;
  73. procedure SysAllocateThreadVars;
  74. var
  75. dataindex : pointer;
  76. begin
  77. { we've to allocate the memory from system }
  78. { because the FPC heap management uses }
  79. { exceptions which use threadvars but }
  80. { these aren't allocated yet ... }
  81. { allocate room on the heap for the thread vars }
  82. dataindex:=pointer(LocalAlloc(LMEM_FIXED or LMEM_ZEROINIT,threadvarblocksize));
  83. TlsSetValue(tlskey,dataindex);
  84. end;
  85. function SysRelocateThreadvar(offset : dword) : pointer;
  86. var
  87. dataindex : pointer;
  88. errorsave : dword;
  89. begin
  90. {$ifdef dummy}
  91. { it least in the on windows 7 x64, this still doesn't not work, fs:(0x2c) is
  92. self referencing on this system (FK)
  93. MVC: It also does not work on Windows Vista 32-bit, Home Premium, SP 1. Results in a crash}
  94. asm
  95. movl TLSKey,%edx
  96. movl %fs:(0x2c),%eax
  97. orl %eax,%eax
  98. jnz .LAddressInEAX
  99. { this works on Windows 7, but I don't know if it works on other OSes (FK) }
  100. movl %fs:(0x18),%eax
  101. movl 0xe10(%eax,%edx,4),%eax
  102. jmp .LToDataIndex
  103. .LAddressInEAX:
  104. movl (%eax,%edx,4),%eax
  105. .LToDataIndex:
  106. movl %eax,dataindex
  107. end;
  108. if DataIndex=nil then
  109. begin
  110. errorsave:=GetLastError;
  111. SysAllocateThreadVars;
  112. DataIndex:=TlsGetValue(tlskey);
  113. SetLastError(errorsave);
  114. end;
  115. {$else win32}
  116. errorsave:=GetLastError;
  117. dataindex:=TlsGetValue(tlskey);
  118. if dataindex=nil then
  119. begin
  120. SysAllocateThreadVars;
  121. dataindex:=TlsGetValue(tlskey);
  122. end;
  123. SetLastError(errorsave);
  124. {$endif win32}
  125. SysRelocateThreadvar:=DataIndex+Offset;
  126. end;
  127. procedure SysReleaseThreadVars;
  128. begin
  129. LocalFree(TlsGetValue(tlskey));
  130. TlsSetValue(tlskey, nil);
  131. end;
  132. {*****************************************************************************
  133. Thread starting
  134. *****************************************************************************}
  135. type
  136. pthreadinfo = ^tthreadinfo;
  137. tthreadinfo = record
  138. f : tthreadfunc;
  139. p : pointer;
  140. stklen : cardinal;
  141. end;
  142. function ThreadMain(param : pointer) : Longint; {$ifdef wince}cdecl{$else}stdcall{$endif};
  143. var
  144. ti : tthreadinfo;
  145. begin
  146. { Allocate local thread vars, this must be the first thing,
  147. because the exception management and io depends on threadvars }
  148. SysAllocateThreadVars;
  149. { Copy parameter to local data }
  150. ti:=pthreadinfo(param)^;
  151. dispose(pthreadinfo(param));
  152. { Initialize thread }
  153. InitThread(ti.stklen);
  154. { Start thread function }
  155. {$ifdef DEBUG_MT}
  156. writeln('Jumping to thread function of thread ',Win32GetCurrentThreadId);
  157. {$endif DEBUG_MT}
  158. ThreadMain:=ti.f(ti.p);
  159. end;
  160. procedure SysInitMultithreading;
  161. begin
  162. { do not check IsMultiThread, as program could have altered it, out of Delphi habit }
  163. { the thread attach/detach code uses locks to avoid multiple calls of this }
  164. if TLSKey=$ffffffff then
  165. begin
  166. { We're still running in single thread mode, setup the TLS }
  167. TLSKey:=TlsAlloc;
  168. InitThreadVars(@SysRelocateThreadvar);
  169. { allocate the thread vars for the main thread }
  170. IsMultiThread:=true;
  171. end;
  172. end;
  173. procedure SysFiniMultithreading;
  174. begin
  175. if IsMultiThread then
  176. begin
  177. TlsFree(TLSKey);
  178. TLSKey:=$ffffffff;
  179. end;
  180. end;
  181. function SysBeginThread(sa : Pointer;stacksize : ptruint;
  182. ThreadFunction : tthreadfunc;p : pointer;
  183. creationFlags : dword;var ThreadId : TThreadID) : TThreadID;
  184. var
  185. ti : pthreadinfo;
  186. _threadid : dword;
  187. begin
  188. {$ifdef DEBUG_MT}
  189. writeln('Creating new thread');
  190. {$endif DEBUG_MT}
  191. { Initialize multithreading if not done }
  192. SysInitMultithreading;
  193. { the only way to pass data to the newly created thread
  194. in a MT safe way, is to use the heap }
  195. new(ti);
  196. ti^.f:=ThreadFunction;
  197. ti^.p:=p;
  198. ti^.stklen:=stacksize;
  199. {$ifdef DEBUG_MT}
  200. writeln('Starting new thread');
  201. {$endif DEBUG_MT}
  202. _threadid:=0;
  203. SysBeginThread:=CreateThread(sa,stacksize,@ThreadMain,ti,creationflags,_threadid);
  204. { creation failed? if yes, we dispose the parameter record }
  205. if SysBeginThread=0 then
  206. begin
  207. {$ifdef DEBUG_MT}
  208. writeln('Thread creation failed');
  209. {$endif DEBUG_MT}
  210. dispose(ti);
  211. end;
  212. ThreadID:=_threadid;
  213. end;
  214. procedure SysEndThread(ExitCode : DWord);
  215. begin
  216. DoneThread;
  217. ExitThread(ExitCode);
  218. end;
  219. procedure SysThreadSwitch;
  220. begin
  221. Sleep(0);
  222. end;
  223. function SysSuspendThread (threadHandle : TThreadID) : dword;
  224. begin
  225. SysSuspendThread:=WinSuspendThread(threadHandle);
  226. end;
  227. function SysResumeThread (threadHandle : TThreadID) : dword;
  228. begin
  229. SysResumeThread:=WinResumeThread(threadHandle);
  230. end;
  231. function SysKillThread (threadHandle : TThreadID) : dword;
  232. var exitCode : dword;
  233. begin
  234. if not TerminateThread (threadHandle, exitCode) then
  235. SysKillThread := GetLastError
  236. else
  237. SysKillThread := 0;
  238. end;
  239. function SysWaitForThreadTerminate (threadHandle : TThreadID; TimeoutMs : longint) : dword;
  240. begin
  241. if timeoutMs = 0 then dec (timeoutMs); // $ffffffff is INFINITE
  242. SysWaitForThreadTerminate := WaitForSingleObject(threadHandle, TimeoutMs);
  243. end;
  244. function SysThreadSetPriority (threadHandle : TThreadID; Prio: longint): boolean; {-15..+15, 0=normal}
  245. begin
  246. SysThreadSetPriority:=WinThreadSetPriority(threadHandle,Prio);
  247. end;
  248. function SysThreadGetPriority (threadHandle : TThreadID): longint;
  249. begin
  250. SysThreadGetPriority:=WinThreadGetPriority(threadHandle);
  251. end;
  252. function SysGetCurrentThreadId : TThreadID;
  253. begin
  254. SysGetCurrentThreadId:=Win32GetCurrentThreadId;
  255. end;
  256. {*****************************************************************************
  257. Delphi/Win32 compatibility
  258. *****************************************************************************}
  259. procedure WinInitCriticalSection(var cs : TRTLCriticalSection);
  260. {$ifdef wince}cdecl{$else}stdcall{$endif};external KernelDLL name 'InitializeCriticalSection';
  261. procedure WinDoneCriticalSection(var cs : TRTLCriticalSection);
  262. {$ifdef wince}cdecl{$else}stdcall{$endif};external KernelDLL name 'DeleteCriticalSection';
  263. procedure WinEnterCriticalSection(var cs : TRTLCriticalSection);
  264. {$ifdef wince}cdecl{$else}stdcall{$endif};external KernelDLL name 'EnterCriticalSection';
  265. procedure WinLeaveCriticalSection(var cs : TRTLCriticalSection);
  266. {$ifdef wince}cdecl{$else}stdcall{$endif};external KernelDLL name 'LeaveCriticalSection';
  267. procedure SySInitCriticalSection(var cs);
  268. begin
  269. WinInitCriticalSection(PRTLCriticalSection(@cs)^);
  270. end;
  271. procedure SysDoneCriticalSection(var cs);
  272. begin
  273. WinDoneCriticalSection(PRTLCriticalSection(@cs)^);
  274. end;
  275. procedure SysEnterCriticalSection(var cs);
  276. begin
  277. WinEnterCriticalSection(PRTLCriticalSection(@cs)^);
  278. end;
  279. procedure SySLeaveCriticalSection(var cs);
  280. begin
  281. WinLeaveCriticalSection(PRTLCriticalSection(@cs)^);
  282. end;
  283. Const
  284. wrSignaled = 0;
  285. wrTimeout = 1;
  286. wrAbandoned= 2;
  287. wrError = 3;
  288. type Tbasiceventstate=record
  289. fhandle : THandle;
  290. flasterror : longint;
  291. end;
  292. plocaleventrec= ^tbasiceventstate;
  293. function intBasicEventCreate(EventAttributes : Pointer;
  294. AManualReset,InitialState : Boolean;const Name : ansistring):pEventState;
  295. begin
  296. new(plocaleventrec(result));
  297. plocaleventrec(result)^.FHandle := CreateEvent(EventAttributes, AManualReset, InitialState,PChar(Name));
  298. end;
  299. procedure intbasiceventdestroy(state:peventstate);
  300. begin
  301. closehandle(plocaleventrec(state)^.fhandle);
  302. dispose(plocaleventrec(state));
  303. end;
  304. procedure intbasiceventResetEvent(state:peventstate);
  305. begin
  306. ResetEvent(plocaleventrec(state)^.FHandle)
  307. end;
  308. procedure intbasiceventSetEvent(state:peventstate);
  309. begin
  310. SetEvent(plocaleventrec(state)^.FHandle);
  311. end;
  312. function intbasiceventWaitFor(Timeout : Cardinal;state:peventstate) : longint;
  313. begin
  314. case WaitForSingleObject(plocaleventrec(state)^.fHandle, Timeout) of
  315. WAIT_ABANDONED: Result := wrAbandoned;
  316. WAIT_OBJECT_0: Result := wrSignaled;
  317. WAIT_TIMEOUT: Result := wrTimeout;
  318. WAIT_FAILED:
  319. begin
  320. Result := wrError;
  321. plocaleventrec(state)^.FLastError := GetLastError;
  322. end;
  323. else
  324. Result := wrError;
  325. end;
  326. end;
  327. function intRTLEventCreate: PRTLEvent;
  328. begin
  329. Result := PRTLEVENT(CreateEvent(nil, false, false, nil));
  330. end;
  331. procedure intRTLEventDestroy(AEvent: PRTLEvent);
  332. begin
  333. CloseHandle(THANDLE(AEvent));
  334. end;
  335. procedure intRTLEventSetEvent(AEvent: PRTLEvent);
  336. begin
  337. SetEvent(THANDLE(AEvent));
  338. end;
  339. procedure intRTLEventResetEvent(AEvent: PRTLEvent);
  340. begin
  341. ResetEvent(THANDLE(AEvent));
  342. end;
  343. procedure intRTLEventWaitFor(AEvent: PRTLEvent);
  344. const
  345. INFINITE=dword(-1);
  346. begin
  347. WaitForSingleObject(THANDLE(AEvent), INFINITE);
  348. end;
  349. procedure intRTLEventWaitForTimeout(AEvent: PRTLEvent;timeout : longint);
  350. begin
  351. WaitForSingleObject(THANDLE(AEvent), timeout);
  352. end;
  353. Var
  354. WinThreadManager : TThreadManager;
  355. Procedure InitSystemThreads;
  356. begin
  357. With WinThreadManager do
  358. begin
  359. InitManager :=Nil;
  360. DoneManager :=Nil;
  361. BeginThread :=@SysBeginThread;
  362. EndThread :=@SysEndThread;
  363. SuspendThread :=@SysSuspendThread;
  364. ResumeThread :=@SysResumeThread;
  365. KillThread :=@SysKillThread;
  366. ThreadSwitch :=@SysThreadSwitch;
  367. WaitForThreadTerminate :=@SysWaitForThreadTerminate;
  368. ThreadSetPriority :=@SysThreadSetPriority;
  369. ThreadGetPriority :=@SysThreadGetPriority;
  370. GetCurrentThreadId :=@SysGetCurrentThreadId;
  371. InitCriticalSection :=@SysInitCriticalSection;
  372. DoneCriticalSection :=@SysDoneCriticalSection;
  373. EnterCriticalSection :=@SysEnterCriticalSection;
  374. LeaveCriticalSection :=@SysLeaveCriticalSection;
  375. InitThreadVar :=@SysInitThreadVar;
  376. RelocateThreadVar :=@SysRelocateThreadVar;
  377. AllocateThreadVars :=@SysAllocateThreadVars;
  378. ReleaseThreadVars :=@SysReleaseThreadVars;
  379. BasicEventCreate :=@intBasicEventCreate;
  380. BasicEventDestroy :=@intBasicEventDestroy;
  381. BasicEventResetEvent :=@intBasicEventResetEvent;
  382. BasicEventSetEvent :=@intBasicEventSetEvent;
  383. BasiceventWaitFor :=@intBasiceventWaitFor;
  384. RTLEventCreate :=@intRTLEventCreate;
  385. RTLEventDestroy :=@intRTLEventDestroy;
  386. RTLEventSetEvent :=@intRTLEventSetEvent;
  387. RTLEventResetEvent :=@intRTLEventResetEvent;
  388. RTLEventWaitFor :=@intRTLEventWaitFor;
  389. RTLEventWaitForTimeout :=@intRTLEventWaitForTimeout;
  390. end;
  391. SetThreadManager(WinThreadManager);
  392. ThreadID := GetCurrentThreadID;
  393. end;