systhrd.inc 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695
  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:PAnsiChar): 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. // shouldn't this be a msgwait in case the thread creates "Windows" See comment in waitforsingle?
  281. if timeoutMs = 0 then dec (timeoutMs); // $ffffffff is INFINITE
  282. // does waiting on thread require cowait too ?
  283. SysWaitForThreadTerminate := WaitForSingleObject(threadHandle, TimeoutMs);
  284. end;
  285. function SysThreadSetPriority (threadHandle : TThreadID; Prio: longint): boolean; {-15..+15, 0=normal}
  286. begin
  287. SysThreadSetPriority:=WinThreadSetPriority(threadHandle,Prio);
  288. end;
  289. function SysThreadGetPriority (threadHandle : TThreadID): longint;
  290. begin
  291. SysThreadGetPriority:=WinThreadGetPriority(threadHandle);
  292. end;
  293. function SysGetCurrentThreadId : TThreadID;
  294. begin
  295. SysGetCurrentThreadId:=Win32GetCurrentThreadId;
  296. end;
  297. {$ifndef WINCE}
  298. { following method is supported on older Windows versions AND currently only supported method by GDB }
  299. procedure RaiseMSVCExceptionMethod(threadHandle: TThreadID; const ThreadName: AnsiString);
  300. const
  301. MS_VC_EXCEPTION: DWord = $406D1388;
  302. type
  303. THREADNAME_INFO = record
  304. dwType: DWord; // Must be 0x1000.
  305. szName: PAnsiChar; // Pointer to name (in user addr space).
  306. dwThreadID: DWord; // Thread ID (-1=caller thread).
  307. dwFlags: DWord; // Reserved for future use, must be zero.
  308. end;
  309. var
  310. thrdinfo: THREADNAME_INFO;
  311. begin
  312. thrdinfo.dwType:=$1000;
  313. thrdinfo.szName:=@ThreadName[1];
  314. thrdinfo.dwThreadID:=threadHandle;
  315. thrdinfo.dwFlags:=0;
  316. try
  317. RaiseException(MS_VC_EXCEPTION, 0, SizeOf(thrdinfo) div SizeOf(DWord), @thrdinfo);
  318. except
  319. {do nothing}
  320. end;
  321. end;
  322. { following method needs at least Windows 10 version 1607 or Windows Server 2016 }
  323. procedure SetThreadDescriptionMethod(threadHandle: TThreadID; const ThreadName: UnicodeString);
  324. var
  325. thrdhandle: THandle;
  326. ClosingNeeded: Boolean;
  327. begin
  328. if threadHandle=TThreadID(-1) then
  329. begin
  330. thrdhandle:=WinGetCurrentThread;
  331. ClosingNeeded:=False;
  332. end
  333. else
  334. begin
  335. thrdhandle:=WinOpenThread($0400, False, threadHandle);
  336. ClosingNeeded:=True;
  337. end;
  338. WinSetThreadDescription(thrdhandle, @ThreadName[1]);
  339. if ClosingNeeded then
  340. begin
  341. CloseHandle(thrdhandle);
  342. end;
  343. end;
  344. {$endif WINCE}
  345. procedure SysSetThreadDebugNameA(threadHandle: TThreadID; const ThreadName: AnsiString);
  346. begin
  347. {$ifndef WINCE}
  348. if ThreadName = '' then
  349. Exit;
  350. if WinIsDebuggerPresent then
  351. begin
  352. RaiseMSVCExceptionMethod(threadHandle, ThreadName);
  353. end;
  354. if Assigned(WinSetThreadDescription) then
  355. begin
  356. SetThreadDescriptionMethod(threadHandle, UnicodeString(ThreadName));
  357. end;
  358. {$else WINCE}
  359. {$Warning SetThreadDebugNameA needs to be implemented}
  360. {$endif WINCE}
  361. end;
  362. procedure SysSetThreadDebugNameU(threadHandle: TThreadID; const ThreadName: UnicodeString);
  363. begin
  364. {$ifndef WINCE}
  365. if ThreadName = '' then
  366. Exit;
  367. if WinIsDebuggerPresent then
  368. begin
  369. RaiseMSVCExceptionMethod(threadHandle, AnsiString(ThreadName));
  370. end;
  371. if Assigned(WinSetThreadDescription) then
  372. begin
  373. SetThreadDescriptionMethod(threadHandle, ThreadName);
  374. end;
  375. {$else WINCE}
  376. {$Warning SetThreadDebugNameU needs to be implemented}
  377. {$endif WINCE}
  378. end;
  379. {*****************************************************************************
  380. Delphi/Win32 compatibility
  381. *****************************************************************************}
  382. procedure SySInitCriticalSection(var cs);
  383. begin
  384. WinInitCriticalSection(PRTLCriticalSection(@cs)^);
  385. end;
  386. procedure SysDoneCriticalSection(var cs);
  387. begin
  388. WinDoneCriticalSection(PRTLCriticalSection(@cs)^);
  389. end;
  390. procedure SysEnterCriticalSection(var cs);
  391. begin
  392. WinEnterCriticalSection(PRTLCriticalSection(@cs)^);
  393. end;
  394. {$ifdef SUPPORT_WIN95}
  395. function Win95TryEnterCriticalSection(var cs : TRTLCriticalSection):longint;stdcall;
  396. var
  397. MyThreadID : DWORD;
  398. begin
  399. MyThreadId:=GetCurrentThreadId();
  400. if InterlockedIncrement(cs.LockCount)=0 then
  401. begin
  402. cs.OwningThread:=MyThreadId;
  403. cs.RecursionCount:=1;
  404. result:=1;
  405. end
  406. else
  407. begin
  408. if cs.OwningThread=MyThreadId then
  409. begin
  410. InterlockedDecrement(cs.LockCount);
  411. InterlockedIncrement(cs.RecursionCount);
  412. result:=1;
  413. end
  414. else
  415. begin
  416. InterlockedDecrement(cs.LockCount);
  417. result:=0;
  418. end;
  419. end;
  420. end;
  421. {$endif SUPPORT_WIN95}
  422. function SysTryEnterCriticalSection(var cs):longint;
  423. begin
  424. result:=WinTryEnterCriticalSection(PRTLCriticalSection(@cs)^);
  425. end;
  426. procedure SySLeaveCriticalSection(var cs);
  427. begin
  428. WinLeaveCriticalSection(PRTLCriticalSection(@cs)^);
  429. end;
  430. Const
  431. wrSignaled = 0;
  432. wrTimeout = 1;
  433. wrAbandoned= 2;
  434. wrError = 3;
  435. function intBasicEventCreate(EventAttributes : Pointer;
  436. AManualReset,InitialState : Boolean;const Name : ansistring):pEventState;
  437. var
  438. n : PAnsiChar;
  439. begin
  440. if Length(Name) = 0 then
  441. n := Nil
  442. else
  443. n := PAnsiChar(Name);
  444. Result := PEventState(CreateEvent(EventAttributes, AManualReset, InitialState,n));
  445. end;
  446. procedure intbasiceventdestroy(state:peventstate);
  447. begin
  448. closehandle(THandle(state));
  449. end;
  450. procedure intbasiceventResetEvent(state:peventstate);
  451. begin
  452. ResetEvent(THandle(state))
  453. end;
  454. procedure intbasiceventSetEvent(state:peventstate);
  455. begin
  456. SetEvent(THandle(state));
  457. end;
  458. function FirstCoWaitForMultipleHandles(dwFlags, dwTimeout: DWORD; cHandles: uint32; pHandles: PWOHandleArray; out lpdwindex: DWORD): HRESULT; stdcall; forward;
  459. var
  460. Ole32Dll: THandle = 0; { Unloaded at win32 & win64 system_exit. }
  461. CoWaitForMultipleHandlesImpl: function(dwFlags, dwTimeout: DWORD; cHandles: uint32; pHandles: PWOHandleArray; out lpdwindex: DWORD): HRESULT; stdcall;
  462. = @FirstCoWaitForMultipleHandles;
  463. function FirstCoWaitForMultipleHandles(dwFlags, dwTimeout: DWORD; cHandles: uint32; pHandles: PWOHandleArray; out lpdwindex: DWORD): HRESULT; stdcall;
  464. var
  465. LocalOle32Dll: THandle;
  466. begin
  467. if Ole32Dll = 0 then
  468. begin
  469. LocalOle32Dll := WinLoadLibraryW('ole32.dll');
  470. if InterlockedCompareExchange(Pointer(Ole32Dll), Pointer(LocalOle32Dll), nil) <> nil then
  471. WinFreeLibrary(LocalOle32Dll);
  472. end;
  473. CodePointer(CoWaitForMultipleHandlesImpl) := WinGetProcAddress(Ole32Dll, 'CoWaitForMultipleHandles');
  474. Result := CoWaitForMultipleHandlesImpl(dwFlags, dwTimeout, cHandles, pHandles, lpdwindex);
  475. end;
  476. function CoWaitForMultipleHandles(dwFlags, dwTimeout: DWORD; cHandles: uint32; pHandles: PWOHandleArray; out lpdwindex: DWORD): HRESULT;
  477. begin
  478. Result := CoWaitForMultipleHandlesImpl(dwFlags, dwTimeout, cHandles, pHandles, lpdwindex);
  479. end;
  480. function intbasiceventWaitFor(Timeout : Cardinal;state:peventstate;UseCOMWait: Boolean = False) : longint;
  481. const COWAIT_DEFAULT = 0;
  482. RPC_S_CALLPENDING = HRESULT($80010115);
  483. var SignaledIndex : DWORD;
  484. begin
  485. if UseComWait Then
  486. case CoWaitForMultipleHandles(COWAIT_DEFAULT, Timeout, 1, PWOHandleArray(@state), SignaledIndex) of
  487. S_OK: Result := wrSignaled;
  488. RPC_S_CALLPENDING: Result := wrTimeout;
  489. else Result := wrError;
  490. end
  491. else
  492. case WaitForSingleObject(THandle(state), Timeout) of
  493. WAIT_OBJECT_0: Result := wrSignaled;
  494. WAIT_TIMEOUT: Result := wrTimeout;
  495. else result := wrError; { WAIT_FAILED or any other value. Note that only mutex waits can return WAIT_ABANDONED. }
  496. end;
  497. end;
  498. function intRTLEventCreate: PRTLEvent;
  499. begin
  500. Result := PRTLEVENT(CreateEvent(nil, false, false, nil));
  501. end;
  502. procedure intRTLEventDestroy(AEvent: PRTLEvent);
  503. begin
  504. CloseHandle(THANDLE(AEvent));
  505. end;
  506. procedure intRTLEventSetEvent(AEvent: PRTLEvent);
  507. begin
  508. SetEvent(THANDLE(AEvent));
  509. end;
  510. procedure intRTLEventResetEvent(AEvent: PRTLEvent);
  511. begin
  512. ResetEvent(THANDLE(AEvent));
  513. end;
  514. procedure intRTLEventWaitFor(AEvent: PRTLEvent);
  515. const
  516. INFINITE=dword(-1);
  517. begin
  518. WaitForSingleObject(THANDLE(AEvent), INFINITE);
  519. end;
  520. procedure intRTLEventWaitForTimeout(AEvent: PRTLEvent;timeout : longint);
  521. begin
  522. WaitForSingleObject(THANDLE(AEvent), timeout);
  523. end;
  524. Var
  525. WinThreadManager : TThreadManager;
  526. Procedure InitSystemThreads;public name '_FPC_InitSystemThreads';
  527. {$ifndef WINCE}
  528. var
  529. KernelHandle : THandle;
  530. {$endif}
  531. begin
  532. With WinThreadManager do
  533. begin
  534. InitManager :=Nil;
  535. DoneManager :=Nil;
  536. BeginThread :=@SysBeginThread;
  537. EndThread :=@SysEndThread;
  538. SuspendThread :=@SysSuspendThread;
  539. ResumeThread :=@SysResumeThread;
  540. KillThread :=@SysKillThread;
  541. ThreadSwitch :=@SysThreadSwitch;
  542. CloseThread :=@SysCloseThread;
  543. WaitForThreadTerminate :=@SysWaitForThreadTerminate;
  544. ThreadSetPriority :=@SysThreadSetPriority;
  545. ThreadGetPriority :=@SysThreadGetPriority;
  546. GetCurrentThreadId :=@SysGetCurrentThreadId;
  547. SetThreadDebugNameA :=@SysSetThreadDebugNameA;
  548. SetThreadDebugNameU :=@SysSetThreadDebugNameU;
  549. InitCriticalSection :=@SysInitCriticalSection;
  550. DoneCriticalSection :=@SysDoneCriticalSection;
  551. EnterCriticalSection :=@SysEnterCriticalSection;
  552. TryEnterCriticalSection:=@SysTryEnterCriticalSection;
  553. LeaveCriticalSection :=@SysLeaveCriticalSection;
  554. InitThreadVar :=@SysInitThreadVar;
  555. RelocateThreadVar :=@SysRelocateThreadVar;
  556. AllocateThreadVars :=@SysAllocateThreadVars;
  557. ReleaseThreadVars :=@SysReleaseThreadVars;
  558. BasicEventCreate :=@intBasicEventCreate;
  559. BasicEventDestroy :=@intBasicEventDestroy;
  560. BasicEventResetEvent :=@intBasicEventResetEvent;
  561. BasicEventSetEvent :=@intBasicEventSetEvent;
  562. BasiceventWaitFor :=@intBasiceventWaitFor;
  563. RTLEventCreate :=@intRTLEventCreate;
  564. RTLEventDestroy :=@intRTLEventDestroy;
  565. RTLEventSetEvent :=@intRTLEventSetEvent;
  566. RTLEventResetEvent :=@intRTLEventResetEvent;
  567. RTLEventWaitFor :=@intRTLEventWaitFor;
  568. RTLEventWaitForTimeout :=@intRTLEventWaitForTimeout;
  569. end;
  570. SetThreadManager(WinThreadManager);
  571. ThreadID := GetCurrentThreadID;
  572. {$ifndef FPC_USE_TLS_DIRECTORY}
  573. if IsLibrary then
  574. {$endif}
  575. SysInitTLS;
  576. {$ifndef WINCE}
  577. KernelHandle:=GetModuleHandle(KernelDLL);
  578. {$endif}
  579. {$IFDEF SUPPORT_WIN95}
  580. { Try to find TryEnterCriticalSection function }
  581. if KernelHandle<>0 then
  582. WinTryEnterCriticalSection:=TTryEnterCriticalSection(WinGetProcAddress(KernelHandle,'TryEnterCriticalSection'));
  583. if not assigned(WinTryEnterCriticalSection) then
  584. WinTryEnterCriticalSection:=@Win95TryEnterCriticalSection;
  585. {$ENDIF SUPPORT_WIN95}
  586. {$ifndef WINCE}
  587. if KernelHandle<>0 then
  588. begin
  589. WinSetThreadDescription:=TSetThreadDescription(WinGetProcAddress(KernelHandle, 'SetThreadDescription'));
  590. end;
  591. {$endif WINCE}
  592. end;