systhrd.inc 17 KB

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