systhrd.inc 16 KB

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