thread.inc 8.4 KB

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