threadh.inc 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. {
  2. This file is part of the Free Pascal Run time library.
  3. Copyright (c) 2000 by the Free Pascal development team
  4. This file contains the OS indenpendend declartions for multi
  5. threading support in FPC
  6. See the File COPYING.FPC, included in this distribution,
  7. for details about the copyright.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  11. **********************************************************************}
  12. const
  13. {$ifndef FPC_USE_SMALL_DEFAULTSTACKSIZE}
  14. { includes 16384 bytes margin for stackchecking }
  15. DefaultStackSize = 4*1024*1024;
  16. {$else i.e. FPC_USE_SMALL_DEFAULTSTACKSIZE}
  17. { Special value of Default stack size }
  18. DefaultStackSize = 16 * 1024;
  19. {$endif not FPC_USE_SMALL_DEFAULTSTACKSIZE}
  20. { every platform can have it's own implementation of GetCPUCount; use the define
  21. HAS_GETCPUCOUNT to disable the default implementation which simply returns 1 }
  22. function GetCPUCount: LongWord;
  23. property CPUCount: LongWord read GetCPUCount;
  24. type
  25. PEventState = pointer;
  26. PRTLEvent = type pointer; // Windows=thandle, other=pointer to record.
  27. TThreadFunc = function(parameter : pointer) : ptrint;
  28. trtlmethod = procedure of object;
  29. // Function prototypes for TThreadManager Record.
  30. TBeginThreadHandler = Function (sa : Pointer;stacksize : PtrUInt; ThreadFunction : tthreadfunc;p : pointer;creationFlags : dword; var ThreadId : TThreadID) : TThreadID;
  31. TEndThreadHandler = Procedure (ExitCode : DWord);
  32. // Used for Suspend/Resume/Kill
  33. TThreadHandler = Function (threadHandle : TThreadID) : dword;
  34. TThreadSwitchHandler = Procedure;
  35. TWaitForThreadTerminateHandler = Function (threadHandle : TThreadID; TimeoutMs : longint) : dword; {0=no timeout}
  36. TThreadSetPriorityHandler = Function (threadHandle : TThreadID; Prio: longint): boolean; {-15..+15, 0=normal}
  37. TThreadGetPriorityHandler = Function (threadHandle : TThreadID): longint;
  38. TGetCurrentThreadIdHandler = Function : TThreadID;
  39. TThreadSetThreadDebugNameHandlerA = procedure(threadHandle: TThreadID; const ThreadName: AnsiString);
  40. {$ifdef FPC_HAS_FEATURE_UNICODESTRINGS}
  41. TThreadSetThreadDebugNameHandlerU = procedure(threadHandle: TThreadID; const ThreadName: UnicodeString);
  42. {$endif FPC_HAS_FEATURE_UNICODESTRINGS}
  43. TCriticalSectionHandler = Procedure (var cs);
  44. TCriticalSectionHandlerTryEnter = function (var cs):longint;
  45. TInitThreadVarHandler = Procedure(var offset : {$ifdef cpu16}word{$else}dword{$endif};size : dword);
  46. TRelocateThreadVarHandler = Function(offset : {$ifdef cpu16}word{$else}dword{$endif}) : pointer;
  47. TAllocateThreadVarsHandler = Procedure;
  48. TReleaseThreadVarsHandler = Procedure;
  49. TBasicEventHandler = procedure(state:peventstate);
  50. TBasicEventWaitForHandler = function (timeout:cardinal;state:peventstate):longint;
  51. TBasicEventCreateHandler = function (EventAttributes :Pointer; AManualReset,InitialState : Boolean;const Name:ansistring):pEventState;
  52. TRTLEventHandler = procedure(AEvent:PRTLEvent);
  53. TRTLEventHandlerTimeout = procedure(AEvent:PRTLEvent;timeout : longint);
  54. TRTLCreateEventHandler = function:PRTLEvent;
  55. // semaphores stuff
  56. TSempahoreInitHandler = function: Pointer;
  57. TSemaphoreDestroyHandler = procedure (const sem: Pointer);
  58. TSemaphorePostHandler = procedure (const sem: Pointer);
  59. TSemaphoreWaitHandler = procedure (const sem: Pointer);
  60. // TThreadManager interface.
  61. TThreadManager = Record
  62. InitManager : Function : Boolean;
  63. DoneManager : Function : Boolean;
  64. BeginThread : TBeginThreadHandler;
  65. EndThread : TEndThreadHandler;
  66. SuspendThread : TThreadHandler;
  67. ResumeThread : TThreadHandler;
  68. KillThread : TThreadHandler;
  69. CloseThread : TThreadHandler;
  70. ThreadSwitch : TThreadSwitchHandler;
  71. WaitForThreadTerminate : TWaitForThreadTerminateHandler;
  72. ThreadSetPriority : TThreadSetPriorityHandler;
  73. ThreadGetPriority : TThreadGetPriorityHandler;
  74. GetCurrentThreadId : TGetCurrentThreadIdHandler;
  75. SetThreadDebugNameA : TThreadSetThreadDebugNameHandlerA;
  76. {$ifdef FPC_HAS_FEATURE_UNICODESTRINGS}
  77. SetThreadDebugNameU : TThreadSetThreadDebugNameHandlerU;
  78. {$endif FPC_HAS_FEATURE_UNICODESTRINGS}
  79. InitCriticalSection : TCriticalSectionHandler;
  80. DoneCriticalSection : TCriticalSectionHandler;
  81. EnterCriticalSection : TCriticalSectionHandler;
  82. TryEnterCriticalSection: TCriticalSectionHandlerTryEnter;
  83. LeaveCriticalSection : TCriticalSectionHandler;
  84. InitThreadVar : TInitThreadVarHandler;
  85. RelocateThreadVar : TRelocateThreadVarHandler;
  86. AllocateThreadVars : TAllocateThreadVarsHandler;
  87. ReleaseThreadVars : TReleaseThreadVarsHandler;
  88. BasicEventCreate : TBasicEventCreateHandler; // left in for a while.
  89. BasicEventDestroy : TBasicEventHandler; // we might need BasicEvent
  90. BasicEventResetEvent : TBasicEventHandler; // for a real TEvent
  91. BasicEventSetEvent : TBasicEventHandler;
  92. BasiceventWaitFOr : TBasicEventWaitForHandler;
  93. RTLEventCreate : TRTLCreateEventHandler;
  94. RTLEventDestroy : TRTLEventHandler;
  95. RTLEventSetEvent : TRTLEventHandler;
  96. RTLEventResetEvent : TRTLEventHandler;
  97. RTLEventWaitFor : TRTLEventHandler;
  98. RTLEventWaitForTimeout : TRTLEventHandlerTimeout;
  99. end;
  100. {*****************************************************************************
  101. Thread Handler routines
  102. *****************************************************************************}
  103. Function GetThreadManager(Var TM : TThreadManager) : Boolean;
  104. Function SetThreadManager(Const NewTM : TThreadManager; Var OldTM : TThreadManager) : Boolean;
  105. Function SetThreadManager(Const NewTM : TThreadManager) : Boolean;
  106. {$ifndef DISABLE_NO_THREAD_MANAGER}
  107. {$endif DISABLE_NO_THREAD_MANAGER}
  108. // Needs to be exported, so the manager can call it.
  109. {$ifndef FPC_SECTION_THREADVARS}
  110. procedure InitThreadVars(RelocProc : TRelocateThreadVarHandler);
  111. {$endif FPC_SECTION_THREADVARS}
  112. procedure InitThread(stklen:SizeUInt);
  113. procedure DoneThread;
  114. {*****************************************************************************
  115. Multithread Handling
  116. *****************************************************************************}
  117. function BeginThread(sa : Pointer;stacksize : SizeUInt;
  118. ThreadFunction : tthreadfunc;p : pointer;creationFlags : dword;
  119. var ThreadId : TThreadID) : TThreadID;
  120. { add some simplfied forms which make lifer easier and porting }
  121. { to other OSes too ... }
  122. function BeginThread(ThreadFunction : tthreadfunc) : TThreadID;
  123. function BeginThread(ThreadFunction : tthreadfunc;p : pointer) : TThreadID;
  124. function BeginThread(ThreadFunction : tthreadfunc;p : pointer; var ThreadId : TThreadID) : TThreadID;
  125. function BeginThread(ThreadFunction : tthreadfunc;p : pointer;
  126. var ThreadId : TThreadID; const stacksize: SizeUInt) : TThreadID;
  127. procedure EndThread(ExitCode : DWord);
  128. procedure EndThread;
  129. {some thread support functions}
  130. procedure FlushThread;
  131. function SuspendThread (threadHandle : TThreadID) : dword;
  132. function ResumeThread (threadHandle : TThreadID) : dword;
  133. function CloseThread (threadHandle : TThreadID) : dword;
  134. procedure ThreadSwitch; {give time to other threads}
  135. function KillThread (threadHandle : TThreadID) : dword;
  136. function WaitForThreadTerminate (threadHandle : TThreadID; TimeoutMs : longint) : dword; {0=no timeout}
  137. function ThreadSetPriority (threadHandle : TThreadID; Prio: longint): boolean; {-15..+15, 0=normal}
  138. function ThreadGetPriority (threadHandle : TThreadID): longint;
  139. function GetCurrentThreadId : TThreadID;
  140. procedure SetThreadDebugName(threadHandle: TThreadID; const ThreadName: AnsiString);
  141. {$ifdef FPC_HAS_FEATURE_UNICODESTRINGS}
  142. procedure SetThreadDebugName(threadHandle: TThreadID; const ThreadName: UnicodeString);
  143. {$endif FPC_HAS_FEATURE_UNICODESTRINGS}
  144. { this allows to do a lot of things in MT safe way }
  145. { it is also used to make the heap management }
  146. { thread safe }
  147. procedure InitCriticalSection(var cs : TRTLCriticalSection);
  148. procedure DoneCriticalSection(var cs : TRTLCriticalSection);
  149. procedure EnterCriticalSection(var cs : TRTLCriticalSection);
  150. procedure LeaveCriticalSection(var cs : TRTLCriticalSection);
  151. function TryEnterCriticalSection(var cs : TRTLCriticalSection):longint;
  152. function BasicEventCreate(EventAttributes : Pointer; AManualReset,InitialState : Boolean;const Name : ansistring):pEventState;
  153. procedure BasicEventDestroy(state:peventstate);
  154. procedure BasicEventResetEvent(state:peventstate);
  155. procedure BasicEventSetEvent(state:peventstate);
  156. function BasicEventWaitFor(Timeout : Cardinal;state:peventstate) : longint;
  157. function RTLEventCreate :PRTLEvent;
  158. procedure RTLEventDestroy(state:pRTLEvent);
  159. procedure RTLEventSetEvent(state:pRTLEvent);
  160. procedure RTLEventResetEvent(state:pRTLEvent);
  161. procedure RTLEventWaitFor(state:pRTLEvent);
  162. procedure RTLEventWaitFor(state:pRTLEvent;timeout : longint);
  163. { lazy thread initialization support }
  164. procedure RegisterLazyInitThreadingProc(const proc: TProcedure);
  165. { do not call LazyInitThreading directly}
  166. procedure LazyInitThreading;