systhrd.inc 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474
  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. { ExceptAddrStack and ExceptObjectStack are threadvars }
  113. { so every thread has its on exception handling capabilities }
  114. SysInitExceptions;
  115. { Open all stdio fds again }
  116. SysInitStdio;
  117. InOutRes:=0;
  118. // ErrNo:=0;
  119. { Stack checking }
  120. StackLength:=stklen;
  121. StackBottom:=Sptr - StackLength;
  122. end;
  123. *)
  124. function ThreadMain(param : pointer) : pointer;cdecl;
  125. var
  126. ti : tthreadinfo;
  127. begin
  128. { Allocate local thread vars, this must be the first thing,
  129. because the exception management and io depends on threadvars }
  130. SysAllocateThreadVars;
  131. { Copy parameter to local data }
  132. {$ifdef DEBUG_MT}
  133. writeln('New thread started, initialising ...');
  134. {$endif DEBUG_MT}
  135. ti:=pthreadinfo(param)^;
  136. dispose(pthreadinfo(param));
  137. { Initialize thread }
  138. InitThread(ti.stklen);
  139. { Start thread function }
  140. {$ifdef DEBUG_MT}
  141. writeln('Jumping to thread function');
  142. {$endif DEBUG_MT}
  143. ThreadMain:=pointer(ti.f(ti.p));
  144. end;
  145. function SysBeginThread(sa : Pointer;stacksize : SizeUInt;
  146. ThreadFunction : tthreadfunc;p : pointer;
  147. creationFlags : dword; var ThreadId : TThreadID) : DWord;
  148. var
  149. TI: PThreadInfo;
  150. begin
  151. {$ifdef DEBUG_MT}
  152. writeln('Creating new thread');
  153. {$endif DEBUG_MT}
  154. { Initialize multithreading if not done }
  155. if not IsMultiThread then
  156. begin
  157. if DosAllocThreadLocalMemory (1, DataIndex) <> 0
  158. then RunError (8);
  159. InitThreadVars(@SysRelocateThreadVar);
  160. IsMultiThread:=true;
  161. end;
  162. { the only way to pass data to the newly created thread
  163. in a MT safe way, is to use the heap }
  164. New (TI);
  165. TI^.F := ThreadFunction;
  166. TI^.P := P;
  167. TI^.StkLen := StackSize;
  168. { call pthread_create }
  169. {$ifdef DEBUG_MT}
  170. writeln('Starting new thread');
  171. {$endif DEBUG_MT}
  172. if DosCreateThread (DWord (ThreadID), @ThreadMain, SA,
  173. CreationFlags, StackSize) = 0 then
  174. SysBeginThread := ThreadID else SysBeginThread := 0;
  175. end;
  176. procedure SysEndThread (ExitCode : DWord);
  177. begin
  178. DoneThread;
  179. DosExit (1, ExitCode);
  180. end;
  181. procedure SysThreadSwitch;
  182. begin
  183. DosSleep (0);
  184. end;
  185. function SysSuspendThread (ThreadHandle: dword): dword;
  186. begin
  187. {$WARNING TODO!}
  188. { SysSuspendThread := WinSuspendThread(threadHandle);
  189. }
  190. end;
  191. function SysResumeThread (ThreadHandle: dword): dword;
  192. begin
  193. {$WARNING TODO!}
  194. { SysResumeThread := WinResumeThread(threadHandle);
  195. }
  196. end;
  197. function SysKillThread (ThreadHandle: dword): dword;
  198. var
  199. ExitCode: dword;
  200. begin
  201. {$WARNING TODO!}
  202. {
  203. if not TerminateThread (ThreadHandle, ExitCode) then
  204. SysKillThread := GetLastError
  205. else
  206. SysKillThread := 0;
  207. }
  208. end;
  209. function SysWaitForThreadTerminate (ThreadHandle: dword;
  210. TimeoutMs: longint): dword;
  211. begin
  212. {$WARNING TODO!}
  213. {
  214. if TimeoutMs = 0 then dec (timeoutMs); // $ffffffff is INFINITE
  215. SysWaitForThreadTerminate := WaitForSingleObject(threadHandle, TimeoutMs);
  216. }
  217. end;
  218. function SysThreadSetPriority (ThreadHandle: dword;
  219. Prio: longint): boolean;
  220. {-15..+15, 0=normal}
  221. begin
  222. {$WARNING TODO!}
  223. {
  224. SysThreadSetPriority:=WinThreadSetPriority(threadHandle,Prio);
  225. }
  226. end;
  227. function SysThreadGetPriority (ThreadHandle: dword): longint;
  228. begin
  229. {$WARNING TODO!}
  230. {
  231. SysThreadGetPriority:=WinThreadGetPriority(threadHandle);
  232. }
  233. end;
  234. function SysGetCurrentThreadID: dword;
  235. begin
  236. {$WARNING TODO!}
  237. {
  238. SysGetCurrentThreadId:=WinGetCurrentThreadId;
  239. }
  240. end;
  241. {*****************************************************************************
  242. Delphi/Win32 compatibility
  243. *****************************************************************************}
  244. { DosEnter/ExitCritSec have quite a few limitations, so let's try to avoid
  245. them. I'm not sure whether mutex semaphores are SMP-safe, though... :-( }
  246. procedure SysInitCriticalSection(var CS);
  247. begin
  248. {$WARNING TODO!}
  249. end;
  250. procedure SysDoneCriticalSection (var CS);
  251. begin
  252. {$WARNING TODO!}
  253. end;
  254. procedure SysEnterCriticalSection (var CS);
  255. begin
  256. {$WARNING TODO!}
  257. end;
  258. procedure SysLeaveCriticalSection (var CS);
  259. begin
  260. {$WARNING TODO!}
  261. end;
  262. type
  263. TBasicEventState = record
  264. FHandle: THandle;
  265. FLastError: longint;
  266. end;
  267. PLocalEventRec = ^TBasicEventState;
  268. function IntBasicEventCreate (EventAttributes: Pointer;
  269. AManualReset, InitialState: Boolean; const Name: ansistring): PEventState;
  270. begin
  271. New (PLocalEventRec (Result));
  272. {$WARNING TODO!}
  273. {
  274. PLocalEventrec (Result)^.FHandle :=
  275. CreateEvent (EventAttributes, AManualReset, InitialState,PChar(Name));
  276. }
  277. end;
  278. procedure IntBasicEventDestroy (State: PEventState);
  279. begin
  280. {$WARNING TODO!}
  281. {
  282. closehandle(plocaleventrec(state)^.fhandle);
  283. }
  284. Dispose (PLocalEventRec (State));
  285. end;
  286. procedure IntBasicEventResetEvent (State: PEventState);
  287. begin
  288. {$WARNING TODO!}
  289. {
  290. ResetEvent(plocaleventrec(state)^.FHandle)
  291. }
  292. end;
  293. procedure IntBasicEventSetEvent (State: PEventState);
  294. begin
  295. {$WARNING TODO!}
  296. {
  297. SetEvent(plocaleventrec(state)^.FHandle);
  298. }
  299. end;
  300. function IntBasicEventWaitFor (Timeout: Cardinal; State: PEventState): longint;
  301. begin
  302. {$WARNING TODO!}
  303. {
  304. case WaitForSingleObject(plocaleventrec(state)^.fHandle, Timeout) of
  305. WAIT_ABANDONED: Result := wrAbandoned;
  306. WAIT_OBJECT_0: Result := wrSignaled;
  307. WAIT_TIMEOUT: Result := wrTimeout;
  308. WAIT_FAILED:
  309. begin
  310. Result := wrError;
  311. plocaleventrec(state)^.FLastError := GetLastError;
  312. end;
  313. else
  314. Result := wrError;
  315. end;
  316. }
  317. end;
  318. function IntRTLEventCreate: PRTLEvent;
  319. begin
  320. {$WARNING TODO!}
  321. {
  322. Result := PRTLEVENT(CreateEvent(nil, false, false, nil));
  323. }
  324. end;
  325. procedure IntRTLEventDestroy (AEvent: PRTLEvent);
  326. begin
  327. {$WARNING TODO!}
  328. {
  329. CloseHandle(THANDLE(AEvent));
  330. }
  331. end;
  332. procedure IntRTLEventSetEvent (AEvent: PRTLEvent);
  333. begin
  334. {$WARNING TODO!}
  335. {
  336. SetEvent(THANDLE(AEvent));
  337. }
  338. end;
  339. CONST INFINITE=-1;
  340. procedure IntRTLEventWaitFor (AEvent: PRTLEvent);
  341. begin
  342. {$WARNING TODO!}
  343. {
  344. WaitForSingleObject(THANDLE(AEvent), INFINITE);
  345. }
  346. end;
  347. var
  348. OS2ThreadManager: TThreadManager;
  349. procedure InitSystemThreads;
  350. begin
  351. with OS2ThreadManager do
  352. begin
  353. InitManager :=Nil;
  354. DoneManager :=Nil;
  355. BeginThread :=@SysBeginThread;
  356. EndThread :=@SysEndThread;
  357. SuspendThread :=@SysSuspendThread;
  358. ResumeThread :=@SysResumeThread;
  359. KillThread :=@SysKillThread;
  360. ThreadSwitch :=@SysThreadSwitch;
  361. WaitForThreadTerminate :=@SysWaitForThreadTerminate;
  362. ThreadSetPriority :=@SysThreadSetPriority;
  363. ThreadGetPriority :=@SysThreadGetPriority;
  364. GetCurrentThreadId :=@SysGetCurrentThreadId;
  365. InitCriticalSection :=@SysInitCriticalSection;
  366. DoneCriticalSection :=@SysDoneCriticalSection;
  367. EnterCriticalSection :=@SysEnterCriticalSection;
  368. LeaveCriticalSection :=@SysLeaveCriticalSection;
  369. InitThreadVar :=@SysInitThreadVar;
  370. RelocateThreadVar :=@SysRelocateThreadVar;
  371. AllocateThreadVars :=@SysAllocateThreadVars;
  372. ReleaseThreadVars :=@SysReleaseThreadVars;
  373. BasicEventCreate :=@IntBasicEventCreate;
  374. BasicEventDestroy :=@IntBasicEventDestroy;
  375. BasicEventResetEvent :=@IntBasicEventResetEvent;
  376. BasicEventSetEvent :=@IntBasicEventSetEvent;
  377. BasiceventWaitFor :=@IntBasiceventWaitFor;
  378. RTLEventCreate :=@IntRTLEventCreate;
  379. RTLEventDestroy :=@IntRTLEventDestroy;
  380. RTLEventSetEvent :=@IntRTLEventSetEvent;
  381. RTLEventWaitFor :=@IntRTLEventWaitFor;
  382. end;
  383. SetThreadManager (OS2ThreadManager);
  384. end;