thread.inc 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487
  1. {
  2. This file is part of the Free Pascal Run time library.
  3. Copyright (c) 2000 by the Free Pascal development team
  4. OS independent thread functions/overloads
  5. See the File COPYING.FPC, included in this distribution,
  6. for details about the copyright.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  10. **********************************************************************}
  11. Var
  12. CurrentTM : TThreadManager;
  13. {*****************************************************************************
  14. Threadvar initialization
  15. *****************************************************************************}
  16. procedure InitThread(stklen:cardinal);
  17. begin
  18. SysResetFPU;
  19. { ExceptAddrStack and ExceptObjectStack are threadvars }
  20. { so every thread has its on exception handling capabilities }
  21. SysInitExceptions;
  22. { Open all stdio fds again }
  23. SysInitStdio;
  24. InOutRes:=0;
  25. // ErrNo:=0;
  26. { Stack checking }
  27. StackLength:=stklen;
  28. StackBottom:=Sptr - StackLength;
  29. ThreadID := CurrentTM.GetCurrentThreadID();
  30. end;
  31. {*****************************************************************************
  32. Overloaded functions
  33. *****************************************************************************}
  34. function BeginThread(ThreadFunction : tthreadfunc) : TThreadID;
  35. var
  36. dummy : TThreadID;
  37. begin
  38. BeginThread:=BeginThread(nil,DefaultStackSize,ThreadFunction,nil,0,dummy);
  39. end;
  40. function BeginThread(ThreadFunction : tthreadfunc;p : pointer) : TThreadID;
  41. var
  42. dummy : TThreadID;
  43. begin
  44. BeginThread:=BeginThread(nil,DefaultStackSize,ThreadFunction,p,0,dummy);
  45. end;
  46. function BeginThread(ThreadFunction : tthreadfunc;p : pointer;var ThreadId : TThreadID) : TThreadID;
  47. begin
  48. BeginThread:=BeginThread(nil,DefaultStackSize,ThreadFunction,p,0,ThreadId);
  49. end;
  50. procedure EndThread;
  51. begin
  52. EndThread(0);
  53. end;
  54. function BeginThread(sa : Pointer;stacksize : dword; ThreadFunction : tthreadfunc;p : pointer;creationFlags : dword; var ThreadId : TThreadID) : TThreadID;
  55. begin
  56. Result:=CurrentTM.BeginThread(sa,stacksize,threadfunction,P,creationflags,ThreadID);
  57. end;
  58. procedure EndThread(ExitCode : DWord);
  59. begin
  60. CurrentTM.EndThread(ExitCode);
  61. end;
  62. function SuspendThread (threadHandle : TThreadID) : dword;
  63. begin
  64. Result:=CurrentTM.SuspendThread(ThreadHandle);
  65. end;
  66. function ResumeThread (threadHandle : TThreadID) : dword;
  67. begin
  68. Result:=CurrentTM.ResumeThread(ThreadHandle);
  69. end;
  70. procedure ThreadSwitch;
  71. begin
  72. CurrentTM.ThreadSwitch;
  73. end;
  74. function KillThread (threadHandle : TThreadID) : dword;
  75. begin
  76. Result:=CurrentTM.KillThread(ThreadHandle);
  77. end;
  78. function WaitForThreadTerminate (threadHandle : TThreadID; TimeoutMs : longint) : dword;
  79. begin
  80. Result:=CurrentTM.WaitForThreadTerminate(ThreadHandle,TimeOutMS);
  81. end;
  82. function ThreadSetPriority (threadHandle : TThreadID; Prio: longint): boolean;
  83. begin
  84. Result:=CurrentTM.ThreadSetPriority(ThreadHandle,Prio);
  85. end;
  86. function ThreadGetPriority (threadHandle : TThreadID): longint;
  87. begin
  88. Result:=CurrentTM.ThreadGetPriority(ThreadHandle);
  89. end;
  90. function GetCurrentThreadId : TThreadID;
  91. begin
  92. Result:=CurrentTM.GetCurrentThreadID();
  93. end;
  94. procedure InitCriticalSection(var cs : TRTLCriticalSection);
  95. begin
  96. CurrentTM.InitCriticalSection(cs);
  97. end;
  98. procedure DoneCriticalsection(var cs : TRTLCriticalSection);
  99. begin
  100. CurrentTM.DoneCriticalSection(cs);
  101. end;
  102. procedure EnterCriticalsection(var cs : TRTLCriticalSection);
  103. begin
  104. CurrentTM.EnterCriticalSection(cs);
  105. end;
  106. procedure LeaveCriticalsection(var cs : TRTLCriticalSection);
  107. begin
  108. CurrentTM.LeaveCriticalSection(cs);
  109. end;
  110. Function GetThreadManager(Var TM : TThreadManager) : Boolean;
  111. begin
  112. TM:=CurrentTM;
  113. Result:=True;
  114. end;
  115. Function SetThreadManager(Const NewTM : TThreadManager; Var OldTM : TThreadManager) : Boolean;
  116. begin
  117. GetThreadManager(OldTM);
  118. Result:=SetThreadManager(NewTM);
  119. end;
  120. Function SetThreadManager(Const NewTM : TThreadManager) : Boolean;
  121. begin
  122. Result:=True;
  123. If Assigned(CurrentTM.DoneManager) then
  124. Result:=CurrentTM.DoneManager();
  125. If Result then
  126. begin
  127. CurrentTM:=NewTM;
  128. If Assigned(CurrentTM.InitManager) then
  129. Result:=CurrentTM.InitManager();
  130. end;
  131. end;
  132. function BasicEventCreate(EventAttributes : Pointer; AManualReset,InitialState : Boolean;const Name : ansistring):pEventState;
  133. begin
  134. result:=currenttm.BasicEventCreate(EventAttributes,AManualReset,InitialState, Name);
  135. end;
  136. procedure basiceventdestroy(state:peventstate);
  137. begin
  138. currenttm.basiceventdestroy(state);
  139. end;
  140. procedure basiceventResetEvent(state:peventstate);
  141. begin
  142. currenttm.basiceventResetEvent(state);
  143. end;
  144. procedure basiceventSetEvent(state:peventstate);
  145. begin
  146. currenttm.basiceventSetEvent(state);
  147. end;
  148. function basiceventWaitFor(Timeout : Cardinal;state:peventstate) : longint;
  149. begin
  150. result:=currenttm.basiceventWaitFor(Timeout,state);
  151. end;
  152. function RTLEventCreate :PRTLEvent;
  153. begin
  154. result:=currenttm.rtleventcreate();
  155. end;
  156. procedure RTLeventdestroy(state:pRTLEvent);
  157. begin
  158. currenttm.rtleventdestroy(state);
  159. end;
  160. procedure RTLeventSetEvent(state:pRTLEvent);
  161. begin
  162. currenttm.rtleventsetEvent(state);
  163. end;
  164. procedure RTLeventResetEvent(state:pRTLEvent);
  165. begin
  166. currenttm.rtleventResetEvent(state);
  167. end;
  168. procedure RTLeventStartWait(state:pRTLEvent);
  169. begin
  170. currenttm.rtleventStartWait(state);
  171. end;
  172. procedure RTLeventWaitFor(state:pRTLEvent);
  173. begin
  174. currenttm.rtleventWaitFor(state);
  175. end;
  176. procedure RTLeventWaitFor(state:pRTLEvent;timeout : longint);
  177. begin
  178. currenttm.rtleventWaitForTimeout(state,timeout);
  179. end;
  180. procedure RTLeventsync(m:trtlmethod;p:tprocedure);
  181. begin
  182. currenttm.rtleventsync(m,p);
  183. end;
  184. { ---------------------------------------------------------------------
  185. ThreadManager which gives run-time error. Use if no thread support.
  186. ---------------------------------------------------------------------}
  187. {$ifndef DISABLE_NO_THREAD_MANAGER}
  188. { resourcestrings are not supported by the system unit,
  189. they are in the objpas unit and not available for fpc/tp modes }
  190. const
  191. SNoThreads = 'This binary has no thread support compiled in.';
  192. SRecompileWithThreads = 'Recompile the application with a thread-driver in the program uses clause before other units using thread.';
  193. Procedure NoThreadError;
  194. begin
  195. If IsConsole then
  196. begin
  197. Writeln(StdErr,SNoThreads);
  198. Writeln(StdErr,SRecompileWithThreads);
  199. end;
  200. RunError(232)
  201. end;
  202. function NoBeginThread(sa : Pointer;stacksize : dword;
  203. ThreadFunction : tthreadfunc;p : pointer;
  204. creationFlags : dword; var ThreadId : TThreadID) : TThreadID;
  205. begin
  206. NoThreadError;
  207. end;
  208. procedure NoEndThread(ExitCode : DWord);
  209. begin
  210. NoThreadError;
  211. end;
  212. function NoThreadHandler (threadHandle : TThreadID) : dword;
  213. begin
  214. NoThreadError;
  215. end;
  216. procedure NoThreadSwitch; {give time to other threads}
  217. begin
  218. NoThreadError;
  219. end;
  220. function NoWaitForThreadTerminate (threadHandle : TThreadID; TimeoutMs : longint) : dword; {0=no timeout}
  221. begin
  222. NoThreadError;
  223. end;
  224. function NoThreadSetPriority (threadHandle : TThreadID; Prio: longint): boolean; {-15..+15, 0=normal}
  225. begin
  226. NoThreadError;
  227. end;
  228. function NoThreadGetPriority (threadHandle : TThreadID): longint;
  229. begin
  230. NoThreadError;
  231. end;
  232. function NoGetCurrentThreadId : TThreadID;
  233. begin
  234. if IsMultiThread then
  235. NoThreadError
  236. else
  237. ThreadingAlreadyUsed:=true;
  238. result:=ThreadID;
  239. end;
  240. procedure NoCriticalSection(var CS);
  241. begin
  242. if IsMultiThread then
  243. NoThreadError
  244. else
  245. ThreadingAlreadyUsed:=true;
  246. end;
  247. procedure NoInitThreadvar(var offset : dword;size : dword);
  248. begin
  249. NoThreadError;
  250. end;
  251. function NoRelocateThreadvar(offset : dword) : pointer;
  252. begin
  253. NoThreadError;
  254. end;
  255. procedure NoAllocateThreadVars;
  256. begin
  257. NoThreadError;
  258. end;
  259. procedure NoReleaseThreadVars;
  260. begin
  261. NoThreadError;
  262. end;
  263. function noBasicEventCreate(EventAttributes : Pointer; AManualReset,InitialState : Boolean;const Name : ansistring):pEventState;
  264. begin
  265. NoThreadError;
  266. end;
  267. procedure nobasiceventdestroy(state:peventstate);
  268. begin
  269. NoThreadError;
  270. end;
  271. procedure nobasiceventResetEvent(state:peventstate);
  272. begin
  273. NoThreadError;
  274. end;
  275. procedure nobasiceventSetEvent(state:peventstate);
  276. begin
  277. NoThreadError;
  278. end;
  279. function nobasiceventWaitFor(Timeout : Cardinal;state:peventstate) : longint;
  280. begin
  281. NoThreadError;
  282. end;
  283. function NORTLEventCreate :PRTLEvent;
  284. begin
  285. if IsMultiThread then
  286. NoThreadError
  287. else
  288. ThreadingAlreadyUsed:=true
  289. end;
  290. procedure NORTLeventdestroy(state:pRTLEvent);
  291. begin
  292. if IsMultiThread then
  293. NoThreadError
  294. else
  295. ThreadingAlreadyUsed:=true
  296. end;
  297. procedure NORTLeventSetEvent(state:pRTLEvent);
  298. begin
  299. NoThreadError;
  300. end;
  301. procedure NORTLeventStartWait(state:pRTLEvent);
  302. begin
  303. NoThreadError;
  304. end;
  305. procedure NORTLeventWaitFor(state:pRTLEvent);
  306. begin
  307. NoThreadError;
  308. end;
  309. procedure NORTLeventWaitForTimeout(state:pRTLEvent;timeout : longint);
  310. begin
  311. NoThreadError;
  312. end;
  313. procedure NORTLeventsync(m:trtlmethod;p:tprocedure);
  314. begin
  315. NoThreadError;
  316. end;
  317. Var
  318. NoThreadManager : TThreadManager;
  319. Procedure SetNoThreadManager;
  320. begin
  321. With NoThreadManager do
  322. begin
  323. InitManager :=Nil;
  324. DoneManager :=Nil;
  325. BeginThread :=@NoBeginThread;
  326. EndThread :=@NoEndThread;
  327. SuspendThread :=@NoThreadHandler;
  328. ResumeThread :=@NoThreadHandler;
  329. KillThread :=@NoThreadHandler;
  330. ThreadSwitch :=@NoThreadSwitch;
  331. WaitForThreadTerminate :=@NoWaitForThreadTerminate;
  332. ThreadSetPriority :=@NoThreadSetPriority;
  333. ThreadGetPriority :=@NoThreadGetPriority;
  334. GetCurrentThreadId :=@NoGetCurrentThreadId;
  335. InitCriticalSection :=@NoCriticalSection;
  336. DoneCriticalSection :=@NoCriticalSection;
  337. EnterCriticalSection :=@NoCriticalSection;
  338. LeaveCriticalSection :=@NoCriticalSection;
  339. InitThreadVar :=@NoInitThreadVar;
  340. RelocateThreadVar :=@NoRelocateThreadVar;
  341. AllocateThreadVars :=@NoAllocateThreadVars;
  342. ReleaseThreadVars :=@NoReleaseThreadVars;
  343. BasicEventCreate :=@NoBasicEventCreate;
  344. basiceventdestroy :=@Nobasiceventdestroy;
  345. basiceventResetEvent :=@NobasiceventResetEvent;
  346. basiceventSetEvent :=@NobasiceventSetEvent;
  347. basiceventWaitFor :=@NobasiceventWaitFor;
  348. rtlEventCreate :=@NortlEventCreate;
  349. rtleventdestroy :=@Nortleventdestroy;
  350. rtleventSetEvent :=@NortleventSetEvent;
  351. rtleventStartWait :=@NortleventStartWait;
  352. rtleventWaitFor :=@NortleventWaitFor;
  353. rtleventsync :=@Nortleventsync;
  354. rtleventwaitfortimeout :=@NortleventWaitForTimeout;
  355. end;
  356. SetThreadManager(NoThreadManager);
  357. end;
  358. {$endif DISABLE_NO_THREAD_MANAGER}