systhrd.inc 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507
  1. {
  2. This file is part of the Free Pascal run time library.
  3. Copyright (c) 2002-5 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 = -1;
  23. dtSuspended = 1;
  24. dtStack_Commited = 2;
  25. { import the necessary stuff from the OS }
  26. function DosAllocThreadLocalMemory (Count: cardinal; var P: pointer): cardinal;
  27. cdecl; external 'DOSCALLS' index 454;
  28. function DosFreeThreadLocalMemory (P: pointer): cardinal; cdecl;
  29. external 'DOSCALLS' index 455;
  30. function DosCreateThread (var TID: cardinal; Address: pointer;
  31. (* TThreadFunc *)
  32. aParam: pointer; Flags: cardinal; StackSize: cardinal): cardinal; cdecl;
  33. external 'DOSCALLS' index 311;
  34. function DosCreateMutExSem (Name: PChar; var Handle: longint; Attr: cardinal;
  35. State: boolean): cardinal; cdecl; external 'DOSCALLS' index 331;
  36. function DosCloseMutExSem (Handle: longint): cardinal; cdecl;
  37. external 'DOSCALLS' index 333;
  38. function DosQueryMutExSem (Handle: longint; var PID, TID, Count: cardinal):
  39. cardinal; cdecl; external 'DOSCALLS' index 336;
  40. function DosRequestMutExSem (Handle:longint; Timeout: cardinal): cardinal; cdecl;
  41. external 'DOSCALLS' index 334;
  42. function DosReleaseMutExSem (Handle: longint): cardinal; cdecl;
  43. external 'DOSCALLS' index 335;
  44. {
  45. function DosEnterCritSec:cardinal; cdecl; external 'DOSCALLS' index 232;
  46. function DosExitCritSec:cardinal; cdecl; external 'DOSCALLS' index 233;
  47. }
  48. procedure DosSleep (MSec: cardinal); cdecl; external 'DOSCALLS' index 229;
  49. {*****************************************************************************
  50. Threadvar support
  51. *****************************************************************************}
  52. const
  53. ThreadVarBlockSize: dword = 0;
  54. var
  55. (* Pointer to an allocated dword space within the local thread *)
  56. (* memory area. Pointer to the real memory block allocated for *)
  57. (* thread vars in this block is then stored in this dword. *)
  58. DataIndex: PPointer;
  59. procedure SysInitThreadvar (var Offset: dword; Size: dword);
  60. begin
  61. Offset := ThreadVarBlockSize;
  62. Inc (ThreadVarBlockSize, Size);
  63. end;
  64. function SysRelocateThreadVar (Offset: dword): pointer;
  65. begin
  66. SysRelocateThreadVar := DataIndex^ + Offset;
  67. end;
  68. procedure SysAllocateThreadVars;
  69. begin
  70. { we've to allocate the memory from the OS }
  71. { because the FPC heap management uses }
  72. { exceptions which use threadvars but }
  73. { these aren't allocated yet ... }
  74. { allocate room on the heap for the thread vars }
  75. if DosAllocMem (DataIndex^, ThreadVarBlockSize, pag_Read or pag_Write
  76. or pag_Commit) <> 0 then HandleError (8);
  77. end;
  78. procedure SysReleaseThreadVars;
  79. begin
  80. { release thread vars }
  81. DosFreeMem (DataIndex^);
  82. DosFreeThreadLocalMemory (DataIndex);
  83. end;
  84. (* procedure InitThreadVars;
  85. begin
  86. { allocate one ThreadVar entry from the OS, we use this entry }
  87. { for a pointer to our threadvars }
  88. if DosAllocThreadLocalMemory (1, DataIndex) <> 0 then HandleError (8);
  89. { initialize threadvars }
  90. init_all_unit_threadvars;
  91. { allocate mem for main thread threadvars }
  92. SysAllocateThreadVars;
  93. { copy main thread threadvars }
  94. copy_all_unit_threadvars;
  95. { install threadvar handler }
  96. fpc_threadvar_relocate_proc := @SysRelocateThreadvar;
  97. end;
  98. *)
  99. {*****************************************************************************
  100. Thread starting
  101. *****************************************************************************}
  102. type
  103. pthreadinfo = ^tthreadinfo;
  104. tthreadinfo = record
  105. f : tthreadfunc;
  106. p : pointer;
  107. stklen : cardinal;
  108. end;
  109. (* procedure InitThread(stklen:cardinal);
  110. begin
  111. SysResetFPU;
  112. SysInitFPU;
  113. { ExceptAddrStack and ExceptObjectStack are threadvars }
  114. { so every thread has its on exception handling capabilities }
  115. SysInitExceptions;
  116. { Open all stdio fds again }
  117. SysInitStdio;
  118. InOutRes:=0;
  119. // ErrNo:=0;
  120. { Stack checking }
  121. StackLength:=stklen;
  122. StackBottom:=Sptr - StackLength;
  123. end;
  124. *)
  125. function ThreadMain(param : pointer) : pointer;cdecl;
  126. var
  127. ti : tthreadinfo;
  128. begin
  129. { Allocate local thread vars, this must be the first thing,
  130. because the exception management and io depends on threadvars }
  131. SysAllocateThreadVars;
  132. { Copy parameter to local data }
  133. {$ifdef DEBUG_MT}
  134. writeln('New thread started, initialising ...');
  135. {$endif DEBUG_MT}
  136. ti:=pthreadinfo(param)^;
  137. dispose(pthreadinfo(param));
  138. { Initialize thread }
  139. InitThread(ti.stklen);
  140. { Start thread function }
  141. {$ifdef DEBUG_MT}
  142. writeln('Jumping to thread function');
  143. {$endif DEBUG_MT}
  144. ThreadMain:=pointer(ti.f(ti.p));
  145. end;
  146. function SysBeginThread(sa : Pointer;stacksize : SizeUInt;
  147. ThreadFunction : tthreadfunc;p : pointer;
  148. creationFlags : dword; var ThreadId : TThreadID) : DWord;
  149. var
  150. TI: PThreadInfo;
  151. begin
  152. {$ifdef DEBUG_MT}
  153. writeln('Creating new thread');
  154. {$endif DEBUG_MT}
  155. { Initialize multithreading if not done }
  156. if not IsMultiThread then
  157. begin
  158. if DosAllocThreadLocalMemory (1, DataIndex) <> 0
  159. then RunError (8);
  160. InitThreadVars(@SysRelocateThreadVar);
  161. IsMultiThread:=true;
  162. end;
  163. { the only way to pass data to the newly created thread
  164. in a MT safe way, is to use the heap }
  165. New (TI);
  166. TI^.F := ThreadFunction;
  167. TI^.P := P;
  168. TI^.StkLen := StackSize;
  169. { call pthread_create }
  170. {$ifdef DEBUG_MT}
  171. writeln('Starting new thread');
  172. {$endif DEBUG_MT}
  173. if DosCreateThread (DWord (ThreadID), @ThreadMain, SA,
  174. CreationFlags, StackSize) = 0 then
  175. SysBeginThread := ThreadID else SysBeginThread := 0;
  176. end;
  177. procedure SysEndThread (ExitCode : DWord);
  178. begin
  179. DoneThread;
  180. DosExit (1, ExitCode);
  181. end;
  182. procedure SysThreadSwitch;
  183. begin
  184. DosSleep (0);
  185. end;
  186. function SysSuspendThread (ThreadHandle: dword): dword;
  187. begin
  188. {$WARNING TODO!}
  189. { SysSuspendThread := WinSuspendThread(threadHandle);
  190. }
  191. end;
  192. function SysResumeThread (ThreadHandle: dword): dword;
  193. begin
  194. {$WARNING TODO!}
  195. { SysResumeThread := WinResumeThread(threadHandle);
  196. }
  197. end;
  198. function SysKillThread (ThreadHandle: dword): dword;
  199. var
  200. ExitCode: dword;
  201. begin
  202. {$WARNING TODO!}
  203. {
  204. if not TerminateThread (ThreadHandle, ExitCode) then
  205. SysKillThread := GetLastError
  206. else
  207. SysKillThread := 0;
  208. }
  209. end;
  210. function SysCloseThread (threadHandle : TThreadID) : dword;
  211. begin
  212. SysCloseThread := 0;
  213. // SysCloseThread:=CloseHandle(threadHandle);
  214. end;
  215. function SysWaitForThreadTerminate (ThreadHandle: dword;
  216. TimeoutMs: longint): dword;
  217. begin
  218. {$WARNING TODO!}
  219. {
  220. if TimeoutMs = 0 then dec (timeoutMs); // $ffffffff is INFINITE
  221. SysWaitForThreadTerminate := WaitForSingleObject(threadHandle, TimeoutMs);
  222. }
  223. end;
  224. function SysThreadSetPriority (ThreadHandle: dword;
  225. Prio: longint): boolean;
  226. {-15..+15, 0=normal}
  227. begin
  228. {$WARNING TODO!}
  229. {
  230. SysThreadSetPriority:=WinThreadSetPriority(threadHandle,Prio);
  231. }
  232. end;
  233. function SysThreadGetPriority (ThreadHandle: dword): longint;
  234. begin
  235. {$WARNING TODO!}
  236. {
  237. SysThreadGetPriority:=WinThreadGetPriority(threadHandle);
  238. }
  239. end;
  240. function SysGetCurrentThreadID: dword;
  241. begin
  242. {$WARNING TODO!}
  243. {
  244. SysGetCurrentThreadId:=WinGetCurrentThreadId;
  245. }
  246. end;
  247. {*****************************************************************************
  248. Delphi/Win32 compatibility
  249. *****************************************************************************}
  250. { DosEnter/ExitCritSec have quite a few limitations, so let's try to avoid
  251. them. I'm not sure whether mutex semaphores are SMP-safe, though... :-( }
  252. procedure SysInitCriticalSection(var CS);
  253. begin
  254. {$WARNING TODO!}
  255. end;
  256. procedure SysDoneCriticalSection (var CS);
  257. begin
  258. {$WARNING TODO!}
  259. end;
  260. procedure SysEnterCriticalSection (var CS);
  261. begin
  262. {$WARNING TODO!}
  263. end;
  264. procedure SysLeaveCriticalSection (var CS);
  265. begin
  266. {$WARNING TODO!}
  267. end;
  268. type
  269. TBasicEventState = record
  270. FHandle: THandle;
  271. FLastError: longint;
  272. end;
  273. PLocalEventRec = ^TBasicEventState;
  274. function IntBasicEventCreate (EventAttributes: Pointer;
  275. AManualReset, InitialState: Boolean; const Name: ansistring): PEventState;
  276. begin
  277. New (PLocalEventRec (Result));
  278. {$WARNING TODO!}
  279. {
  280. PLocalEventrec (Result)^.FHandle :=
  281. CreateEvent (EventAttributes, AManualReset, InitialState,PChar(Name));
  282. }
  283. end;
  284. procedure IntBasicEventDestroy (State: PEventState);
  285. begin
  286. {$WARNING TODO!}
  287. {
  288. closehandle(plocaleventrec(state)^.fhandle);
  289. }
  290. Dispose (PLocalEventRec (State));
  291. end;
  292. procedure IntBasicEventResetEvent (State: PEventState);
  293. begin
  294. {$WARNING TODO!}
  295. {
  296. ResetEvent(plocaleventrec(state)^.FHandle)
  297. }
  298. end;
  299. procedure IntBasicEventSetEvent (State: PEventState);
  300. begin
  301. {$WARNING TODO!}
  302. {
  303. SetEvent(plocaleventrec(state)^.FHandle);
  304. }
  305. end;
  306. function IntBasicEventWaitFor (Timeout: Cardinal; State: PEventState): longint;
  307. begin
  308. {$WARNING TODO!}
  309. {
  310. case WaitForSingleObject(plocaleventrec(state)^.fHandle, Timeout) of
  311. WAIT_ABANDONED: Result := wrAbandoned;
  312. WAIT_OBJECT_0: Result := wrSignaled;
  313. WAIT_TIMEOUT: Result := wrTimeout;
  314. WAIT_FAILED:
  315. begin
  316. Result := wrError;
  317. plocaleventrec(state)^.FLastError := GetLastError;
  318. end;
  319. else
  320. Result := wrError;
  321. end;
  322. }
  323. end;
  324. function IntRTLEventCreate: PRTLEvent;
  325. begin
  326. {$WARNING TODO!}
  327. {
  328. Result := PRTLEVENT(CreateEvent(nil, false, false, nil));
  329. }
  330. end;
  331. procedure IntRTLEventDestroy (AEvent: PRTLEvent);
  332. begin
  333. {$WARNING TODO!}
  334. {
  335. CloseHandle(THANDLE(AEvent));
  336. }
  337. end;
  338. procedure IntRTLEventSetEvent (AEvent: PRTLEvent);
  339. begin
  340. {$WARNING TODO!}
  341. {
  342. SetEvent(THANDLE(AEvent));
  343. }
  344. end;
  345. procedure IntRTLEventWaitFor (AEvent: PRTLEvent);
  346. CONST INFINITE=-1;
  347. begin
  348. {$WARNING TODO!}
  349. {
  350. WaitForSingleObject(THANDLE(AEvent), INFINITE);
  351. }
  352. end;
  353. function SysTryEnterCriticalSection (var CS): longint;
  354. begin
  355. {$WARNING TODO!}
  356. end;
  357. procedure IntRTLEventWaitForTimeout (AEvent: PRTLEvent; Timeout: longint);
  358. begin
  359. {$WARNING TODO!}
  360. {
  361. WaitForSingleObject(THANDLE(AEvent), Timeout);
  362. }
  363. end;
  364. procedure intRTLEventResetEvent (AEvent: PRTLEvent);
  365. begin
  366. {$WARNING TODO!}
  367. {
  368. ResetEvent(THANDLE(AEvent));
  369. }
  370. end;
  371. var
  372. OS2ThreadManager: TThreadManager;
  373. procedure InitSystemThreads;
  374. begin
  375. with OS2ThreadManager do
  376. begin
  377. InitManager :=Nil;
  378. DoneManager :=Nil;
  379. BeginThread :=@SysBeginThread;
  380. EndThread :=@SysEndThread;
  381. SuspendThread :=@SysSuspendThread;
  382. ResumeThread :=@SysResumeThread;
  383. KillThread :=@SysKillThread;
  384. ThreadSwitch :=@SysThreadSwitch;
  385. CloseThread :=@SysCloseThread;
  386. WaitForThreadTerminate :=@SysWaitForThreadTerminate;
  387. ThreadSetPriority :=@SysThreadSetPriority;
  388. ThreadGetPriority :=@SysThreadGetPriority;
  389. GetCurrentThreadId :=@SysGetCurrentThreadId;
  390. InitCriticalSection :=@SysInitCriticalSection;
  391. DoneCriticalSection :=@SysDoneCriticalSection;
  392. EnterCriticalSection :=@SysEnterCriticalSection;
  393. TryEnterCriticalSection:=@SysTryEnterCriticalSection;
  394. LeaveCriticalSection :=@SysLeaveCriticalSection;
  395. InitThreadVar :=@SysInitThreadVar;
  396. RelocateThreadVar :=@SysRelocateThreadVar;
  397. AllocateThreadVars :=@SysAllocateThreadVars;
  398. ReleaseThreadVars :=@SysReleaseThreadVars;
  399. BasicEventCreate :=@IntBasicEventCreate;
  400. BasicEventDestroy :=@IntBasicEventDestroy;
  401. BasicEventResetEvent :=@IntBasicEventResetEvent;
  402. BasicEventSetEvent :=@IntBasicEventSetEvent;
  403. BasiceventWaitFor :=@IntBasiceventWaitFor;
  404. RTLEventCreate :=@IntRTLEventCreate;
  405. RTLEventDestroy :=@IntRTLEventDestroy;
  406. RTLEventSetEvent :=@IntRTLEventSetEvent;
  407. RTLEventResetEvent :=@intRTLEventResetEvent;
  408. RTLEventWaitFor :=@IntRTLEventWaitFor;
  409. RTLEventWaitForTimeout :=@intRTLEventWaitForTimeout;
  410. end;
  411. SetThreadManager (OS2ThreadManager);
  412. end;