threadh.inc 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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. This File contains the OS indenpendend declartions for multi
  6. threading support in FPC
  7. See the File COPYING.FPC, included in this distribution,
  8. for details about the copyright.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  12. **********************************************************************}
  13. const
  14. DefaultStackSize = 32768; { including 16384 margin for stackchecking }
  15. type
  16. PEventState = pointer;
  17. PRTLEvent = pointer; // Windows=thandle, other=pointer to record.
  18. TThreadFunc = function(parameter : pointer) : ptrint;
  19. trtlmethod = procedure of object;
  20. // Function prototypes for TThreadManager Record.
  21. TBeginThreadHandler = Function (sa : Pointer;stacksize : dword; ThreadFunction : tthreadfunc;p : pointer;creationFlags : dword; var ThreadId : THandle) : DWord;
  22. TEndThreadHandler = Procedure (ExitCode : DWord);
  23. // Used for Suspend/Resume/Kill
  24. TThreadHandler = Function (threadHandle : dword) : dword;
  25. TThreadSwitchHandler = Procedure;
  26. TWaitForThreadTerminateHandler = Function (threadHandle : dword; TimeoutMs : longint) : dword; {0=no timeout}
  27. TThreadSetPriorityHandler = Function (threadHandle : dword; Prio: longint): boolean; {-15..+15, 0=normal}
  28. TThreadGetPriorityHandler = Function (threadHandle : dword): Integer;
  29. TGetCurrentThreadIdHandler = Function : dword;
  30. TCriticalSectionHandler = Procedure (var cs);
  31. TInitThreadVarHandler = Procedure(var offset : dword;size : dword);
  32. TRelocateThreadVarHandler = Function(offset : dword) : pointer;
  33. TAllocateThreadVarsHandler = Procedure;
  34. TReleaseThreadVarsHandler = Procedure;
  35. TBasicEventHandler = procedure(state:peventstate);
  36. TBasicEventWaitForHandler = function (timeout:cardinal;state:peventstate):longint;
  37. TBasicEventCreateHandler = function (EventAttributes :Pointer; AManualReset,InitialState : Boolean;const Name:ansistring):pEventState;
  38. TRTLEventHandler = procedure(AEvent:PRTLEvent);
  39. TRTLCreateEventHandler = function:PRTLEvent;
  40. TRTLEventSyncHandler = procedure (m:trtlmethod;p:tprocedure);
  41. TRTLCheckSyncUnixHandler = procedure;
  42. // TThreadManager interface.
  43. TThreadManager = Record
  44. InitManager : Function : Boolean;
  45. DoneManager : Function : Boolean;
  46. BeginThread : TBeginThreadHandler;
  47. EndThread : TEndThreadHandler;
  48. SuspendThread : TThreadHandler;
  49. ResumeThread : TThreadHandler;
  50. KillThread : TThreadHandler;
  51. ThreadSwitch : TThreadSwitchHandler;
  52. WaitForThreadTerminate : TWaitForThreadTerminateHandler;
  53. ThreadSetPriority : TThreadSetPriorityHandler;
  54. ThreadGetPriority : TThreadGetPriorityHandler;
  55. GetCurrentThreadId : TGetCurrentThreadIdHandler;
  56. InitCriticalSection : TCriticalSectionHandler;
  57. DoneCriticalSection : TCriticalSectionHandler;
  58. EnterCriticalSection : TCriticalSectionHandler;
  59. LeaveCriticalSection : TCriticalSectionHandler;
  60. InitThreadVar : TInitThreadVarHandler;
  61. RelocateThreadVar : TRelocateThreadVarHandler;
  62. AllocateThreadVars : TAllocateThreadVarsHandler;
  63. ReleaseThreadVars : TReleaseThreadVarsHandler;
  64. BasicEventCreate : TBasicEventCreateHandler; // left in for a while.
  65. BasicEventDestroy : TBasicEventHandler; // we might need BasicEvent
  66. BasicEventResetEvent : TBasicEventHandler; // for a real TEvent
  67. BasicEventSetEvent : TBasicEventHandler;
  68. BasiceventWaitFOr : TBasicEventWaitForHandler;
  69. RTLEventCreate : TRTLCreateEventHandler;
  70. RTLEventDestroy : TRTLEventHandler;
  71. RTLEventSetEvent : TRTLEventHandler;
  72. RTLEventStartWait : TRTLEventHandler;
  73. RTLEventWaitFor : TRTLEventHandler;
  74. RTLEventSync : TRTLEventSyncHandler;
  75. RTLChkSyncUnix : TRTLCheckSyncUnixHandler;
  76. end;
  77. {*****************************************************************************
  78. Thread Handler routines
  79. *****************************************************************************}
  80. Function GetThreadManager(Var TM : TThreadManager) : Boolean;
  81. Function SetThreadManager(Const NewTM : TThreadManager; Var OldTM : TThreadManager) : Boolean;
  82. Function SetThreadManager(Const NewTM : TThreadManager) : Boolean;
  83. {$ifndef DISABLE_NO_THREAD_MANAGER}
  84. Procedure SetNoThreadManager;
  85. {$endif DISABLE_NO_THREAD_MANAGER}
  86. // Needs to be exported, so the manager can call it.
  87. {$ifdef HASTHREADVAR}
  88. procedure InitThreadVars(RelocProc : Pointer);
  89. {$endif HASTHREADVAR}
  90. procedure InitThread(stklen:cardinal);
  91. {*****************************************************************************
  92. Multithread Handling
  93. *****************************************************************************}
  94. function BeginThread(sa : Pointer;stacksize : dword;
  95. ThreadFunction : tthreadfunc;p : pointer;creationFlags : dword;
  96. var ThreadId : THandle) : DWord;
  97. {$ifndef CPU64}
  98. {$ifndef unix}
  99. {$endif unix}
  100. {$endif CPU64}
  101. { add some simplfied forms which make lifer easier and porting }
  102. { to other OSes too ... }
  103. function BeginThread(ThreadFunction : tthreadfunc) : DWord;
  104. function BeginThread(ThreadFunction : tthreadfunc;p : pointer) : DWord;
  105. function BeginThread(ThreadFunction : tthreadfunc;p : pointer; var ThreadId : THandle) : DWord;
  106. {$ifndef CPU64}
  107. {$ifndef unix}
  108. {$endif unix}
  109. {$endif CPU64}
  110. procedure EndThread(ExitCode : DWord);
  111. procedure EndThread;
  112. {some thread support functions}
  113. function SuspendThread (threadHandle : dword) : dword;
  114. function ResumeThread (threadHandle : dword) : dword;
  115. procedure ThreadSwitch; {give time to other threads}
  116. function KillThread (threadHandle : dword) : dword;
  117. function WaitForThreadTerminate (threadHandle : dword; TimeoutMs : longint) : dword; {0=no timeout}
  118. function ThreadSetPriority (threadHandle : dword; Prio: longint): boolean; {-15..+15, 0=normal}
  119. function ThreadGetPriority (threadHandle : dword): Integer;
  120. function GetCurrentThreadId : dword;
  121. { this allows to do a lot of things in MT safe way }
  122. { it is also used to make the heap management }
  123. { thread safe }
  124. procedure InitCriticalSection(var cs : TRTLCriticalSection);
  125. procedure DoneCriticalsection(var cs : TRTLCriticalSection);
  126. procedure EnterCriticalsection(var cs : TRTLCriticalSection);
  127. procedure LeaveCriticalsection(var cs : TRTLCriticalSection);
  128. function BasicEventCreate(EventAttributes : Pointer; AManualReset,InitialState : Boolean;const Name : ansistring):pEventState;
  129. procedure basiceventdestroy(state:peventstate);
  130. procedure basiceventResetEvent(state:peventstate);
  131. procedure basiceventSetEvent(state:peventstate);
  132. function basiceventWaitFor(Timeout : Cardinal;state:peventstate) : longint;
  133. function RTLEventCreate :PRTLEvent;
  134. procedure RTLeventdestroy(state:pRTLEvent);
  135. procedure RTLeventSetEvent(state:pRTLEvent);
  136. procedure RTLeventStartWait(state:pRTLEvent);
  137. procedure RTLeventWaitFor(state:pRTLEvent);
  138. procedure RTLeventsync(m:trtlmethod;p:tprocedure);
  139. procedure RTLchecksynchronize;
  140. {
  141. $Log$
  142. Revision 1.25 2005-01-21 21:43:12 armin
  143. * applied patch to compile go32v2 from Tomas (tested by John)
  144. Revision 1.24 2004/12/28 14:20:03 marco
  145. * tthread patch from neli
  146. Revision 1.23 2004/12/27 15:28:40 marco
  147. * checksynchronize now in interface win32 uses the default impl.
  148. unix uses systhrds, rest empty implementation.
  149. Revision 1.22 2004/12/23 15:08:58 marco
  150. * 2nd synchronize attempt. cthreads<->systhrds difference was not ok, but
  151. only showed on make install should be fixed now.
  152. Revision 1.21 2004/12/22 21:29:24 marco
  153. * rtlevent kraam. Checked (compile): Linux, FreeBSD, Darwin, Windows
  154. Check work: ask Neli.
  155. Revision 1.20 2004/12/12 14:30:27 peter
  156. * x86_64 updates
  157. Revision 1.19 2004/09/19 18:55:30 armin
  158. * added define DISABLE_NO_THREAD_MANAGER to avoid warnings if thread manager is always present
  159. Revision 1.18 2004/05/23 20:26:20 marco
  160. * wrappers and nothread prototypes for the basic* functions
  161. Revision 1.17 2004/05/23 15:30:29 marco
  162. * first try basicevent
  163. Revision 1.16 2004/02/22 23:22:49 florian
  164. * fixed BeginThread on unix
  165. Revision 1.15 2004/02/22 16:48:39 florian
  166. * several 64 bit issues fixed
  167. Revision 1.14 2003/11/29 17:29:32 michael
  168. + Added overloaded version of SetThreadManager without old parameter
  169. Revision 1.13 2003/11/27 10:28:41 michael
  170. + Patch from peter to fix make cycle
  171. Revision 1.12 2003/11/26 20:10:59 michael
  172. + New threadmanager implementation
  173. Revision 1.11 2003/10/01 21:00:09 peter
  174. * GetCurrentThreadHandle renamed to GetCurrentThreadId
  175. Revision 1.10 2003/03/27 17:14:27 armin
  176. * more platform independent thread routines, needs to be implemented for unix
  177. Revision 1.9 2002/10/16 19:04:27 michael
  178. + More system-independent thread routines
  179. Revision 1.8 2002/10/14 19:39:17 peter
  180. * threads unit added for thread support
  181. Revision 1.7 2002/09/07 15:07:46 peter
  182. * old logs removed and tabs fixed
  183. Revision 1.6 2002/07/28 20:43:48 florian
  184. * several fixes for linux/powerpc
  185. * several fixes to MT
  186. }