systhrd.inc 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673
  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:SIZE_T):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 WinGetCurrentThread: THandle; stdcall; external KernelDLL name 'GetCurrentThread';
  48. function WinOpenThread(dwDesiredAccess: DWord; bInheritHandle: Boolean; dwThreadId: DWord): THandle; stdcall; external KernelDLL name 'OpenThread';
  49. function WinIsDebuggerPresent: Boolean; stdcall; external KernelDLL name 'IsDebuggerPresent';
  50. type
  51. TSetThreadDescription = function(threadHandle: THandle; lpThreadDescription: PWideChar): HResult; stdcall;
  52. var
  53. WinSetThreadDescription: TSetThreadDescription;
  54. function CreateEvent(lpEventAttributes:pointer;bManualReset:longbool;bInitialState:longbool;lpName:pchar): THandle; stdcall; external KernelDLL name 'CreateEventA';
  55. function ResetEvent(hEvent:THandle):LONGBOOL; stdcall; external KernelDLL name 'ResetEvent';
  56. function SetEvent(hEvent:THandle):LONGBOOL; stdcall; external KernelDLL name 'SetEvent';
  57. {$endif WINCE}
  58. procedure WinInitCriticalSection(var cs : TRTLCriticalSection);
  59. {$ifdef wince}cdecl{$else}stdcall{$endif};external KernelDLL name 'InitializeCriticalSection';
  60. procedure WinDoneCriticalSection(var cs : TRTLCriticalSection);
  61. {$ifdef wince}cdecl{$else}stdcall{$endif};external KernelDLL name 'DeleteCriticalSection';
  62. procedure WinEnterCriticalSection(var cs : TRTLCriticalSection);
  63. {$ifdef wince}cdecl{$else}stdcall{$endif};external KernelDLL name 'EnterCriticalSection';
  64. procedure WinLeaveCriticalSection(var cs : TRTLCriticalSection);
  65. {$ifdef wince}cdecl{$else}stdcall{$endif};external KernelDLL name 'LeaveCriticalSection';
  66. CONST
  67. WAIT_OBJECT_0 = 0;
  68. WAIT_ABANDONED_0 = $80;
  69. WAIT_TIMEOUT = $102;
  70. WAIT_IO_COMPLETION = $c0;
  71. WAIT_ABANDONED = $80;
  72. WAIT_FAILED = $ffffffff;
  73. {$ifndef SUPPORT_WIN95}
  74. function WinTryEnterCriticalSection(var cs : TRTLCriticalSection):longint;
  75. {$ifdef wince}cdecl{$else}stdcall{$endif};external KernelDLL name 'TryEnterCriticalSection';
  76. {$else SUPPORT_WIN95}
  77. type
  78. TTryEnterCriticalSection = function(var cs : TRTLCriticalSection):longint; stdcall;
  79. var
  80. WinTryEnterCriticalSection : TTryEnterCriticalSection;
  81. {$endif SUPPORT_WIN95}
  82. {*****************************************************************************
  83. Threadvar support
  84. *****************************************************************************}
  85. var
  86. // public names are used by heaptrc unit
  87. threadvarblocksize : dword; public name '_FPC_TlsSize';
  88. {$ifdef FPC_HAS_INDIRECT_ENTRY_INFORMATION}
  89. TLSKey : PDword = nil; public name '_FPC_TlsKey';
  90. {$else FPC_HAS_INDIRECT_ENTRY_INFORMATION}
  91. TLSKeyVar : DWord = $ffffffff;
  92. TLSKey : PDWord = @TLSKeyVar; public name '_FPC_TlsKey';
  93. {$endif FPC_HAS_INDIRECT_ENTRY_INFORMATION}
  94. var
  95. MainThreadIdWin32 : DWORD;
  96. procedure SysInitThreadvar(var offset : dword;size : dword);
  97. begin
  98. offset:=threadvarblocksize;
  99. {$ifdef CPUARM}
  100. // Data must be allocated at 4 bytes boundary for ARM
  101. size:=(size + 3) and not dword(3);
  102. {$endif CPUARM}
  103. inc(threadvarblocksize,size);
  104. end;
  105. procedure SysAllocateThreadVars; public name '_FPC_SysAllocateThreadVars';
  106. var
  107. dataindex : pointer;
  108. errorsave : dword;
  109. begin
  110. { we've to allocate the memory from system }
  111. { because the FPC heap management uses }
  112. { exceptions which use threadvars but }
  113. { these aren't allocated yet ... }
  114. { allocate room on the heap for the thread vars }
  115. errorsave:=GetLastError;
  116. if tlskey^=$ffffffff then
  117. RunError(226);
  118. dataindex:=TlsGetValue(tlskey^);
  119. if dataindex=nil then
  120. begin
  121. dataindex:=pointer(LocalAlloc(LMEM_FIXED or LMEM_ZEROINIT,threadvarblocksize));
  122. if dataindex=nil then
  123. RunError(226);
  124. TlsSetValue(tlskey^,dataindex);
  125. end;
  126. SetLastError(errorsave);
  127. end;
  128. function SysRelocateThreadvar(offset : dword) : pointer; forward;
  129. procedure SysInitTLS;
  130. begin
  131. { do not check IsMultiThread, as program could have altered it, out of Delphi habit }
  132. { the thread attach/detach code uses locks to avoid multiple calls of this }
  133. if TLSKey^=$ffffffff then
  134. begin
  135. { We're still running in single thread mode, setup the TLS }
  136. TLSKey^:=TlsAlloc;
  137. InitThreadVars(@SysRelocateThreadvar);
  138. end;
  139. end;
  140. procedure SysFiniMultithreading;
  141. begin
  142. if TLSKey^<>$ffffffff then
  143. TlsFree(TLSKey^);
  144. TLSKey^:=$ffffffff;
  145. end;
  146. function SysRelocateThreadvar(offset : dword) : pointer;
  147. var
  148. dataindex : pointer;
  149. errorsave : dword;
  150. begin
  151. errorsave:=GetLastError;
  152. dataindex:=TlsGetValue(tlskey^);
  153. if dataindex=nil then
  154. begin
  155. SysAllocateThreadVars;
  156. dataindex:=TlsGetValue(tlskey^);
  157. InitThread($1000000);
  158. end;
  159. SetLastError(errorsave);
  160. SysRelocateThreadvar:=DataIndex+Offset;
  161. end;
  162. procedure SysReleaseThreadVars;
  163. var
  164. p: pointer;
  165. begin
  166. if TLSKey^<>$ffffffff then
  167. begin
  168. p:=TlsGetValue(tlskey^);
  169. if Assigned(p) then
  170. LocalFree(p);
  171. TlsSetValue(tlskey^, nil);
  172. end;
  173. end;
  174. {*****************************************************************************
  175. Thread starting
  176. *****************************************************************************}
  177. type
  178. pthreadinfo = ^tthreadinfo;
  179. tthreadinfo = record
  180. f : tthreadfunc;
  181. p : pointer;
  182. stklen : ptruint;
  183. end;
  184. function ThreadMain(param : pointer) : Longint; {$ifdef wince}cdecl{$else}stdcall{$endif};
  185. var
  186. ti : tthreadinfo;
  187. begin
  188. { Copy parameter to local data }
  189. ti:=pthreadinfo(param)^;
  190. { Handle all possible threadvar models:
  191. - dynamic threadvars: initialized either in DllMain,
  192. or upon accessing the threadvar ThreadID;
  193. - static threadvars+TLS callback: initialized in TLS callback;
  194. - static threadvars, no callback: ThreadID remains 0 and
  195. initialization happens here. }
  196. if ThreadID=TThreadID(0) then
  197. InitThread(ti.stklen);
  198. dispose(pthreadinfo(param));
  199. { Start thread function }
  200. {$ifdef DEBUG_MT}
  201. writeln('Jumping to thread function of thread ',Win32GetCurrentThreadId);
  202. {$endif DEBUG_MT}
  203. {$if defined(FPC_USE_WIN64_SEH) or defined(FPC_USE_WIN32_SEH)}
  204. { use special 'top-level' exception handler around the thread function }
  205. ThreadMain:=main_wrapper(ti.p,pointer(ti.f));
  206. {$else FPC_USE_WIN64_SEH}
  207. ThreadMain:=ti.f(ti.p);
  208. {$endif FPC_USE_WIN64_SEH or FPC_USE_WIN32_SEH}
  209. end;
  210. function SysBeginThread(sa : Pointer;stacksize : ptruint;
  211. ThreadFunction : tthreadfunc;p : pointer;
  212. creationFlags : dword;var ThreadId : TThreadID) : TThreadID;
  213. var
  214. ti : pthreadinfo;
  215. _threadid : dword;
  216. begin
  217. {$ifdef DEBUG_MT}
  218. writeln('Creating new thread');
  219. {$endif DEBUG_MT}
  220. { Initialize multithreading if not done }
  221. SysInitTLS;
  222. if not IsMultiThread then
  223. begin
  224. { lazy initialize thread support }
  225. LazyInitThreading;
  226. IsMultiThread:=true;
  227. end;
  228. { the only way to pass data to the newly created thread
  229. in a MT safe way, is to use the heap }
  230. new(ti);
  231. ti^.f:=ThreadFunction;
  232. ti^.p:=p;
  233. ti^.stklen:=stacksize;
  234. {$ifdef DEBUG_MT}
  235. writeln('Starting new thread');
  236. {$endif DEBUG_MT}
  237. _threadid:=0;
  238. SysBeginThread:=CreateThread(sa,stacksize,@ThreadMain,ti,creationflags,_threadid);
  239. { creation failed? if yes, we dispose the parameter record }
  240. if SysBeginThread=0 then
  241. begin
  242. {$ifdef DEBUG_MT}
  243. writeln('Thread creation failed');
  244. {$endif DEBUG_MT}
  245. dispose(ti);
  246. end;
  247. ThreadID:=_threadid;
  248. end;
  249. procedure SysEndThread(ExitCode : DWord);
  250. begin
  251. DoneThread;
  252. ExitThread(ExitCode);
  253. end;
  254. procedure SysThreadSwitch;
  255. begin
  256. Sleep(0);
  257. end;
  258. function SysSuspendThread (threadHandle : TThreadID) : dword;
  259. begin
  260. SysSuspendThread:=WinSuspendThread(threadHandle);
  261. end;
  262. function SysResumeThread (threadHandle : TThreadID) : dword;
  263. begin
  264. SysResumeThread:=WinResumeThread(threadHandle);
  265. end;
  266. function SysKillThread (threadHandle : TThreadID) : dword;
  267. var exitCode : dword;
  268. begin
  269. if not TerminateThread (threadHandle, exitCode) then
  270. SysKillThread := GetLastError
  271. else
  272. SysKillThread := 0;
  273. end;
  274. function SysCloseThread (threadHandle : TThreadID) : dword;
  275. begin
  276. SysCloseThread:=winCloseHandle(threadHandle);
  277. end;
  278. function SysWaitForThreadTerminate (threadHandle : TThreadID; TimeoutMs : longint) : dword;
  279. begin
  280. if timeoutMs = 0 then dec (timeoutMs); // $ffffffff is INFINITE
  281. SysWaitForThreadTerminate := WaitForSingleObject(threadHandle, TimeoutMs);
  282. end;
  283. function SysThreadSetPriority (threadHandle : TThreadID; Prio: longint): boolean; {-15..+15, 0=normal}
  284. begin
  285. SysThreadSetPriority:=WinThreadSetPriority(threadHandle,Prio);
  286. end;
  287. function SysThreadGetPriority (threadHandle : TThreadID): longint;
  288. begin
  289. SysThreadGetPriority:=WinThreadGetPriority(threadHandle);
  290. end;
  291. function SysGetCurrentThreadId : TThreadID;
  292. begin
  293. SysGetCurrentThreadId:=Win32GetCurrentThreadId;
  294. end;
  295. {$ifndef WINCE}
  296. { following method is supported on older Windows versions AND currently only supported method by GDB }
  297. procedure RaiseMSVCExceptionMethod(threadHandle: TThreadID; const ThreadName: AnsiString);
  298. const
  299. MS_VC_EXCEPTION: DWord = $406D1388;
  300. type
  301. THREADNAME_INFO = record
  302. dwType: DWord; // Must be 0x1000.
  303. szName: PAnsiChar; // Pointer to name (in user addr space).
  304. dwThreadID: DWord; // Thread ID (-1=caller thread).
  305. dwFlags: DWord; // Reserved for future use, must be zero.
  306. end;
  307. var
  308. thrdinfo: THREADNAME_INFO;
  309. begin
  310. thrdinfo.dwType:=$1000;
  311. thrdinfo.szName:=@ThreadName[1];
  312. thrdinfo.dwThreadID:=threadHandle;
  313. thrdinfo.dwFlags:=0;
  314. try
  315. RaiseException(MS_VC_EXCEPTION, 0, SizeOf(thrdinfo) div SizeOf(DWord), @thrdinfo);
  316. except
  317. {do nothing}
  318. end;
  319. end;
  320. { following method needs at least Windows 10 version 1607 or Windows Server 2016 }
  321. procedure SetThreadDescriptionMethod(threadHandle: TThreadID; const ThreadName: UnicodeString);
  322. var
  323. thrdhandle: THandle;
  324. ClosingNeeded: Boolean;
  325. begin
  326. if threadHandle=TThreadID(-1) then
  327. begin
  328. thrdhandle:=WinGetCurrentThread;
  329. ClosingNeeded:=False;
  330. end
  331. else
  332. begin
  333. thrdhandle:=WinOpenThread($0400, False, threadHandle);
  334. ClosingNeeded:=True;
  335. end;
  336. WinSetThreadDescription(thrdhandle, @ThreadName[1]);
  337. if ClosingNeeded then
  338. begin
  339. CloseHandle(thrdhandle);
  340. end;
  341. end;
  342. {$endif WINCE}
  343. procedure SysSetThreadDebugNameA(threadHandle: TThreadID; const ThreadName: AnsiString);
  344. begin
  345. {$ifndef WINCE}
  346. if ThreadName = '' then
  347. Exit;
  348. if WinIsDebuggerPresent then
  349. begin
  350. RaiseMSVCExceptionMethod(threadHandle, ThreadName);
  351. end;
  352. if Assigned(WinSetThreadDescription) then
  353. begin
  354. SetThreadDescriptionMethod(threadHandle, UnicodeString(ThreadName));
  355. end;
  356. {$else WINCE}
  357. {$Warning SetThreadDebugNameA needs to be implemented}
  358. {$endif WINCE}
  359. end;
  360. procedure SysSetThreadDebugNameU(threadHandle: TThreadID; const ThreadName: UnicodeString);
  361. begin
  362. {$ifndef WINCE}
  363. if ThreadName = '' then
  364. Exit;
  365. if WinIsDebuggerPresent then
  366. begin
  367. RaiseMSVCExceptionMethod(threadHandle, AnsiString(ThreadName));
  368. end;
  369. if Assigned(WinSetThreadDescription) then
  370. begin
  371. SetThreadDescriptionMethod(threadHandle, ThreadName);
  372. end;
  373. {$else WINCE}
  374. {$Warning SetThreadDebugNameU needs to be implemented}
  375. {$endif WINCE}
  376. end;
  377. {*****************************************************************************
  378. Delphi/Win32 compatibility
  379. *****************************************************************************}
  380. procedure SySInitCriticalSection(var cs);
  381. begin
  382. WinInitCriticalSection(PRTLCriticalSection(@cs)^);
  383. end;
  384. procedure SysDoneCriticalSection(var cs);
  385. begin
  386. WinDoneCriticalSection(PRTLCriticalSection(@cs)^);
  387. end;
  388. procedure SysEnterCriticalSection(var cs);
  389. begin
  390. WinEnterCriticalSection(PRTLCriticalSection(@cs)^);
  391. end;
  392. {$ifdef SUPPORT_WIN95}
  393. function Win95TryEnterCriticalSection(var cs : TRTLCriticalSection):longint;stdcall;
  394. var
  395. MyThreadID : DWORD;
  396. begin
  397. MyThreadId:=GetCurrentThreadId();
  398. if InterlockedIncrement(cs.LockCount)=0 then
  399. begin
  400. cs.OwningThread:=MyThreadId;
  401. cs.RecursionCount:=1;
  402. result:=1;
  403. end
  404. else
  405. begin
  406. if cs.OwningThread=MyThreadId then
  407. begin
  408. InterlockedDecrement(cs.LockCount);
  409. InterlockedIncrement(cs.RecursionCount);
  410. result:=1;
  411. end
  412. else
  413. begin
  414. InterlockedDecrement(cs.LockCount);
  415. result:=0;
  416. end;
  417. end;
  418. end;
  419. {$endif SUPPORT_WIN95}
  420. function SysTryEnterCriticalSection(var cs):longint;
  421. begin
  422. result:=WinTryEnterCriticalSection(PRTLCriticalSection(@cs)^);
  423. end;
  424. procedure SySLeaveCriticalSection(var cs);
  425. begin
  426. WinLeaveCriticalSection(PRTLCriticalSection(@cs)^);
  427. end;
  428. Const
  429. wrSignaled = 0;
  430. wrTimeout = 1;
  431. wrAbandoned= 2;
  432. wrError = 3;
  433. type Tbasiceventstate=record
  434. fhandle : THandle;
  435. flasterror : longint;
  436. end;
  437. plocaleventrec= ^tbasiceventstate;
  438. function intBasicEventCreate(EventAttributes : Pointer;
  439. AManualReset,InitialState : Boolean;const Name : ansistring):pEventState;
  440. var
  441. n : PChar;
  442. begin
  443. new(plocaleventrec(result));
  444. if Length(Name) = 0 then
  445. n := Nil
  446. else
  447. n := PChar(Name);
  448. plocaleventrec(result)^.FHandle := CreateEvent(EventAttributes, AManualReset, InitialState,n);
  449. end;
  450. procedure intbasiceventdestroy(state:peventstate);
  451. begin
  452. closehandle(plocaleventrec(state)^.fhandle);
  453. dispose(plocaleventrec(state));
  454. end;
  455. procedure intbasiceventResetEvent(state:peventstate);
  456. begin
  457. ResetEvent(plocaleventrec(state)^.FHandle)
  458. end;
  459. procedure intbasiceventSetEvent(state:peventstate);
  460. begin
  461. SetEvent(plocaleventrec(state)^.FHandle);
  462. end;
  463. function intbasiceventWaitFor(Timeout : Cardinal;state:peventstate) : longint;
  464. begin
  465. case WaitForSingleObject(plocaleventrec(state)^.fHandle, Timeout) of
  466. WAIT_ABANDONED: Result := wrAbandoned;
  467. WAIT_OBJECT_0: Result := wrSignaled;
  468. WAIT_TIMEOUT: Result := wrTimeout;
  469. WAIT_FAILED:
  470. begin
  471. Result := wrError;
  472. plocaleventrec(state)^.FLastError := GetLastError;
  473. end;
  474. else
  475. Result := wrError;
  476. end;
  477. end;
  478. function intRTLEventCreate: PRTLEvent;
  479. begin
  480. Result := PRTLEVENT(CreateEvent(nil, false, false, nil));
  481. end;
  482. procedure intRTLEventDestroy(AEvent: PRTLEvent);
  483. begin
  484. CloseHandle(THANDLE(AEvent));
  485. end;
  486. procedure intRTLEventSetEvent(AEvent: PRTLEvent);
  487. begin
  488. SetEvent(THANDLE(AEvent));
  489. end;
  490. procedure intRTLEventResetEvent(AEvent: PRTLEvent);
  491. begin
  492. ResetEvent(THANDLE(AEvent));
  493. end;
  494. procedure intRTLEventWaitFor(AEvent: PRTLEvent);
  495. const
  496. INFINITE=dword(-1);
  497. begin
  498. WaitForSingleObject(THANDLE(AEvent), INFINITE);
  499. end;
  500. procedure intRTLEventWaitForTimeout(AEvent: PRTLEvent;timeout : longint);
  501. begin
  502. WaitForSingleObject(THANDLE(AEvent), timeout);
  503. end;
  504. Var
  505. WinThreadManager : TThreadManager;
  506. Procedure InitSystemThreads;public name '_FPC_InitSystemThreads';
  507. {$ifndef WINCE}
  508. var
  509. KernelHandle : THandle;
  510. {$endif}
  511. begin
  512. With WinThreadManager do
  513. begin
  514. InitManager :=Nil;
  515. DoneManager :=Nil;
  516. BeginThread :=@SysBeginThread;
  517. EndThread :=@SysEndThread;
  518. SuspendThread :=@SysSuspendThread;
  519. ResumeThread :=@SysResumeThread;
  520. KillThread :=@SysKillThread;
  521. ThreadSwitch :=@SysThreadSwitch;
  522. CloseThread :=@SysCloseThread;
  523. WaitForThreadTerminate :=@SysWaitForThreadTerminate;
  524. ThreadSetPriority :=@SysThreadSetPriority;
  525. ThreadGetPriority :=@SysThreadGetPriority;
  526. GetCurrentThreadId :=@SysGetCurrentThreadId;
  527. SetThreadDebugNameA :=@SysSetThreadDebugNameA;
  528. SetThreadDebugNameU :=@SysSetThreadDebugNameU;
  529. InitCriticalSection :=@SysInitCriticalSection;
  530. DoneCriticalSection :=@SysDoneCriticalSection;
  531. EnterCriticalSection :=@SysEnterCriticalSection;
  532. TryEnterCriticalSection:=@SysTryEnterCriticalSection;
  533. LeaveCriticalSection :=@SysLeaveCriticalSection;
  534. InitThreadVar :=@SysInitThreadVar;
  535. RelocateThreadVar :=@SysRelocateThreadVar;
  536. AllocateThreadVars :=@SysAllocateThreadVars;
  537. ReleaseThreadVars :=@SysReleaseThreadVars;
  538. BasicEventCreate :=@intBasicEventCreate;
  539. BasicEventDestroy :=@intBasicEventDestroy;
  540. BasicEventResetEvent :=@intBasicEventResetEvent;
  541. BasicEventSetEvent :=@intBasicEventSetEvent;
  542. BasiceventWaitFor :=@intBasiceventWaitFor;
  543. RTLEventCreate :=@intRTLEventCreate;
  544. RTLEventDestroy :=@intRTLEventDestroy;
  545. RTLEventSetEvent :=@intRTLEventSetEvent;
  546. RTLEventResetEvent :=@intRTLEventResetEvent;
  547. RTLEventWaitFor :=@intRTLEventWaitFor;
  548. RTLEventWaitForTimeout :=@intRTLEventWaitForTimeout;
  549. end;
  550. SetThreadManager(WinThreadManager);
  551. ThreadID := GetCurrentThreadID;
  552. {$ifndef FPC_USE_TLS_DIRECTORY}
  553. if IsLibrary then
  554. {$endif}
  555. SysInitTLS;
  556. {$ifndef WINCE}
  557. KernelHandle:=GetModuleHandle(KernelDLL);
  558. {$endif}
  559. {$IFDEF SUPPORT_WIN95}
  560. { Try to find TryEnterCriticalSection function }
  561. if KernelHandle<>0 then
  562. WinTryEnterCriticalSection:=TTryEnterCriticalSection(WinGetProcAddress(KernelHandle,'TryEnterCriticalSection'));
  563. if not assigned(WinTryEnterCriticalSection) then
  564. WinTryEnterCriticalSection:=@Win95TryEnterCriticalSection;
  565. {$ENDIF SUPPORT_WIN95}
  566. {$ifndef WINCE}
  567. if KernelHandle<>0 then
  568. begin
  569. WinSetThreadDescription:=TSetThreadDescription(WinGetProcAddress(KernelHandle, 'SetThreadDescription'));
  570. end;
  571. {$endif WINCE}
  572. end;