thread.inc 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  1. {
  2. $Id$
  3. This file is part of the Free Pascal Run time library.
  4. Copyright (c) 2000 by the Free Pascal development team
  5. OS independent thread functions/overloads
  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. Var
  13. CurrentTM : TThreadManager;
  14. {*****************************************************************************
  15. Threadvar initialization
  16. *****************************************************************************}
  17. procedure InitThread(stklen:cardinal);
  18. begin
  19. SysResetFPU;
  20. { ExceptAddrStack and ExceptObjectStack are threadvars }
  21. { so every thread has its on exception handling capabilities }
  22. SysInitExceptions;
  23. { Open all stdio fds again }
  24. SysInitStdio;
  25. InOutRes:=0;
  26. // ErrNo:=0;
  27. { Stack checking }
  28. StackLength:=stklen;
  29. StackBottom:=Sptr - StackLength;
  30. ThreadID := CurrentTM.GetCurrentThreadID();
  31. end;
  32. {*****************************************************************************
  33. Overloaded functions
  34. *****************************************************************************}
  35. function BeginThread(sa : Pointer;stacksize : dword;
  36. ThreadFunction : tthreadfunc;p : pointer;creationFlags : dword;
  37. var ThreadId : Longint) : DWord;
  38. begin
  39. BeginThread:=BeginThread(nil,StackSize,ThreadFunction,p,creationFlags,Dword(THreadId));
  40. end;
  41. function BeginThread(ThreadFunction : tthreadfunc) : DWord;
  42. var
  43. dummy : dword;
  44. begin
  45. BeginThread:=BeginThread(nil,DefaultStackSize,ThreadFunction,nil,0,dummy);
  46. end;
  47. function BeginThread(ThreadFunction : tthreadfunc;p : pointer) : DWord;
  48. var
  49. dummy : dword;
  50. begin
  51. BeginThread:=BeginThread(nil,DefaultStackSize,ThreadFunction,p,0,dummy);
  52. end;
  53. function BeginThread(ThreadFunction : tthreadfunc;p : pointer;var ThreadId : DWord) : DWord;
  54. begin
  55. BeginThread:=BeginThread(nil,DefaultStackSize,ThreadFunction,p,0,ThreadId);
  56. end;
  57. function BeginThread(ThreadFunction : tthreadfunc;p : pointer;var ThreadId : Longint) : DWord;
  58. begin
  59. BeginThread:=BeginThread(nil,DefaultStackSize,ThreadFunction,p,0,Dword(ThreadId));
  60. end;
  61. procedure EndThread;
  62. begin
  63. EndThread(0);
  64. end;
  65. function BeginThread(sa : Pointer;stacksize : dword; ThreadFunction : tthreadfunc;p : pointer;creationFlags : dword; var ThreadId : DWord) : DWord;
  66. begin
  67. Result:=CurrentTM.BeginThread(sa,stacksize,threadfunction,P,creationflags,ThreadID);
  68. end;
  69. procedure EndThread(ExitCode : DWord);
  70. begin
  71. CurrentTM.EndThread(ExitCode);
  72. end;
  73. function SuspendThread (threadHandle : dword) : dword;
  74. begin
  75. Result:=CurrentTM.SuspendThread(ThreadHandle);
  76. end;
  77. function ResumeThread (threadHandle : dword) : dword;
  78. begin
  79. Result:=CurrentTM.ResumeThread(ThreadHandle);
  80. end;
  81. procedure ThreadSwitch;
  82. begin
  83. CurrentTM.ThreadSwitch;
  84. end;
  85. function KillThread (threadHandle : dword) : dword;
  86. begin
  87. Result:=CurrentTM.KillThread(ThreadHandle);
  88. end;
  89. function WaitForThreadTerminate (threadHandle : dword; TimeoutMs : longint) : dword;
  90. begin
  91. Result:=CurrentTM.WaitForThreadTerminate(ThreadHandle,TimeOutMS);
  92. end;
  93. function ThreadSetPriority (threadHandle : dword; Prio: longint): boolean;
  94. begin
  95. Result:=CurrentTM.ThreadSetPriority(ThreadHandle,Prio);
  96. end;
  97. function ThreadGetPriority (threadHandle : dword): Integer;
  98. begin
  99. Result:=CurrentTM.ThreadGetPriority(ThreadHandle);
  100. end;
  101. function GetCurrentThreadId : dword;
  102. begin
  103. Result:=CurrentTM.GetCurrentThreadID();
  104. end;
  105. procedure InitCriticalSection(var cs : TRTLCriticalSection);
  106. begin
  107. CurrentTM.InitCriticalSection(cs);
  108. end;
  109. procedure DoneCriticalsection(var cs : TRTLCriticalSection);
  110. begin
  111. CurrentTM.DoneCriticalSection(cs);
  112. end;
  113. procedure EnterCriticalsection(var cs : TRTLCriticalSection);
  114. begin
  115. CurrentTM.EnterCriticalSection(cs);
  116. end;
  117. procedure LeaveCriticalsection(var cs : TRTLCriticalSection);
  118. begin
  119. CurrentTM.LeaveCriticalSection(cs);
  120. end;
  121. Function GetThreadManager(Var TM : TThreadManager) : Boolean;
  122. begin
  123. TM:=CurrentTM;
  124. Result:=True;
  125. end;
  126. Function SetThreadManager(Const NewTM : TThreadManager; Var OldTM : TThreadManager) : Boolean;
  127. begin
  128. GetThreadManager(OldTM);
  129. Result:=SetThreadManager(NewTM);
  130. end;
  131. Function SetThreadManager(Const NewTM : TThreadManager) : Boolean;
  132. begin
  133. Result:=True;
  134. If Assigned(CurrentTM.DoneManager) then
  135. Result:=CurrentTM.DoneManager();
  136. If Result then
  137. begin
  138. CurrentTM:=NewTM;
  139. If Assigned(CurrentTM.InitManager) then
  140. Result:=CurrentTM.InitManager();
  141. end;
  142. end;
  143. { ---------------------------------------------------------------------
  144. ThreadManager which gives run-time error. Use if no thread support.
  145. ---------------------------------------------------------------------}
  146. Resourcestring
  147. SNoThreads = 'This binary has no thread support compiled in.';
  148. SRecompileWithThreads = 'Recompile the application with a thread-driver in the program uses clause.';
  149. Procedure NoThreadError;
  150. begin
  151. If IsConsole then
  152. begin
  153. Writeln(StdErr,SNoThreads);
  154. Writeln(StdErr,SRecompileWithThreads);
  155. end;
  156. RunError(232)
  157. end;
  158. function NoBeginThread(sa : Pointer;stacksize : dword;
  159. ThreadFunction : tthreadfunc;p : pointer;
  160. creationFlags : dword; var ThreadId : DWord) : DWord;
  161. begin
  162. NoThreadError;
  163. end;
  164. procedure NoEndThread(ExitCode : DWord);
  165. begin
  166. NoThreadError;
  167. end;
  168. function NoThreadHandler (threadHandle : dword) : dword;
  169. begin
  170. NoThreadError;
  171. end;
  172. procedure NoThreadSwitch; {give time to other threads}
  173. begin
  174. NoThreadError;
  175. end;
  176. function NoWaitForThreadTerminate (threadHandle : dword; TimeoutMs : longint) : dword; {0=no timeout}
  177. begin
  178. NoThreadError;
  179. end;
  180. function NoThreadSetPriority (threadHandle : dword; Prio: longint): boolean; {-15..+15, 0=normal}
  181. begin
  182. NoThreadError;
  183. end;
  184. function NoThreadGetPriority (threadHandle : dword): Integer;
  185. begin
  186. NoThreadError;
  187. end;
  188. function NoGetCurrentThreadId : dword;
  189. begin
  190. NoThreadError;
  191. end;
  192. procedure NoCriticalSection(var CS);
  193. begin
  194. NoThreadError;
  195. end;
  196. procedure NoInitThreadvar(var offset : dword;size : dword);
  197. begin
  198. NoThreadError;
  199. end;
  200. function NoRelocateThreadvar(offset : dword) : pointer;
  201. begin
  202. NoThreadError;
  203. end;
  204. procedure NoAllocateThreadVars;
  205. begin
  206. NoThreadError;
  207. end;
  208. procedure NoReleaseThreadVars;
  209. begin
  210. NoThreadError;
  211. end;
  212. Var
  213. NoThreadManager : TThreadManager;
  214. Procedure SetNoThreadManager;
  215. begin
  216. With NoThreadManager do
  217. begin
  218. InitManager :=Nil;
  219. DoneManager :=Nil;
  220. BeginThread :=@NoBeginThread;
  221. EndThread :=@NoEndThread;
  222. SuspendThread :=@NoThreadHandler;
  223. ResumeThread :=@NoThreadHandler;
  224. KillThread :=@NoThreadHandler;
  225. ThreadSwitch :=@NoThreadSwitch;
  226. WaitForThreadTerminate :=@NoWaitForThreadTerminate;
  227. ThreadSetPriority :=@NoThreadSetPriority;
  228. ThreadGetPriority :=@NoThreadGetPriority;
  229. GetCurrentThreadId :=@NoGetCurrentThreadId;
  230. InitCriticalSection :=@NoCriticalSection;
  231. DoneCriticalSection :=@NoCriticalSection;
  232. EnterCriticalSection :=@NoCriticalSection;
  233. LeaveCriticalSection :=@NoCriticalSection;
  234. InitThreadVar :=@NoInitThreadVar;
  235. RelocateThreadVar :=@NoRelocateThreadVar;
  236. AllocateThreadVars :=@NoAllocateThreadVars;
  237. ReleaseThreadVars :=@NoReleaseThreadVars;
  238. end;
  239. SetThreadManager(NoThreadManager);
  240. end;
  241. {
  242. $Log$
  243. Revision 1.8 2004-01-21 20:11:06 peter
  244. * fixed compile for unix
  245. Revision 1.7 2004/01/20 23:13:53 hajny
  246. * ExecuteProcess fixes, ProcessID and ThreadID added
  247. Revision 1.6 2003/11/29 17:33:09 michael
  248. + Removed dummy variable from SetNothreadManager
  249. Revision 1.5 2003/11/29 17:29:32 michael
  250. + Added overloaded version of SetThreadManager without old parameter
  251. Revision 1.4 2003/11/26 20:10:59 michael
  252. + New threadmanager implementation
  253. Revision 1.3 2002/11/14 12:40:06 jonas
  254. * the BeginThread() variant that allowed you to specify the stacksize
  255. still passed DefaultStackSize to the OS-specific routines
  256. Revision 1.2 2002/10/16 19:04:27 michael
  257. + More system-independent thread routines
  258. Revision 1.1 2002/10/14 19:39:17 peter
  259. * threads unit added for thread support
  260. }