threadh.inc 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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. Procedure SetNoThreadManager;
  71. // Needs to be exported, so the manager can call it.
  72. {$ifdef HASTHREADVAR}
  73. procedure InitThreadVars(RelocProc : Pointer);
  74. {$endif HASTHREADVAR}
  75. procedure InitThread(stklen:cardinal);
  76. {*****************************************************************************
  77. Multithread Handling
  78. *****************************************************************************}
  79. function BeginThread(sa : Pointer;stacksize : dword;
  80. ThreadFunction : tthreadfunc;p : pointer;creationFlags : dword;
  81. var ThreadId : THandle) : DWord;
  82. {$ifndef CPU64}
  83. {$ifndef unix}
  84. { Delphi uses a longint for threadid }
  85. function BeginThread(sa : Pointer;stacksize : dword;
  86. ThreadFunction : tthreadfunc;p : pointer;creationFlags : dword;
  87. var ThreadId : Longint) : DWord;
  88. {$endif unix}
  89. {$endif CPU64}
  90. { add some simplfied forms which make lifer easier and porting }
  91. { to other OSes too ... }
  92. function BeginThread(ThreadFunction : tthreadfunc) : DWord;
  93. function BeginThread(ThreadFunction : tthreadfunc;p : pointer) : DWord;
  94. function BeginThread(ThreadFunction : tthreadfunc;p : pointer; var ThreadId : THandle) : DWord;
  95. {$ifndef CPU64}
  96. {$ifndef unix}
  97. function BeginThread(ThreadFunction : tthreadfunc;p : pointer; var ThreadId : Longint) : DWord;
  98. {$endif unix}
  99. {$endif CPU64}
  100. procedure EndThread(ExitCode : DWord);
  101. procedure EndThread;
  102. {some thread support functions}
  103. function SuspendThread (threadHandle : dword) : dword;
  104. function ResumeThread (threadHandle : dword) : dword;
  105. procedure ThreadSwitch; {give time to other threads}
  106. function KillThread (threadHandle : dword) : dword;
  107. function WaitForThreadTerminate (threadHandle : dword; TimeoutMs : longint) : dword; {0=no timeout}
  108. function ThreadSetPriority (threadHandle : dword; Prio: longint): boolean; {-15..+15, 0=normal}
  109. function ThreadGetPriority (threadHandle : dword): Integer;
  110. function GetCurrentThreadId : dword;
  111. { this allows to do a lot of things in MT safe way }
  112. { it is also used to make the heap management }
  113. { thread safe }
  114. procedure InitCriticalSection(var cs : TRTLCriticalSection);
  115. procedure DoneCriticalsection(var cs : TRTLCriticalSection);
  116. procedure EnterCriticalsection(var cs : TRTLCriticalSection);
  117. procedure LeaveCriticalsection(var cs : TRTLCriticalSection);
  118. function BasicEventCreate(EventAttributes : Pointer; AManualReset,InitialState : Boolean;const Name : ansistring):pEventState;
  119. procedure basiceventdestroy(state:peventstate);
  120. procedure basiceventResetEvent(state:peventstate);
  121. procedure basiceventSetEvent(state:peventstate);
  122. function basiceventWaitFor(Timeout : Cardinal;state:peventstate) : longint;
  123. {
  124. $Log$
  125. Revision 1.18 2004-05-23 20:26:20 marco
  126. * wrappers and nothread prototypes for the basic* functions
  127. Revision 1.17 2004/05/23 15:30:29 marco
  128. * first try basicevent
  129. Revision 1.16 2004/02/22 23:22:49 florian
  130. * fixed BeginThread on unix
  131. Revision 1.15 2004/02/22 16:48:39 florian
  132. * several 64 bit issues fixed
  133. Revision 1.14 2003/11/29 17:29:32 michael
  134. + Added overloaded version of SetThreadManager without old parameter
  135. Revision 1.13 2003/11/27 10:28:41 michael
  136. + Patch from peter to fix make cycle
  137. Revision 1.12 2003/11/26 20:10:59 michael
  138. + New threadmanager implementation
  139. Revision 1.11 2003/10/01 21:00:09 peter
  140. * GetCurrentThreadHandle renamed to GetCurrentThreadId
  141. Revision 1.10 2003/03/27 17:14:27 armin
  142. * more platform independent thread routines, needs to be implemented for unix
  143. Revision 1.9 2002/10/16 19:04:27 michael
  144. + More system-independent thread routines
  145. Revision 1.8 2002/10/14 19:39:17 peter
  146. * threads unit added for thread support
  147. Revision 1.7 2002/09/07 15:07:46 peter
  148. * old logs removed and tabs fixed
  149. Revision 1.6 2002/07/28 20:43:48 florian
  150. * several fixes for linux/powerpc
  151. * several fixes to MT
  152. }