systhrd.inc 23 KB

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