threadh.inc 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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. { Delphi uses a longint for threadid }
  100. function BeginThread(sa : Pointer;stacksize : dword;
  101. ThreadFunction : tthreadfunc;p : pointer;creationFlags : dword;
  102. var ThreadId : Longint) : DWord;
  103. {$endif unix}
  104. {$endif CPU64}
  105. { add some simplfied forms which make lifer easier and porting }
  106. { to other OSes too ... }
  107. function BeginThread(ThreadFunction : tthreadfunc) : DWord;
  108. function BeginThread(ThreadFunction : tthreadfunc;p : pointer) : DWord;
  109. function BeginThread(ThreadFunction : tthreadfunc;p : pointer; var ThreadId : THandle) : DWord;
  110. {$ifndef CPU64}
  111. {$ifndef unix}
  112. function BeginThread(ThreadFunction : tthreadfunc;p : pointer; var ThreadId : Longint) : DWord;
  113. {$endif unix}
  114. {$endif CPU64}
  115. procedure EndThread(ExitCode : DWord);
  116. procedure EndThread;
  117. {some thread support functions}
  118. function SuspendThread (threadHandle : dword) : dword;
  119. function ResumeThread (threadHandle : dword) : dword;
  120. procedure ThreadSwitch; {give time to other threads}
  121. function KillThread (threadHandle : dword) : dword;
  122. function WaitForThreadTerminate (threadHandle : dword; TimeoutMs : longint) : dword; {0=no timeout}
  123. function ThreadSetPriority (threadHandle : dword; Prio: longint): boolean; {-15..+15, 0=normal}
  124. function ThreadGetPriority (threadHandle : dword): Integer;
  125. function GetCurrentThreadId : dword;
  126. { this allows to do a lot of things in MT safe way }
  127. { it is also used to make the heap management }
  128. { thread safe }
  129. procedure InitCriticalSection(var cs : TRTLCriticalSection);
  130. procedure DoneCriticalsection(var cs : TRTLCriticalSection);
  131. procedure EnterCriticalsection(var cs : TRTLCriticalSection);
  132. procedure LeaveCriticalsection(var cs : TRTLCriticalSection);
  133. function BasicEventCreate(EventAttributes : Pointer; AManualReset,InitialState : Boolean;const Name : ansistring):pEventState;
  134. procedure basiceventdestroy(state:peventstate);
  135. procedure basiceventResetEvent(state:peventstate);
  136. procedure basiceventSetEvent(state:peventstate);
  137. function basiceventWaitFor(Timeout : Cardinal;state:peventstate) : longint;
  138. function RTLEventCreate :PRTLEvent;
  139. procedure RTLeventdestroy(state:pRTLEvent);
  140. procedure RTLeventSetEvent(state:pRTLEvent);
  141. procedure RTLeventStartWait(state:pRTLEvent);
  142. procedure RTLeventWaitFor(state:pRTLEvent);
  143. procedure RTLeventsync(m:trtlmethod;p:tprocedure);
  144. procedure RTLchecksynchronize;
  145. {
  146. $Log$
  147. Revision 1.24 2004-12-28 14:20:03 marco
  148. * tthread patch from neli
  149. Revision 1.23 2004/12/27 15:28:40 marco
  150. * checksynchronize now in interface win32 uses the default impl.
  151. unix uses systhrds, rest empty implementation.
  152. Revision 1.22 2004/12/23 15:08:58 marco
  153. * 2nd synchronize attempt. cthreads<->systhrds difference was not ok, but
  154. only showed on make install should be fixed now.
  155. Revision 1.21 2004/12/22 21:29:24 marco
  156. * rtlevent kraam. Checked (compile): Linux, FreeBSD, Darwin, Windows
  157. Check work: ask Neli.
  158. Revision 1.20 2004/12/12 14:30:27 peter
  159. * x86_64 updates
  160. Revision 1.19 2004/09/19 18:55:30 armin
  161. * added define DISABLE_NO_THREAD_MANAGER to avoid warnings if thread manager is always present
  162. Revision 1.18 2004/05/23 20:26:20 marco
  163. * wrappers and nothread prototypes for the basic* functions
  164. Revision 1.17 2004/05/23 15:30:29 marco
  165. * first try basicevent
  166. Revision 1.16 2004/02/22 23:22:49 florian
  167. * fixed BeginThread on unix
  168. Revision 1.15 2004/02/22 16:48:39 florian
  169. * several 64 bit issues fixed
  170. Revision 1.14 2003/11/29 17:29:32 michael
  171. + Added overloaded version of SetThreadManager without old parameter
  172. Revision 1.13 2003/11/27 10:28:41 michael
  173. + Patch from peter to fix make cycle
  174. Revision 1.12 2003/11/26 20:10:59 michael
  175. + New threadmanager implementation
  176. Revision 1.11 2003/10/01 21:00:09 peter
  177. * GetCurrentThreadHandle renamed to GetCurrentThreadId
  178. Revision 1.10 2003/03/27 17:14:27 armin
  179. * more platform independent thread routines, needs to be implemented for unix
  180. Revision 1.9 2002/10/16 19:04:27 michael
  181. + More system-independent thread routines
  182. Revision 1.8 2002/10/14 19:39:17 peter
  183. * threads unit added for thread support
  184. Revision 1.7 2002/09/07 15:07:46 peter
  185. * old logs removed and tabs fixed
  186. Revision 1.6 2002/07/28 20:43:48 florian
  187. * several fixes for linux/powerpc
  188. * several fixes to MT
  189. }