threadh.inc 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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 : TThreadID) : DWord;
  22. TEndThreadHandler = Procedure (ExitCode : DWord);
  23. // Used for Suspend/Resume/Kill
  24. TThreadHandler = Function (threadHandle : TThreadID) : dword;
  25. TThreadSwitchHandler = Procedure;
  26. TWaitForThreadTerminateHandler = Function (threadHandle : TThreadID; TimeoutMs : longint) : dword; {0=no timeout}
  27. TThreadSetPriorityHandler = Function (threadHandle : TThreadID; Prio: longint): boolean; {-15..+15, 0=normal}
  28. TThreadGetPriorityHandler = Function (threadHandle : TThreadID): longint;
  29. TGetCurrentThreadIdHandler = Function : TThreadID;
  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. TRTLEventHandlerTimeout = procedure(AEvent:PRTLEvent;timeout : longint);
  40. TRTLCreateEventHandler = function:PRTLEvent;
  41. TRTLEventSyncHandler = procedure (m:trtlmethod;p:tprocedure);
  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. RTLEventResetEvent : TRTLEventHandler;
  73. RTLEventStartWait : TRTLEventHandler;
  74. RTLEventWaitFor : TRTLEventHandler;
  75. RTLEventSync : TRTLEventSyncHandler;
  76. RTLEventWaitForTimeout : TRTLEventHandlerTimeout;
  77. end;
  78. {*****************************************************************************
  79. Thread Handler routines
  80. *****************************************************************************}
  81. Function GetThreadManager(Var TM : TThreadManager) : Boolean;
  82. Function SetThreadManager(Const NewTM : TThreadManager; Var OldTM : TThreadManager) : Boolean;
  83. Function SetThreadManager(Const NewTM : TThreadManager) : Boolean;
  84. {$ifndef DISABLE_NO_THREAD_MANAGER}
  85. Procedure SetNoThreadManager;
  86. {$endif DISABLE_NO_THREAD_MANAGER}
  87. // Needs to be exported, so the manager can call it.
  88. {$ifdef HASTHREADVAR}
  89. procedure InitThreadVars(RelocProc : Pointer);
  90. {$endif HASTHREADVAR}
  91. procedure InitThread(stklen:cardinal);
  92. {*****************************************************************************
  93. Multithread Handling
  94. *****************************************************************************}
  95. function BeginThread(sa : Pointer;stacksize : dword;
  96. ThreadFunction : tthreadfunc;p : pointer;creationFlags : dword;
  97. var ThreadId : THandle) : DWord;
  98. { add some simplfied forms which make lifer easier and porting }
  99. { to other OSes too ... }
  100. function BeginThread(ThreadFunction : tthreadfunc) : DWord;
  101. function BeginThread(ThreadFunction : tthreadfunc;p : pointer) : DWord;
  102. function BeginThread(ThreadFunction : tthreadfunc;p : pointer; var ThreadId : TThreadID) : DWord;
  103. procedure EndThread(ExitCode : DWord);
  104. procedure EndThread;
  105. {some thread support functions}
  106. function SuspendThread (threadHandle : TThreadID) : dword;
  107. function ResumeThread (threadHandle : TThreadID) : dword;
  108. procedure ThreadSwitch; {give time to other threads}
  109. function KillThread (threadHandle : TThreadID) : dword;
  110. function WaitForThreadTerminate (threadHandle : TThreadID; TimeoutMs : longint) : dword; {0=no timeout}
  111. function ThreadSetPriority (threadHandle : TThreadID; Prio: longint): boolean; {-15..+15, 0=normal}
  112. function ThreadGetPriority (threadHandle : TThreadID): longint;
  113. function GetCurrentThreadId : TThreadID;
  114. { this allows to do a lot of things in MT safe way }
  115. { it is also used to make the heap management }
  116. { thread safe }
  117. procedure InitCriticalSection(var cs : TRTLCriticalSection);
  118. procedure DoneCriticalsection(var cs : TRTLCriticalSection);
  119. procedure EnterCriticalsection(var cs : TRTLCriticalSection);
  120. procedure LeaveCriticalsection(var cs : TRTLCriticalSection);
  121. function BasicEventCreate(EventAttributes : Pointer; AManualReset,InitialState : Boolean;const Name : ansistring):pEventState;
  122. procedure basiceventdestroy(state:peventstate);
  123. procedure basiceventResetEvent(state:peventstate);
  124. procedure basiceventSetEvent(state:peventstate);
  125. function basiceventWaitFor(Timeout : Cardinal;state:peventstate) : longint;
  126. function RTLEventCreate :PRTLEvent;
  127. procedure RTLeventdestroy(state:pRTLEvent);
  128. procedure RTLeventSetEvent(state:pRTLEvent);
  129. procedure RTLeventResetEvent(state:pRTLEvent);
  130. procedure RTLeventStartWait(state:pRTLEvent);
  131. procedure RTLeventWaitFor(state:pRTLEvent);
  132. procedure RTLeventWaitFor(state:pRTLEvent;timeout : longint);
  133. procedure RTLeventsync(m:trtlmethod;p:tprocedure);
  134. {
  135. $Log$
  136. Revision 1.31 2005-04-14 20:42:14 florian
  137. * fixed more TThreadID stuff
  138. Revision 1.30 2005/04/13 20:10:50 florian
  139. + TThreadID
  140. Revision 1.29 2005/04/09 17:26:08 florian
  141. + classes.mainthreadid is set now
  142. + rtleventresetevent
  143. + rtleventwairfor with timeout
  144. + checksynchronize with timeout
  145. * race condition in synchronize fixed
  146. Revision 1.28 2005/02/25 22:02:48 florian
  147. * another "transfer to linux"-commit
  148. Revision 1.27 2005/02/14 17:13:29 peter
  149. * truncate log
  150. Revision 1.26 2005/02/06 11:20:52 peter
  151. * threading in system unit
  152. * removed systhrds unit
  153. Revision 1.25 2005/01/21 21:43:12 armin
  154. * applied patch to compile go32v2 from Tomas (tested by John)
  155. }