systhrd.inc 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772
  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. function DosQuerySysInfo (First, Last: cardinal; var Buf; BufSize: cardinal):
  142. cardinal; cdecl;
  143. external 'DOSCALLS' index 348;
  144. {*****************************************************************************
  145. Threadvar support
  146. *****************************************************************************}
  147. const
  148. ThreadVarBlockSize: dword = 0;
  149. const
  150. (* Pointer to an allocated dword space within the local thread *)
  151. (* memory area. Pointer to the real memory block allocated for *)
  152. (* thread vars in this block is then stored in this dword. *)
  153. DataIndex: PPointer = nil;
  154. procedure SysInitThreadvar (var Offset: dword; Size: dword);
  155. begin
  156. Offset := ThreadVarBlockSize;
  157. Inc (ThreadVarBlockSize, Size);
  158. end;
  159. procedure SysAllocateThreadVars;
  160. begin
  161. { we've to allocate the memory from the OS }
  162. { because the FPC heap management uses }
  163. { exceptions which use threadvars but }
  164. { these aren't allocated yet ... }
  165. { allocate room on the heap for the thread vars }
  166. if DosAllocMem (DataIndex^, ThreadVarBlockSize, pag_Read or pag_Write
  167. or pag_Commit) <> 0 then
  168. HandleError (8);
  169. { The Windows API apparently provides a way to fill the allocated memory with }
  170. { zeros; we probably need to do it ourselves for compatibility. }
  171. FillChar (DataIndex^^, 0, ThreadVarBlockSize);
  172. end;
  173. function SysRelocateThreadVar (Offset: dword): pointer;
  174. begin
  175. { DataIndex itself not checked for not being nil - expected that this should }
  176. { not be necessary because the equivalent check (i.e. TlsKey not being set) }
  177. { is note performed by the Windows implementation. }
  178. if DataIndex^ = nil then
  179. begin
  180. SysAllocateThreadVars;
  181. InitThread ($1000000);
  182. end;
  183. SysRelocateThreadVar := DataIndex^ + Offset;
  184. end;
  185. procedure SysInitMultithreading;
  186. begin
  187. { do not check IsMultiThread, as program could have altered it, out of Delphi habit }
  188. { the thread attach/detach code uses locks to avoid multiple calls of this }
  189. if DataIndex = nil then
  190. begin
  191. { We're still running in single thread mode, setup the TLS }
  192. if DosAllocThreadLocalMemory (1, DataIndex) <> 0 then RunError (8);
  193. InitThreadVars (@SysRelocateThreadvar);
  194. IsMultiThread := true;
  195. end;
  196. end;
  197. procedure SysFiniMultithreading;
  198. begin
  199. if IsMultiThread then
  200. begin
  201. if DosFreeThreadLocalMemory (DataIndex) <> 0 then
  202. begin
  203. {??? What to do if releasing fails?}
  204. end;
  205. DataIndex := nil;
  206. end;
  207. end;
  208. procedure SysReleaseThreadVars;
  209. begin
  210. DosFreeMem (DataIndex^);
  211. DataIndex^ := nil;
  212. end;
  213. (* procedure InitThreadVars;
  214. begin
  215. { allocate one ThreadVar entry from the OS, we use this entry }
  216. { for a pointer to our threadvars }
  217. if DosAllocThreadLocalMemory (1, DataIndex) <> 0 then HandleError (8);
  218. { initialize threadvars }
  219. init_all_unit_threadvars;
  220. { allocate mem for main thread threadvars }
  221. SysAllocateThreadVars;
  222. { copy main thread threadvars }
  223. copy_all_unit_threadvars;
  224. { install threadvar handler }
  225. fpc_threadvar_relocate_proc := @SysRelocateThreadvar;
  226. end;
  227. *)
  228. {*****************************************************************************
  229. Thread starting
  230. *****************************************************************************}
  231. type
  232. pthreadinfo = ^tthreadinfo;
  233. tthreadinfo = record
  234. f : tthreadfunc;
  235. p : pointer;
  236. stklen : cardinal;
  237. end;
  238. (* procedure InitThread(stklen:cardinal);
  239. begin
  240. SysResetFPU;
  241. SysInitFPU;
  242. { ExceptAddrStack and ExceptObjectStack are threadvars }
  243. { so every thread has its on exception handling capabilities }
  244. SysInitExceptions;
  245. { Open all stdio fds again }
  246. SysInitStdio;
  247. InOutRes:=0;
  248. // ErrNo:=0;
  249. { Stack checking }
  250. StackLength:=stklen;
  251. StackBottom:=Sptr - StackLength;
  252. end;
  253. *)
  254. function ThreadMain(param : pointer) : pointer;cdecl;
  255. var
  256. ti : tthreadinfo;
  257. begin
  258. { Allocate local thread vars, this must be the first thing,
  259. because the exception management and io depends on threadvars }
  260. SysAllocateThreadVars;
  261. { Copy parameter to local data }
  262. {$ifdef DEBUG_MT}
  263. writeln('New thread started, initialising ...');
  264. {$endif DEBUG_MT}
  265. ti:=pthreadinfo(param)^;
  266. dispose(pthreadinfo(param));
  267. { Initialize thread }
  268. InitThread(ti.stklen);
  269. { Start thread function }
  270. {$ifdef DEBUG_MT}
  271. writeln('Jumping to thread function');
  272. {$endif DEBUG_MT}
  273. ThreadMain:=pointer(ti.f(ti.p));
  274. end;
  275. function SysBeginThread (SA: pointer; StackSize : PtrUInt;
  276. ThreadFunction: TThreadFunc; P: pointer;
  277. CreationFlags: cardinal; var ThreadId: TThreadID): DWord;
  278. var
  279. TI: PThreadInfo;
  280. begin
  281. { WriteLn is not a good idea before thread initialization...
  282. $ifdef DEBUG_MT
  283. WriteLn ('Creating new thread');
  284. $endif DEBUG_MT}
  285. { Initialize multithreading if not done }
  286. SysInitMultithreading;
  287. { the only way to pass data to the newly created thread
  288. in a MT safe way, is to use the heap }
  289. New (TI);
  290. TI^.F := ThreadFunction;
  291. TI^.P := P;
  292. TI^.StkLen := StackSize;
  293. ThreadID := 0;
  294. {$ifdef DEBUG_MT}
  295. WriteLn ('Starting new thread');
  296. {$endif DEBUG_MT}
  297. if DosCreateThread (cardinal (ThreadID), @ThreadMain, TI,
  298. CreationFlags, StackSize) = 0 then
  299. SysBeginThread := ThreadID
  300. else
  301. begin
  302. SysBeginThread := 0;
  303. {$IFDEF DEBUG_MT}
  304. WriteLn ('Thread creation failed');
  305. {$ENDIF DEBUG_MT}
  306. Dispose (TI);
  307. end;
  308. end;
  309. procedure SysEndThread (ExitCode: cardinal);
  310. begin
  311. DoneThread;
  312. DosExit (0, ExitCode);
  313. end;
  314. procedure SysThreadSwitch;
  315. begin
  316. DosSleep (0);
  317. end;
  318. function SysSuspendThread (ThreadHandle: dword): dword;
  319. begin
  320. {$WARNING Check expected return value}
  321. SysSuspendThread := DosSuspendThread (ThreadHandle);
  322. end;
  323. function SysResumeThread (ThreadHandle: dword): dword;
  324. begin
  325. {$WARNING Check expected return value}
  326. SysResumeThread := DosResumeThread (ThreadHandle);
  327. end;
  328. function SysKillThread (ThreadHandle: dword): dword;
  329. begin
  330. SysKillThread := DosKillThread (ThreadHandle);
  331. end;
  332. function SysCloseThread (ThreadHandle: TThreadID): dword;
  333. begin
  334. { Probably not relevant under OS/2? }
  335. // SysCloseThread:=CloseHandle(threadHandle);
  336. end;
  337. function SysWaitForThreadTerminate (ThreadHandle: dword;
  338. TimeoutMs: longint): dword;
  339. var
  340. RC: cardinal;
  341. const
  342. { Wait at most 100 ms before next check for thread termination }
  343. WaitTime = 100;
  344. begin
  345. if TimeoutMs = 0 then
  346. RC := DosWaitThread (ThreadHandle, dcWW_Wait)
  347. else
  348. repeat
  349. RC := DosWaitThread (ThreadHandle, dcWW_NoWait);
  350. if RC = 294 then
  351. begin
  352. if TimeoutMs > WaitTime then
  353. DosSleep (WaitTime)
  354. else
  355. begin
  356. DosSleep (TimeoutMs);
  357. DosWaitThread (ThreadHandle, dcWW_NoWait);
  358. end;
  359. Dec (TimeoutMs, WaitTime);
  360. end;
  361. until (RC <> 294) or (TimeoutMs <= 0);
  362. SysWaitForThreadTerminate := RC;
  363. end;
  364. function GetOS2ThreadPriority (ThreadHandle: dword): cardinal;
  365. const
  366. BufSize = 32768; (* Sufficient space for > 1000 threads (for one process!) *)
  367. var
  368. PPtrRec: PQSPtrRec;
  369. PTRec: PQSTRec;
  370. BufEnd: PtrUInt;
  371. RC: cardinal;
  372. begin
  373. GetOS2ThreadPriority := cardinal (-1);
  374. GetMem (PPtrRec, BufSize);
  375. if PPtrRec = nil then
  376. begin
  377. FreeMem (PPtrRec, BufSize);
  378. FPC_ThreadError;
  379. end
  380. else
  381. begin
  382. RC := DosQuerySysState (qs_Process, 0, ProcessID, 0, PPtrRec^, BufSize);
  383. if (RC = 0) and (PPtrRec^.PProcRec <> nil)
  384. and (PPtrRec^.PProcRec^.PThrdRec <> nil) then
  385. begin
  386. BufEnd := PtrUInt (PPtrRec) + BufSize;
  387. PTRec := PPtrRec^.PProcRec^.PThrdRec;
  388. while (PTRec^.RecType = qs_Thread) and (PTRec^.TID <> ThreadHandle) and
  389. (PtrUInt (PTRec) + SizeOf (PTRec^) < BufEnd) do
  390. Inc (PTRec);
  391. if (PTRec^.RecType = qs_Thread) and (PTRec^.TID = ThreadHandle) then
  392. GetOS2ThreadPriority := PTRec^.Priority;
  393. end;
  394. FreeMem (PPtrRec, BufSize);
  395. end;
  396. end;
  397. type
  398. TPrio = packed record
  399. PrioLevel: byte;
  400. PrioClass: byte;
  401. Padding: word;
  402. end;
  403. function SysThreadSetPriority (ThreadHandle: dword; Prio: longint): boolean;
  404. {-15..+15, 0=normal}
  405. var
  406. Delta: longint;
  407. Priority: cardinal;
  408. begin
  409. Priority := GetOS2ThreadPriority (ThreadHandle);
  410. if Priority > High (word) then
  411. SysThreadSetPriority := false
  412. else
  413. begin
  414. Delta := Prio * 2;
  415. if Delta + TPrio (Priority).PrioLevel < 0 then
  416. Delta := - TPrio (Priority).PrioLevel
  417. else if Delta + TPrio (Priority).PrioLevel > 31 then
  418. Delta := 31 - TPrio (Priority).PrioLevel;
  419. SysThreadSetPriority :=
  420. DosSetPriority (dpThread, dpSameClass, Delta, ThreadHandle) = 0;
  421. end;
  422. end;
  423. function SysThreadGetPriority (ThreadHandle: dword): longint;
  424. var
  425. Priority: cardinal;
  426. begin
  427. Priority := GetOS2ThreadPriority (ThreadHandle);
  428. (*
  429. Windows priority levels follow a fairly strange logic; let's mimic at least
  430. the part related to the idle priority returning negative numbers.
  431. Result range (based on Windows behaviour) is -15..+15.
  432. *)
  433. if TPrio (Priority).PrioClass = 1 then
  434. SysThreadGetPriority := TPrio (Priority).PrioLevel div 2 - 15
  435. else
  436. SysThreadGetPriority := TPrio (Priority).PrioLevel div 2;
  437. end;
  438. function SysGetCurrentThreadID: dword;
  439. var
  440. TIB: PThreadInfoBlock;
  441. begin
  442. DosGetInfoBlocks (@TIB, nil);
  443. SysGetCurrentThreadID := TIB^.TIB2^.TID;
  444. end;
  445. {*****************************************************************************
  446. Delphi/Win32 compatibility
  447. *****************************************************************************}
  448. procedure SysInitCriticalSection (var CS);
  449. begin
  450. if DosCreateMutExSem (nil, THandle (CS), 0, 0) <> 0 then
  451. FPC_ThreadError;
  452. end;
  453. procedure SysDoneCriticalSection (var CS);
  454. begin
  455. (* Trying to release first since this might apparently be the expected *)
  456. (* behaviour in Delphi according to comment in the Unix implementation. *)
  457. repeat
  458. until DosReleaseMutExSem (THandle (CS)) <> 0;
  459. if DosCloseMutExSem (THandle (CS)) <> 0 then
  460. FPC_ThreadError;
  461. end;
  462. procedure SysEnterCriticalSection (var CS);
  463. begin
  464. if DosRequestMutExSem (THandle (CS), cardinal (-1)) <> 0 then
  465. FPC_ThreadError;
  466. end;
  467. function SysTryEnterCriticalSection (var CS): longint;
  468. begin
  469. if DosRequestMutExSem (THandle (CS), 0) = 0 then
  470. Result := 1
  471. else
  472. Result := 0;
  473. end;
  474. procedure SysLeaveCriticalSection (var CS);
  475. begin
  476. if DosReleaseMutExSem (THandle (CS)) <> 0 then
  477. FPC_ThreadError;
  478. end;
  479. type
  480. TBasicEventState = record
  481. FHandle: THandle;
  482. FLastError: longint;
  483. end;
  484. PLocalEventRec = ^TBasicEventState;
  485. const
  486. wrSignaled = 0;
  487. wrTimeout = 1;
  488. wrAbandoned = 2; (* This cannot happen for an event semaphore with OS/2? *)
  489. wrError = 3;
  490. Error_Timeout = 640;
  491. OS2SemNamePrefix = '\SEM32\';
  492. function SysBasicEventCreate (EventAttributes: Pointer;
  493. AManualReset, InitialState: boolean; const Name: ansistring): PEventState;
  494. var
  495. RC: cardinal;
  496. Name2: ansistring;
  497. Attr: cardinal;
  498. begin
  499. New (PLocalEventRec (Result));
  500. if (Name <> '') and (UpCase (Copy (Name, 1, 7)) <> OS2SemNamePrefix) then
  501. Name2 := OS2SemNamePrefix + Name
  502. else
  503. Name2 := Name;
  504. if AManualReset then
  505. Attr := 0
  506. else
  507. Attr := DCE_AutoReset;
  508. if Name2 = '' then
  509. RC := DosCreateEventSem (nil, PLocalEventRec (Result)^.FHandle,
  510. Attr, cardinal (InitialState))
  511. else
  512. RC := DosCreateEventSem (PChar (Name2), PLocalEventRec (Result)^.FHandle,
  513. Attr, cardinal (InitialState));
  514. if RC <> 0 then
  515. begin
  516. Dispose (PLocalEventRec (Result));
  517. FPC_ThreadError;
  518. end;
  519. end;
  520. procedure SysBasicEventDestroy (State: PEventState);
  521. begin
  522. if State = nil then
  523. FPC_ThreadError
  524. else
  525. begin
  526. DosCloseEventSem (PLocalEventRec (State)^.FHandle);
  527. Dispose (PLocalEventRec (State));
  528. end;
  529. end;
  530. procedure SysBasicEventResetEvent (State: PEventState);
  531. var
  532. PostCount: cardinal;
  533. begin
  534. if State = nil then
  535. FPC_ThreadError
  536. else
  537. (* In case of later addition of error checking: *)
  538. (* RC 300 = Error_Already_Reset which would be OK. *)
  539. DosResetEventSem (PLocalEventRec (State)^.FHandle, PostCount);
  540. end;
  541. procedure SysBasicEventSetEvent (State: PEventState);
  542. begin
  543. if State = nil then
  544. FPC_ThreadError
  545. else
  546. DosPostEventSem (PLocalEventRec (State)^.FHandle);
  547. end;
  548. function SysBasicEventWaitFor (Timeout: Cardinal; State: PEventState): longint;
  549. var
  550. RC: cardinal;
  551. begin
  552. if State = nil then
  553. FPC_ThreadError
  554. else
  555. begin
  556. RC := DosWaitEventSem (PLocalEventRec (State)^.FHandle, Timeout);
  557. case RC of
  558. 0: Result := wrSignaled;
  559. Error_Timeout: Result := wrTimeout;
  560. else
  561. begin
  562. Result := wrError;
  563. PLocalEventRec (State)^.FLastError := RC;
  564. end;
  565. end;
  566. end;
  567. end;
  568. function SysRTLEventCreate: PRTLEvent;
  569. begin
  570. Result := PRTLEvent (-1);
  571. DosCreateEventSem (nil, THandle (Result), dce_AutoReset, 0);
  572. end;
  573. procedure SysRTLEventDestroy (AEvent: PRTLEvent);
  574. begin
  575. DosCloseEventSem (THandle (AEvent));
  576. end;
  577. procedure SysRTLEventSetEvent (AEvent: PRTLEvent);
  578. begin
  579. DosPostEventSem (THandle (AEvent));
  580. end;
  581. procedure SysRTLEventWaitFor (AEvent: PRTLEvent);
  582. begin
  583. DosWaitEventSem (THandle (AEvent), cardinal (-1));
  584. end;
  585. procedure SysRTLEventWaitForTimeout (AEvent: PRTLEvent; Timeout: longint);
  586. begin
  587. DosWaitEventSem (THandle (AEvent), Timeout);
  588. end;
  589. procedure SysRTLEventResetEvent (AEvent: PRTLEvent);
  590. var
  591. PostCount: cardinal;
  592. begin
  593. DosResetEventSem (THandle (AEvent), PostCount);
  594. end;
  595. {$DEFINE HAS_GETCPUCOUNT}
  596. function GetCPUCount: LongWord;
  597. const
  598. svNumProcessors = 26;
  599. var
  600. ProcNum: cardinal;
  601. begin
  602. GetCPUCount := 1;
  603. if DosQuerySysInfo (svNumProcessors, svNumProcessors, ProcNum,
  604. SizeOf (ProcNum)) = 0 then
  605. GetCPUCount := ProcNum;
  606. end;
  607. var
  608. OS2ThreadManager: TThreadManager;
  609. procedure InitSystemThreads;
  610. begin
  611. with OS2ThreadManager do
  612. begin
  613. InitManager :=Nil;
  614. DoneManager :=Nil;
  615. BeginThread :=@SysBeginThread;
  616. EndThread :=@SysEndThread;
  617. SuspendThread :=@SysSuspendThread;
  618. ResumeThread :=@SysResumeThread;
  619. KillThread :=@SysKillThread;
  620. CloseThread :=@SysCloseThread;
  621. ThreadSwitch :=@SysThreadSwitch;
  622. WaitForThreadTerminate :=@SysWaitForThreadTerminate;
  623. ThreadSetPriority :=@SysThreadSetPriority;
  624. ThreadGetPriority :=@SysThreadGetPriority;
  625. GetCurrentThreadId :=@SysGetCurrentThreadId;
  626. InitCriticalSection :=@SysInitCriticalSection;
  627. DoneCriticalSection :=@SysDoneCriticalSection;
  628. EnterCriticalSection :=@SysEnterCriticalSection;
  629. TryEnterCriticalSection:=@SysTryEnterCriticalSection;
  630. LeaveCriticalSection :=@SysLeaveCriticalSection;
  631. InitThreadVar :=@SysInitThreadVar;
  632. RelocateThreadVar :=@SysRelocateThreadVar;
  633. AllocateThreadVars :=@SysAllocateThreadVars;
  634. ReleaseThreadVars :=@SysReleaseThreadVars;
  635. BasicEventCreate :=@SysBasicEventCreate;
  636. BasicEventDestroy :=@SysBasicEventDestroy;
  637. BasicEventSetEvent :=@SysBasicEventSetEvent;
  638. BasicEventResetEvent :=@SysBasicEventResetEvent;
  639. BasiceventWaitFor :=@SysBasiceventWaitFor;
  640. RTLEventCreate :=@SysRTLEventCreate;
  641. RTLEventDestroy :=@SysRTLEventDestroy;
  642. RTLEventSetEvent :=@SysRTLEventSetEvent;
  643. RTLEventResetEvent :=@SysRTLEventResetEvent;
  644. RTLEventWaitFor :=@SysRTLEventWaitFor;
  645. RTLEventWaitForTimeout :=@SysRTLEventWaitForTimeout;
  646. end;
  647. SetThreadManager (OS2ThreadManager);
  648. end;