systhrd.inc 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755
  1. {
  2. This file is part of the Free Pascal run time library.
  3. Copyright (c) 2002-2011 by Tomas Hajny,
  4. member of the Free Pascal development team.
  5. OS/2 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 Api imports
  14. *****************************************************************************}
  15. const
  16. pag_Read = 1;
  17. pag_Write = 2;
  18. pag_Execute = 4;
  19. pag_Guard = 8;
  20. pag_Commit = $10;
  21. obj_Tile = $40;
  22. sem_Indefinite_Wait = cardinal (-1);
  23. dtSuspended = 1;
  24. dtStack_Commited = 2;
  25. deThread = 0; {DosExit - exit thread only}
  26. dcWW_Wait = 0;
  27. dcWW_NoWait = 1;
  28. dpThread = 2;
  29. dpSameClass = 0;
  30. dce_AutoReset = $1000;
  31. qs_End = 0;
  32. qs_Process = 1;
  33. qs_Thread = 256;
  34. type
  35. PQSTRec = ^TQSTRec;
  36. TQSTRec = record
  37. RecType: cardinal; { Record type }
  38. TID: word; { Thread ID }
  39. Slot: word; { "Unique" thread slot number }
  40. SleepID: cardinal; { Sleep ID thread is sleeping on }
  41. case boolean of
  42. false: (
  43. Priority: cardinal; { Thread priority (class + level) }
  44. SysTime: cardinal; { Thread system time }
  45. UserTime: cardinal; { Thread user time }
  46. State: byte; { Thread state }
  47. Pad: array [1..3] of byte); { Padding for 32-bit alignment }
  48. true: (
  49. PrioLevel: byte; { Thread priority level only }
  50. PrioClass: byte; { Thread priority class only }
  51. Pad2: array [1..14] of byte);
  52. end;
  53. PQSPRec = ^TQSPRec;
  54. TQSPrec = record
  55. RecType: cardinal; { Type of record being processed }
  56. PThrdRec: PQSTRec; { (Far?) pointer to thread records for this process }
  57. PID: word; { Process ID }
  58. PPID: word; { Parent process ID }
  59. ProcType: cardinal; { Process type }
  60. Stat: cardinal; { Process status }
  61. SGID: cardinal; { Process screen group }
  62. hMte: word; { Program module handle for process }
  63. cTCB: word; { Number of TCBs (Thread Control Blocks) in use }
  64. c32PSem: cardinal; { Number of private 32-bit semaphores in use }
  65. p32SemRec: pointer; { (Far?) pointer to head of 32-bit semaphores info }
  66. c16Sem: word; { Number of 16 bit system semaphores in use }
  67. cLib: word; { Number of runtime linked libraries }
  68. cShrMem: word; { Number of shared memory handles }
  69. cFH: word; { Number of open files }
  70. { NOTE: cFH is size of active part of }
  71. { the handle table if QS_FILE specified }
  72. p16SemRec: word; { Far pointer? to head of 16-bit semaphores info }
  73. pLibRec: word; { Far pointer? to list of runtime libraries }
  74. pShrMemRec: word; { Far pointer? to list of shared memory handles }
  75. pFSRec: word; { Far pointer to list of file handles; }
  76. { 0xFFFF means it's closed, otherwise }
  77. { it's an SFN if non-zero }
  78. end;
  79. (* Simplified version here to avoid need for all record types copied here. *)
  80. PQSPtrRec = ^TQSPtrRec;
  81. TQSPtrRec = record
  82. PGlobalRec: pointer;
  83. PProcRec: PQSPRec; { Pointer to head of process records }
  84. P16SemRec: pointer;
  85. P32SemRec: pointer;
  86. PMemRec: pointer;
  87. PLibRec: pointer;
  88. PShrMemRec: pointer;
  89. PFSRec: pointer;
  90. end;
  91. { import the necessary stuff from the OS }
  92. function DosAllocThreadLocalMemory (Count: cardinal; var P: pointer): cardinal;
  93. cdecl; external 'DOSCALLS' index 454;
  94. function DosFreeThreadLocalMemory (P: pointer): cardinal; cdecl;
  95. external 'DOSCALLS' index 455;
  96. function DosCreateThread (var TID: cardinal; Address: pointer;
  97. (* TThreadFunc *)
  98. aParam: pointer; Flags: cardinal; StackSize: cardinal): cardinal; cdecl;
  99. external 'DOSCALLS' index 311;
  100. function DosCreateMutExSem (Name: PChar; var Handle: THandle; Attr: cardinal;
  101. State: cardinal): cardinal; cdecl; external 'DOSCALLS' index 331;
  102. function DosCloseMutExSem (Handle: THandle): cardinal; cdecl;
  103. external 'DOSCALLS' index 333;
  104. function DosRequestMutExSem (Handle: THandle; Timeout: cardinal): cardinal;
  105. cdecl; external 'DOSCALLS' index 334;
  106. function DosReleaseMutExSem (Handle: THandle): cardinal; cdecl;
  107. external 'DOSCALLS' index 335;
  108. function DosSuspendThread (TID:cardinal): cardinal; cdecl;
  109. external 'DOSCALLS' index 238;
  110. function DosResumeThread (TID: cardinal): cardinal; cdecl;
  111. external 'DOSCALLS' index 237;
  112. function DosKillThread (TID: cardinal): cardinal; cdecl;
  113. external 'DOSCALLS' index 111;
  114. function DosWaitThread (var TID: cardinal; Option: cardinal): cardinal; cdecl;
  115. external 'DOSCALLS' index 349;
  116. procedure DosSleep (MSec: cardinal); cdecl; external 'DOSCALLS' index 229;
  117. {
  118. procedure DosExit (Action, Result: cardinal); cdecl;
  119. external 'DOSCALLS' index 234;
  120. Already declared in the main part of system.pas...
  121. }
  122. function DosSetPriority (Scope, TrClass: cardinal; Delta: longint;
  123. PortID: cardinal): cardinal; cdecl;
  124. external 'DOSCALLS' index 236;
  125. function DosCreateEventSem (Name: PChar; var Handle: THandle;
  126. Attr: cardinal; State: cardinal): cardinal; cdecl;
  127. external 'DOSCALLS' index 324;
  128. function DosCloseEventSem (Handle: THandle): cardinal; cdecl;
  129. external 'DOSCALLS' index 326;
  130. function DosResetEventSem (Handle: THandle; var PostCount: cardinal): cardinal;
  131. cdecl; external 'DOSCALLS' index 327;
  132. function DosPostEventSem (Handle: THandle): cardinal; cdecl;
  133. external 'DOSCALLS' index 328;
  134. function DosWaitEventSem (Handle: THandle; Timeout: cardinal): cardinal; cdecl;
  135. external 'DOSCALLS' index 329;
  136. function DosQueryEventSem (Handle: THandle; var Posted: cardinal): cardinal;
  137. cdecl; external 'DOSCALLS' index 330;
  138. function DosQuerySysState (EntityList, EntityLevel, PID, TID: cardinal;
  139. var Buffer; BufLen: cardinal): cardinal; cdecl;
  140. external 'DOSCALLS' index 368;
  141. {*****************************************************************************
  142. Threadvar support
  143. *****************************************************************************}
  144. const
  145. ThreadVarBlockSize: dword = 0;
  146. const
  147. (* Pointer to an allocated dword space within the local thread *)
  148. (* memory area. Pointer to the real memory block allocated for *)
  149. (* thread vars in this block is then stored in this dword. *)
  150. DataIndex: PPointer = nil;
  151. procedure SysInitThreadvar (var Offset: dword; Size: dword);
  152. begin
  153. Offset := ThreadVarBlockSize;
  154. Inc (ThreadVarBlockSize, Size);
  155. end;
  156. procedure SysAllocateThreadVars;
  157. begin
  158. { we've to allocate the memory from the OS }
  159. { because the FPC heap management uses }
  160. { exceptions which use threadvars but }
  161. { these aren't allocated yet ... }
  162. { allocate room on the heap for the thread vars }
  163. if DosAllocMem (DataIndex^, ThreadVarBlockSize, pag_Read or pag_Write
  164. or pag_Commit) <> 0 then
  165. HandleError (8);
  166. { The Windows API apparently provides a way to fill the allocated memory with }
  167. { zeros; we probably need to do it ourselves for compatibility. }
  168. FillChar (DataIndex^^, 0, ThreadVarBlockSize);
  169. end;
  170. function SysRelocateThreadVar (Offset: dword): pointer;
  171. begin
  172. { DataIndex itself not checked for not being nil - expected that this should }
  173. { not be necessary because the equivalent check (i.e. TlsKey not being set) }
  174. { is note performed by the Windows implementation. }
  175. if DataIndex^ = nil then
  176. begin
  177. SysAllocateThreadVars;
  178. InitThread ($1000000);
  179. end;
  180. SysRelocateThreadVar := DataIndex^ + Offset;
  181. end;
  182. procedure SysInitMultithreading;
  183. begin
  184. { do not check IsMultiThread, as program could have altered it, out of Delphi habit }
  185. { the thread attach/detach code uses locks to avoid multiple calls of this }
  186. if DataIndex = nil then
  187. begin
  188. { We're still running in single thread mode, setup the TLS }
  189. if DosAllocThreadLocalMemory (1, DataIndex) <> 0 then RunError (8);
  190. InitThreadVars (@SysRelocateThreadvar);
  191. IsMultiThread := true;
  192. end;
  193. end;
  194. procedure SysFiniMultithreading;
  195. begin
  196. if IsMultiThread then
  197. begin
  198. if DosFreeThreadLocalMemory (DataIndex) <> 0 then
  199. begin
  200. {??? What to do if releasing fails?}
  201. end;
  202. DataIndex := nil;
  203. end;
  204. end;
  205. procedure SysReleaseThreadVars;
  206. begin
  207. DosFreeMem (DataIndex^);
  208. DataIndex^ := nil;
  209. end;
  210. (* procedure InitThreadVars;
  211. begin
  212. { allocate one ThreadVar entry from the OS, we use this entry }
  213. { for a pointer to our threadvars }
  214. if DosAllocThreadLocalMemory (1, DataIndex) <> 0 then HandleError (8);
  215. { initialize threadvars }
  216. init_all_unit_threadvars;
  217. { allocate mem for main thread threadvars }
  218. SysAllocateThreadVars;
  219. { copy main thread threadvars }
  220. copy_all_unit_threadvars;
  221. { install threadvar handler }
  222. fpc_threadvar_relocate_proc := @SysRelocateThreadvar;
  223. end;
  224. *)
  225. {*****************************************************************************
  226. Thread starting
  227. *****************************************************************************}
  228. type
  229. pthreadinfo = ^tthreadinfo;
  230. tthreadinfo = record
  231. f : tthreadfunc;
  232. p : pointer;
  233. stklen : cardinal;
  234. end;
  235. (* procedure InitThread(stklen:cardinal);
  236. begin
  237. SysResetFPU;
  238. SysInitFPU;
  239. { ExceptAddrStack and ExceptObjectStack are threadvars }
  240. { so every thread has its on exception handling capabilities }
  241. SysInitExceptions;
  242. { Open all stdio fds again }
  243. SysInitStdio;
  244. InOutRes:=0;
  245. // ErrNo:=0;
  246. { Stack checking }
  247. StackLength:=stklen;
  248. StackBottom:=Sptr - StackLength;
  249. end;
  250. *)
  251. function ThreadMain(param : pointer) : pointer;cdecl;
  252. var
  253. ti : tthreadinfo;
  254. begin
  255. { Allocate local thread vars, this must be the first thing,
  256. because the exception management and io depends on threadvars }
  257. SysAllocateThreadVars;
  258. { Copy parameter to local data }
  259. {$ifdef DEBUG_MT}
  260. writeln('New thread started, initialising ...');
  261. {$endif DEBUG_MT}
  262. ti:=pthreadinfo(param)^;
  263. dispose(pthreadinfo(param));
  264. { Initialize thread }
  265. InitThread(ti.stklen);
  266. { Start thread function }
  267. {$ifdef DEBUG_MT}
  268. writeln('Jumping to thread function');
  269. {$endif DEBUG_MT}
  270. ThreadMain:=pointer(ti.f(ti.p));
  271. end;
  272. function SysBeginThread (SA: pointer; StackSize : PtrUInt;
  273. ThreadFunction: TThreadFunc; P: pointer;
  274. CreationFlags: cardinal; var ThreadId: TThreadID): DWord;
  275. var
  276. TI: PThreadInfo;
  277. begin
  278. { WriteLn is not a good idea before thread initialization...
  279. $ifdef DEBUG_MT
  280. WriteLn ('Creating new thread');
  281. $endif DEBUG_MT}
  282. { Initialize multithreading if not done }
  283. SysInitMultithreading;
  284. { the only way to pass data to the newly created thread
  285. in a MT safe way, is to use the heap }
  286. New (TI);
  287. TI^.F := ThreadFunction;
  288. TI^.P := P;
  289. TI^.StkLen := StackSize;
  290. ThreadID := 0;
  291. {$ifdef DEBUG_MT}
  292. WriteLn ('Starting new thread');
  293. {$endif DEBUG_MT}
  294. if DosCreateThread (cardinal (ThreadID), @ThreadMain, TI,
  295. CreationFlags, StackSize) = 0 then
  296. SysBeginThread := ThreadID
  297. else
  298. begin
  299. SysBeginThread := 0;
  300. {$IFDEF DEBUG_MT}
  301. WriteLn ('Thread creation failed');
  302. {$ENDIF DEBUG_MT}
  303. Dispose (TI);
  304. end;
  305. end;
  306. procedure SysEndThread (ExitCode: cardinal);
  307. begin
  308. DoneThread;
  309. DosExit (0, ExitCode);
  310. end;
  311. procedure SysThreadSwitch;
  312. begin
  313. DosSleep (0);
  314. end;
  315. function SysSuspendThread (ThreadHandle: dword): dword;
  316. begin
  317. {$WARNING Check expected return value}
  318. SysSuspendThread := DosSuspendThread (ThreadHandle);
  319. end;
  320. function SysResumeThread (ThreadHandle: dword): dword;
  321. begin
  322. {$WARNING Check expected return value}
  323. SysResumeThread := DosResumeThread (ThreadHandle);
  324. end;
  325. function SysKillThread (ThreadHandle: dword): dword;
  326. begin
  327. SysKillThread := DosKillThread (ThreadHandle);
  328. end;
  329. function SysCloseThread (ThreadHandle: TThreadID): dword;
  330. begin
  331. { Probably not relevant under OS/2? }
  332. // SysCloseThread:=CloseHandle(threadHandle);
  333. end;
  334. function SysWaitForThreadTerminate (ThreadHandle: dword;
  335. TimeoutMs: longint): dword;
  336. var
  337. RC: cardinal;
  338. const
  339. { Wait at most 100 ms before next check for thread termination }
  340. WaitTime = 100;
  341. begin
  342. if TimeoutMs = 0 then
  343. RC := DosWaitThread (ThreadHandle, dcWW_Wait)
  344. else
  345. repeat
  346. RC := DosWaitThread (ThreadHandle, dcWW_NoWait);
  347. if RC = 294 then
  348. begin
  349. if TimeoutMs > WaitTime then
  350. DosSleep (WaitTime)
  351. else
  352. begin
  353. DosSleep (TimeoutMs);
  354. DosWaitThread (ThreadHandle, dcWW_NoWait);
  355. end;
  356. Dec (TimeoutMs, WaitTime);
  357. end;
  358. until (RC <> 294) or (TimeoutMs <= 0);
  359. SysWaitForThreadTerminate := RC;
  360. end;
  361. function GetOS2ThreadPriority (ThreadHandle: dword): cardinal;
  362. const
  363. BufSize = 32768; (* Sufficient space for > 1000 threads (for one process!) *)
  364. var
  365. PPtrRec: PQSPtrRec;
  366. PTRec: PQSTRec;
  367. BufEnd: PtrUInt;
  368. RC: cardinal;
  369. begin
  370. GetOS2ThreadPriority := cardinal (-1);
  371. GetMem (PPtrRec, BufSize);
  372. if PPtrRec = nil then
  373. begin
  374. FreeMem (PPtrRec, BufSize);
  375. FPC_ThreadError;
  376. end
  377. else
  378. begin
  379. RC := DosQuerySysState (qs_Process, 0, ProcessID, 0, PPtrRec^, BufSize);
  380. if (RC = 0) and (PPtrRec^.PProcRec <> nil)
  381. and (PPtrRec^.PProcRec^.PThrdRec <> nil) then
  382. begin
  383. BufEnd := PtrUInt (PPtrRec) + BufSize;
  384. PTRec := PPtrRec^.PProcRec^.PThrdRec;
  385. while (PTRec^.RecType = qs_Thread) and (PTRec^.TID <> ThreadHandle) and
  386. (PtrUInt (PTRec) + SizeOf (PTRec^) < BufEnd) do
  387. Inc (PTRec);
  388. if (PTRec^.RecType = qs_Thread) and (PTRec^.TID = ThreadHandle) then
  389. GetOS2ThreadPriority := PTRec^.Priority;
  390. end;
  391. FreeMem (PPtrRec, BufSize);
  392. end;
  393. end;
  394. type
  395. TPrio = packed record
  396. PrioLevel: byte;
  397. PrioClass: byte;
  398. Padding: word;
  399. end;
  400. function SysThreadSetPriority (ThreadHandle: dword; Prio: longint): boolean;
  401. {-15..+15, 0=normal}
  402. var
  403. Delta: longint;
  404. Priority: cardinal;
  405. begin
  406. Priority := GetOS2ThreadPriority (ThreadHandle);
  407. if Priority > High (word) then
  408. SysThreadSetPriority := false
  409. else
  410. begin
  411. Delta := Prio * 2;
  412. if Delta + TPrio (Priority).PrioLevel < 0 then
  413. Delta := - TPrio (Priority).PrioLevel
  414. else if Delta + TPrio (Priority).PrioLevel > 31 then
  415. Delta := 31 - TPrio (Priority).PrioLevel;
  416. SysThreadSetPriority :=
  417. DosSetPriority (dpThread, dpSameClass, Delta, ThreadHandle) = 0;
  418. end;
  419. end;
  420. function SysThreadGetPriority (ThreadHandle: dword): longint;
  421. var
  422. Priority: cardinal;
  423. begin
  424. Priority := GetOS2ThreadPriority (ThreadHandle);
  425. (*
  426. Windows priority levels follow a fairly strange logic; let's mimic at least
  427. the part related to the idle priority returning negative numbers.
  428. Result range (based on Windows behaviour) is -15..+15.
  429. *)
  430. if TPrio (Priority).PrioClass = 1 then
  431. SysThreadGetPriority := TPrio (Priority).PrioLevel div 2 - 15
  432. else
  433. SysThreadGetPriority := TPrio (Priority).PrioLevel div 2;
  434. end;
  435. function SysGetCurrentThreadID: dword;
  436. var
  437. TIB: PThreadInfoBlock;
  438. begin
  439. DosGetInfoBlocks (@TIB, nil);
  440. SysGetCurrentThreadID := TIB^.TIB2^.TID;
  441. end;
  442. {*****************************************************************************
  443. Delphi/Win32 compatibility
  444. *****************************************************************************}
  445. procedure SysInitCriticalSection (var CS);
  446. begin
  447. if DosCreateMutExSem (nil, THandle (CS), 0, 0) <> 0 then
  448. FPC_ThreadError;
  449. end;
  450. procedure SysDoneCriticalSection (var CS);
  451. begin
  452. (* Trying to release first since this might apparently be the expected *)
  453. (* behaviour in Delphi according to comment in the Unix implementation. *)
  454. repeat
  455. until DosReleaseMutExSem (THandle (CS)) <> 0;
  456. if DosCloseMutExSem (THandle (CS)) <> 0 then
  457. FPC_ThreadError;
  458. end;
  459. procedure SysEnterCriticalSection (var CS);
  460. begin
  461. if DosRequestMutExSem (THandle (CS), cardinal (-1)) <> 0 then
  462. FPC_ThreadError;
  463. end;
  464. function SysTryEnterCriticalSection (var CS): longint;
  465. begin
  466. if DosRequestMutExSem (THandle (CS), 0) = 0 then
  467. Result := 1
  468. else
  469. Result := 0;
  470. end;
  471. procedure SysLeaveCriticalSection (var CS);
  472. begin
  473. if DosReleaseMutExSem (THandle (CS)) <> 0 then
  474. FPC_ThreadError;
  475. end;
  476. type
  477. TBasicEventState = record
  478. FHandle: THandle;
  479. FLastError: longint;
  480. end;
  481. PLocalEventRec = ^TBasicEventState;
  482. const
  483. wrSignaled = 0;
  484. wrTimeout = 1;
  485. wrAbandoned = 2; (* This cannot happen for an event semaphore with OS/2? *)
  486. wrError = 3;
  487. Error_Timeout = 640;
  488. OS2SemNamePrefix = '\SEM32\';
  489. function SysBasicEventCreate (EventAttributes: Pointer;
  490. AManualReset, InitialState: boolean; const Name: ansistring): PEventState;
  491. var
  492. RC: cardinal;
  493. Name2: ansistring;
  494. Attr: cardinal;
  495. begin
  496. New (PLocalEventRec (Result));
  497. if (Name <> '') and (UpCase (Copy (Name, 1, 7)) <> OS2SemNamePrefix) then
  498. Name2 := OS2SemNamePrefix + Name
  499. else
  500. Name2 := Name;
  501. if AManualReset then
  502. Attr := 0
  503. else
  504. Attr := DCE_AutoReset;
  505. if Name2 = '' then
  506. RC := DosCreateEventSem (nil, PLocalEventRec (Result)^.FHandle,
  507. Attr, cardinal (InitialState))
  508. else
  509. RC := DosCreateEventSem (PChar (Name2), PLocalEventRec (Result)^.FHandle,
  510. Attr, cardinal (InitialState));
  511. if RC <> 0 then
  512. begin
  513. Dispose (PLocalEventRec (Result));
  514. FPC_ThreadError;
  515. end;
  516. end;
  517. procedure SysBasicEventDestroy (State: PEventState);
  518. begin
  519. if State = nil then
  520. FPC_ThreadError
  521. else
  522. begin
  523. DosCloseEventSem (PLocalEventRec (State)^.FHandle);
  524. Dispose (PLocalEventRec (State));
  525. end;
  526. end;
  527. procedure SysBasicEventResetEvent (State: PEventState);
  528. var
  529. PostCount: cardinal;
  530. begin
  531. if State = nil then
  532. FPC_ThreadError
  533. else
  534. (* In case of later addition of error checking: *)
  535. (* RC 300 = Error_Already_Reset which would be OK. *)
  536. DosResetEventSem (PLocalEventRec (State)^.FHandle, PostCount);
  537. end;
  538. procedure SysBasicEventSetEvent (State: PEventState);
  539. begin
  540. if State = nil then
  541. FPC_ThreadError
  542. else
  543. DosPostEventSem (PLocalEventRec (State)^.FHandle);
  544. end;
  545. function SysBasicEventWaitFor (Timeout: Cardinal; State: PEventState): longint;
  546. var
  547. RC: cardinal;
  548. begin
  549. if State = nil then
  550. FPC_ThreadError
  551. else
  552. begin
  553. RC := DosWaitEventSem (PLocalEventRec (State)^.FHandle, Timeout);
  554. case RC of
  555. 0: Result := wrSignaled;
  556. Error_Timeout: Result := wrTimeout;
  557. else
  558. begin
  559. Result := wrError;
  560. PLocalEventRec (State)^.FLastError := RC;
  561. end;
  562. end;
  563. end;
  564. end;
  565. function SysRTLEventCreate: PRTLEvent;
  566. begin
  567. Result := PRTLEvent (-1);
  568. DosCreateEventSem (nil, THandle (Result), dce_AutoReset, 0);
  569. end;
  570. procedure SysRTLEventDestroy (AEvent: PRTLEvent);
  571. begin
  572. DosCloseEventSem (THandle (AEvent));
  573. end;
  574. procedure SysRTLEventSetEvent (AEvent: PRTLEvent);
  575. begin
  576. DosPostEventSem (THandle (AEvent));
  577. end;
  578. procedure SysRTLEventWaitFor (AEvent: PRTLEvent);
  579. begin
  580. DosWaitEventSem (THandle (AEvent), cardinal (-1));
  581. end;
  582. procedure SysRTLEventWaitForTimeout (AEvent: PRTLEvent; Timeout: longint);
  583. begin
  584. DosWaitEventSem (THandle (AEvent), Timeout);
  585. end;
  586. procedure SysRTLEventResetEvent (AEvent: PRTLEvent);
  587. var
  588. PostCount: cardinal;
  589. begin
  590. DosResetEventSem (THandle (AEvent), PostCount);
  591. end;
  592. var
  593. OS2ThreadManager: TThreadManager;
  594. procedure InitSystemThreads;
  595. begin
  596. with OS2ThreadManager do
  597. begin
  598. InitManager :=Nil;
  599. DoneManager :=Nil;
  600. BeginThread :=@SysBeginThread;
  601. EndThread :=@SysEndThread;
  602. SuspendThread :=@SysSuspendThread;
  603. ResumeThread :=@SysResumeThread;
  604. KillThread :=@SysKillThread;
  605. CloseThread :=@SysCloseThread;
  606. ThreadSwitch :=@SysThreadSwitch;
  607. WaitForThreadTerminate :=@SysWaitForThreadTerminate;
  608. ThreadSetPriority :=@SysThreadSetPriority;
  609. ThreadGetPriority :=@SysThreadGetPriority;
  610. GetCurrentThreadId :=@SysGetCurrentThreadId;
  611. InitCriticalSection :=@SysInitCriticalSection;
  612. DoneCriticalSection :=@SysDoneCriticalSection;
  613. EnterCriticalSection :=@SysEnterCriticalSection;
  614. TryEnterCriticalSection:=@SysTryEnterCriticalSection;
  615. LeaveCriticalSection :=@SysLeaveCriticalSection;
  616. InitThreadVar :=@SysInitThreadVar;
  617. RelocateThreadVar :=@SysRelocateThreadVar;
  618. AllocateThreadVars :=@SysAllocateThreadVars;
  619. ReleaseThreadVars :=@SysReleaseThreadVars;
  620. BasicEventCreate :=@SysBasicEventCreate;
  621. BasicEventDestroy :=@SysBasicEventDestroy;
  622. BasicEventSetEvent :=@SysBasicEventSetEvent;
  623. BasicEventResetEvent :=@SysBasicEventResetEvent;
  624. BasiceventWaitFor :=@SysBasiceventWaitFor;
  625. RTLEventCreate :=@SysRTLEventCreate;
  626. RTLEventDestroy :=@SysRTLEventDestroy;
  627. RTLEventSetEvent :=@SysRTLEventSetEvent;
  628. RTLEventResetEvent :=@SysRTLEventResetEvent;
  629. RTLEventWaitFor :=@SysRTLEventWaitFor;
  630. RTLEventWaitForTimeout :=@SysRTLEventWaitForTimeout;
  631. end;
  632. SetThreadManager (OS2ThreadManager);
  633. end;