threadh.inc 9.3 KB

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