threadh.inc 9.4 KB

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