systhrd.inc 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  1. {
  2. This file is part of the Free Pascal run time library.
  3. Copyright (c) 2013 by Marcus Sackrow.
  4. See the file COPYING.FPC, included in this distribution,
  5. for details about the copyright.
  6. This program is distributed in the hope that it will be useful,
  7. but WITHOUT ANY WARRANTY; without even the implied warranty of
  8. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  9. **********************************************************************}
  10. //type
  11. // TThreadEntryfunction = function(data: Pointer): Pointer; cdecl;
  12. const
  13. threadvarblocksize : dword = 0; // total size of allocated threadvars
  14. thredvarsmainthread: pointer = nil; // to free the threadvars in the signal handler
  15. var
  16. ThreadsVarList: array of Pointer;
  17. {$define THREAD_SYSTEM}
  18. {$I arosthreads.inc}
  19. // Thread manager:
  20. procedure SysInitThreadvar(var offset : dword;size : dword);
  21. begin
  22. //offset:=threadvarblocksize;
  23. //inc(threadvarblocksize,size);
  24. end;
  25. procedure SaveThreadVars(t: Pointer);
  26. var
  27. Idx: Integer;
  28. begin
  29. {Idx := AROSCurrentThread();
  30. if Idx >= 0 then
  31. begin
  32. if Idx > High(ThreadsVarList) then
  33. SetLength(ThreadsVarList, Idx + 1);
  34. ThreadsVarList[Idx] := t;
  35. end;}
  36. end;
  37. function GetThreadV: Pointer;
  38. var
  39. Idx: Integer;
  40. begin
  41. {
  42. Result := nil;
  43. Idx := AROSCurrentThread();
  44. if (Idx >= 0) and (Idx <= High(ThreadsVarList)) then
  45. begin
  46. Result := ThreadsVarList[Idx];
  47. end;
  48. }
  49. end;
  50. function SysRelocateThreadvar (offset: dword): Pointer;
  51. begin
  52. //SysRelocateThreadvar:= GetThreadV + offset;
  53. end;
  54. procedure SaveThreadV(t: Pointer);
  55. var
  56. Idx: Integer;
  57. begin
  58. {Idx := AROSCurrentThread();
  59. if Idx >= 0 then
  60. begin
  61. if Idx > High(ThreadsVarList) then
  62. SetLength(ThreadsVarList, Idx + 1);
  63. ThreadsVarList[Idx] := t;
  64. end;}
  65. end;
  66. procedure SysAllocateThreadVars;
  67. var
  68. threadvars: Pointer;
  69. begin
  70. {threadvars := AllocPooled(AOS_heapPool, threadvarblocksize);
  71. FillChar(threadvars^, threadvarblocksize, 0);
  72. SaveThreadV(threadvars);
  73. if thredvarsmainthread = nil then
  74. thredvarsmainthread := threadvars;}
  75. end;
  76. procedure SysReleaseThreadVars;
  77. var
  78. threadvars: Pointer;
  79. begin
  80. { release thread vars }
  81. {
  82. if threadvarblocksize > 0 then
  83. begin
  84. threadvars := GetThreadV;
  85. if threadvars <> nil then
  86. begin
  87. FreePooled(AOS_heapPool, threadvars, threadvarblocksize);
  88. SaveThreadVars(nil);
  89. end;
  90. end;}
  91. end;
  92. type
  93. TThreadInfo = record
  94. F: TThreadfunc;
  95. P: Pointer;
  96. end;
  97. PThreadinfo = ^TThreadinfo;
  98. function ThreadFunc(Data: Pointer): Pointer; cdecl;
  99. var
  100. Ti: TThreadinfo;
  101. begin
  102. {SysAllocateThreadVars;
  103. ti := PThreadInfo(Data)^;
  104. Dispose(PThreadInfo(Data));
  105. // execute
  106. ThreadFunc := Pointer(Ti.f(Ti.p));
  107. DoneThread;}
  108. end;
  109. function SysBeginThread(Sa: Pointer; StackSize: PtrUInt; ThreadFunction: TThreadfunc; p: Pointer; CreationFlags: dword; var ThreadId: TThreadID): TThreadID;
  110. var
  111. Ti: PThreadinfo;
  112. begin
  113. Result := 0;
  114. if not IsMultiThread then
  115. begin
  116. InitThreadVars(@SysRelocateThreadvar);
  117. IsMultithread:=true;
  118. end;
  119. New(Ti);
  120. Ti^.f := ThreadFunction;
  121. Ti^.p := p;
  122. SetLength(ThreadsVarList, 200);
  123. //SysBeginThread := CreateThread(@ThreadFunc, Ti);
  124. ThreadID := SysBeginThread;
  125. end;
  126. procedure SysEndThread(ExitCode : DWord);
  127. begin
  128. DoneThread;
  129. //ExitThread(Pointer(ExitCode));
  130. end;
  131. procedure SysThreadSwitch;
  132. begin
  133. DOSDelay(0);
  134. end;
  135. function SysSuspendThread(ThreadHandle: THandle): dword;
  136. begin
  137. Result := 0;
  138. end;
  139. function SysResumeThread(ThreadHandle: THandle): dword;
  140. begin
  141. Result := 0;
  142. end;
  143. function SysKillThread(threadHandle: THandle): dword;
  144. begin
  145. SysKillThread := 0; {not supported for AROS}
  146. end;
  147. function SysWaitForThreadTerminate(threadHandle: THandle; TimeoutMs: LongInt): dword;
  148. begin
  149. Result := 0;
  150. end;
  151. function SysThreadSetPriority (threadHandle : THandle; Prio: longint): boolean; {-15..+15, 0=normal}
  152. begin
  153. SysThreadSetPriority := true;
  154. end;
  155. function SysThreadGetPriority (threadHandle : THandle): Longint;
  156. begin
  157. SysThreadGetPriority := 0;
  158. end;
  159. function SysGetCurrentThreadId: LongInt;
  160. begin
  161. SysGetCurrentThreadId := AROSCurrentThread;
  162. end;
  163. // Close all Semaphores
  164. procedure SysCloseAllRemainingSemaphores;
  165. var
  166. i: Integer;
  167. begin
  168. ObtainSemaphore(@AROSThreadStruct^.MutexListSem);
  169. i := 0;
  170. for i := 0 to High(AROSThreadStruct^.MutexList) do
  171. begin
  172. if Assigned(AROSThreadStruct^.MutexList[i]) then
  173. begin
  174. Dispose(AROSThreadStruct^.MutexList[i]);
  175. end;
  176. end;
  177. ReleaseSemaphore(@AROSThreadStruct^.MutexListSem);
  178. end;
  179. // Critical Sections (done by Mutex)
  180. procedure SysInitCriticalSection(var cs: TRTLCriticalSection);
  181. begin
  182. cs := CreateMutex;
  183. //DebugLn('Create Mutex');
  184. end;
  185. procedure SysDoneCriticalsection(var cs: TRTLCriticalSection);
  186. begin
  187. //DebugLn('Destroy Mutex');
  188. if Assigned(cs) then
  189. DestroyMutex(TRTLCriticalSection(cs));
  190. cs := nil;
  191. end;
  192. procedure SysEnterCriticalsection(var cs: TRTLCriticalSection);
  193. begin
  194. //DebugLn('EnterMutex');
  195. if Assigned(cs) then
  196. LockMutex(cs);
  197. end;
  198. function SysTryEnterCriticalsection(var cs: TRTLCriticalSection): longint;
  199. begin
  200. //DebugLn('TryEnter Mutex');
  201. Result := 0;
  202. if Assigned(cs) then
  203. Result := LongInt(TryLockMutex(cs));
  204. end;
  205. procedure SysLeaveCriticalsection(var cs: TRTLCriticalSection);
  206. begin
  207. //DebugLn('Leave Mutex');
  208. if Assigned(cs) then
  209. UnlockMutex(cs);
  210. end;
  211. function SysSetThreadDataAreaPtr (newPtr:pointer):pointer;
  212. begin
  213. end;
  214. function intBasicEventCreate(EventAttributes : Pointer;
  215. AManualReset,InitialState : Boolean;const Name : ansistring):pEventState;
  216. begin
  217. end;
  218. procedure intbasiceventdestroy(state:peventstate);
  219. begin
  220. end;
  221. procedure intbasiceventResetEvent(state:peventstate);
  222. begin
  223. end;
  224. procedure intbasiceventSetEvent(state:peventstate);
  225. begin
  226. end;
  227. function intbasiceventWaitFor(Timeout : Cardinal;state:peventstate) : longint;
  228. begin
  229. end;
  230. function intRTLEventCreate: PRTLEvent;
  231. begin
  232. end;
  233. procedure intRTLEventDestroy(AEvent: PRTLEvent);
  234. begin
  235. end;
  236. procedure intRTLEventSetEvent(AEvent: PRTLEvent);
  237. begin
  238. end;
  239. procedure intRTLEventResetEvent(AEvent: PRTLEvent);
  240. begin
  241. end;
  242. procedure intRTLEventWaitFor(AEvent: PRTLEvent);
  243. begin
  244. end;
  245. procedure intRTLEventWaitForTimeout(AEvent: PRTLEvent;timeout : longint);
  246. begin
  247. end;
  248. function SysInitManager: Boolean;
  249. begin
  250. InitThreadLib;
  251. Result := True;
  252. end;
  253. function SysDoneManager: Boolean;
  254. begin
  255. FinishThreadLib;
  256. Result := True;
  257. end;
  258. Var
  259. AROSThreadManager : TThreadManager;
  260. procedure InitSystemThreads;
  261. begin
  262. with AROSThreadManager do
  263. begin
  264. InitManager :=@SysInitManager;
  265. DoneManager :=@SysDoneManager;
  266. BeginThread :=@SysBeginThread;
  267. EndThread :=@SysEndThread;
  268. SuspendThread :=@SysSuspendThread;
  269. ResumeThread :=@SysResumeThread;
  270. KillThread :=@SysKillThread;
  271. ThreadSwitch :=@SysThreadSwitch;
  272. WaitForThreadTerminate :=@SysWaitForThreadTerminate;
  273. ThreadSetPriority :=@SysThreadSetPriority;
  274. ThreadGetPriority :=@SysThreadGetPriority;
  275. GetCurrentThreadId :=@SysGetCurrentThreadId;
  276. InitCriticalSection :=TCriticalSectionHandler(@SysInitCriticalSection);
  277. DoneCriticalSection :=TCriticalSectionHandler(@SysDoneCriticalSection);
  278. EnterCriticalSection :=TCriticalSectionHandler(@SysEnterCriticalSection);
  279. LeaveCriticalSection :=TCriticalSectionHandler(@SysLeaveCriticalSection);
  280. InitThreadVar :=@SysInitThreadVar;
  281. RelocateThreadVar :=@SysRelocateThreadVar;
  282. AllocateThreadVars :=@SysAllocateThreadVars;
  283. ReleaseThreadVars :=@SysReleaseThreadVars;
  284. BasicEventCreate :=@intBasicEventCreate;
  285. basiceventdestroy :=@intbasiceventdestroy;
  286. basiceventResetEvent :=@intbasiceventResetEvent;
  287. basiceventSetEvent :=@intbasiceventSetEvent;
  288. basiceventWaitFor :=@intbasiceventWaitFor;
  289. RTLEventCreate :=@intRTLEventCreate;
  290. RTLEventDestroy :=@intRTLEventDestroy;
  291. RTLEventSetEvent :=@intRTLEventSetEvent;
  292. RTLEventResetEvent :=@intRTLEventResetEvent;
  293. RTLEventWaitFor :=@intRTLEventWaitFor;
  294. RTLEventWaitForTimeout :=@intRTLEventWaitForTimeout;
  295. end;
  296. SetThreadManager(AROSThreadManager);
  297. end;