threadh.inc 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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. TThreadFunc = function(parameter : pointer) : longint;
  18. // Function prototypes for TThreadManager Record.
  19. TBeginThreadHandler = Function (sa : Pointer;stacksize : dword; ThreadFunction : tthreadfunc;p : pointer;creationFlags : dword; var ThreadId : THandle) : DWord;
  20. TEndThreadHandler = Procedure (ExitCode : DWord);
  21. // Used for Suspend/Resume/Kill
  22. TThreadHandler = Function (threadHandle : dword) : dword;
  23. TThreadSwitchHandler = Procedure;
  24. TWaitForThreadTerminateHandler = Function (threadHandle : dword; TimeoutMs : longint) : dword; {0=no timeout}
  25. TThreadSetPriorityHandler = Function (threadHandle : dword; Prio: longint): boolean; {-15..+15, 0=normal}
  26. TThreadGetPriorityHandler = Function (threadHandle : dword): Integer;
  27. TGetCurrentThreadIdHandler = Function : dword;
  28. TCriticalSectionHandler = Procedure (var cs);
  29. TInitThreadVarHandler = Procedure(var offset : dword;size : dword);
  30. TRelocateThreadVarHandler = Function(offset : dword) : pointer;
  31. TAllocateThreadVarsHandler = Procedure;
  32. TReleaseThreadVarsHandler = Procedure;
  33. TBasicEventHandler = procedure(state:peventstate);
  34. TBasicEventWaitForHandler = function (timeout:cardinal;state:peventstate):longint;
  35. TBasicEventCreateHandler = function (EventAttributes :Pointer; AManualReset,InitialState : Boolean;const Name:ansistring):pEventState;
  36. // TThreadManager interface.
  37. TThreadManager = Record
  38. InitManager : Function : Boolean;
  39. DoneManager : Function : Boolean;
  40. BeginThread : TBeginThreadHandler;
  41. EndThread : TEndThreadHandler;
  42. SuspendThread : TThreadHandler;
  43. ResumeThread : TThreadHandler;
  44. KillThread : TThreadHandler;
  45. ThreadSwitch : TThreadSwitchHandler;
  46. WaitForThreadTerminate : TWaitForThreadTerminateHandler;
  47. ThreadSetPriority : TThreadSetPriorityHandler;
  48. ThreadGetPriority : TThreadGetPriorityHandler;
  49. GetCurrentThreadId : TGetCurrentThreadIdHandler;
  50. InitCriticalSection : TCriticalSectionHandler;
  51. DoneCriticalSection : TCriticalSectionHandler;
  52. EnterCriticalSection : TCriticalSectionHandler;
  53. LeaveCriticalSection : TCriticalSectionHandler;
  54. InitThreadVar : TInitThreadVarHandler;
  55. RelocateThreadVar : TRelocateThreadVarHandler;
  56. AllocateThreadVars : TAllocateThreadVarsHandler;
  57. ReleaseThreadVars : TReleaseThreadVarsHandler;
  58. BasicEventCreate : TBasicEventCreateHandler;
  59. BasicEventDestroy : TBasicEventHandler;
  60. BasicEventResetEvent : TBasicEventHandler;
  61. BasicEventSetEvent : TBasicEventHandler;
  62. BasiceventWaitFOr : TBasicEventWaitForHandler;
  63. end;
  64. {*****************************************************************************
  65. Thread Handler routines
  66. *****************************************************************************}
  67. Function GetThreadManager(Var TM : TThreadManager) : Boolean;
  68. Function SetThreadManager(Const NewTM : TThreadManager; Var OldTM : TThreadManager) : Boolean;
  69. Function SetThreadManager(Const NewTM : TThreadManager) : Boolean;
  70. {$ifndef DISABLE_NO_THREAD_MANAGER}
  71. Procedure SetNoThreadManager;
  72. {$endif DISABLE_NO_THREAD_MANAGER}
  73. // Needs to be exported, so the manager can call it.
  74. {$ifdef HASTHREADVAR}
  75. procedure InitThreadVars(RelocProc : Pointer);
  76. {$endif HASTHREADVAR}
  77. procedure InitThread(stklen:cardinal);
  78. {*****************************************************************************
  79. Multithread Handling
  80. *****************************************************************************}
  81. function BeginThread(sa : Pointer;stacksize : dword;
  82. ThreadFunction : tthreadfunc;p : pointer;creationFlags : dword;
  83. var ThreadId : THandle) : DWord;
  84. {$ifndef CPU64}
  85. {$ifndef unix}
  86. { Delphi uses a longint for threadid }
  87. function BeginThread(sa : Pointer;stacksize : dword;
  88. ThreadFunction : tthreadfunc;p : pointer;creationFlags : dword;
  89. var ThreadId : Longint) : DWord;
  90. {$endif unix}
  91. {$endif CPU64}
  92. { add some simplfied forms which make lifer easier and porting }
  93. { to other OSes too ... }
  94. function BeginThread(ThreadFunction : tthreadfunc) : DWord;
  95. function BeginThread(ThreadFunction : tthreadfunc;p : pointer) : DWord;
  96. function BeginThread(ThreadFunction : tthreadfunc;p : pointer; var ThreadId : THandle) : DWord;
  97. {$ifndef CPU64}
  98. {$ifndef unix}
  99. function BeginThread(ThreadFunction : tthreadfunc;p : pointer; var ThreadId : Longint) : DWord;
  100. {$endif unix}
  101. {$endif CPU64}
  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): Integer;
  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. {
  126. $Log$
  127. Revision 1.19 2004-09-19 18:55:30 armin
  128. * added define DISABLE_NO_THREAD_MANAGER to avoid warnings if thread manager is always present
  129. Revision 1.18 2004/05/23 20:26:20 marco
  130. * wrappers and nothread prototypes for the basic* functions
  131. Revision 1.17 2004/05/23 15:30:29 marco
  132. * first try basicevent
  133. Revision 1.16 2004/02/22 23:22:49 florian
  134. * fixed BeginThread on unix
  135. Revision 1.15 2004/02/22 16:48:39 florian
  136. * several 64 bit issues fixed
  137. Revision 1.14 2003/11/29 17:29:32 michael
  138. + Added overloaded version of SetThreadManager without old parameter
  139. Revision 1.13 2003/11/27 10:28:41 michael
  140. + Patch from peter to fix make cycle
  141. Revision 1.12 2003/11/26 20:10:59 michael
  142. + New threadmanager implementation
  143. Revision 1.11 2003/10/01 21:00:09 peter
  144. * GetCurrentThreadHandle renamed to GetCurrentThreadId
  145. Revision 1.10 2003/03/27 17:14:27 armin
  146. * more platform independent thread routines, needs to be implemented for unix
  147. Revision 1.9 2002/10/16 19:04:27 michael
  148. + More system-independent thread routines
  149. Revision 1.8 2002/10/14 19:39:17 peter
  150. * threads unit added for thread support
  151. Revision 1.7 2002/09/07 15:07:46 peter
  152. * old logs removed and tabs fixed
  153. Revision 1.6 2002/07/28 20:43:48 florian
  154. * several fixes for linux/powerpc
  155. * several fixes to MT
  156. }