threadh.inc 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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. TCriticalSectionHandler = Procedure (var cs);
  40. TCriticalSectionHandlerTryEnter = function (var cs):longint;
  41. TInitThreadVarHandler = Procedure(var offset : {$ifdef cpu16}word{$else}dword{$endif};size : dword);
  42. TRelocateThreadVarHandler = Function(offset : {$ifdef cpu16}word{$else}dword{$endif}) : pointer;
  43. TAllocateThreadVarsHandler = Procedure;
  44. TReleaseThreadVarsHandler = Procedure;
  45. TBasicEventHandler = procedure(state:peventstate);
  46. TBasicEventWaitForHandler = function (timeout:cardinal;state:peventstate):longint;
  47. TBasicEventCreateHandler = function (EventAttributes :Pointer; AManualReset,InitialState : Boolean;const Name:ansistring):pEventState;
  48. TRTLEventHandler = procedure(AEvent:PRTLEvent);
  49. TRTLEventHandlerTimeout = procedure(AEvent:PRTLEvent;timeout : longint);
  50. TRTLCreateEventHandler = function:PRTLEvent;
  51. // semaphores stuff
  52. TSempahoreInitHandler = function: Pointer;
  53. TSemaphoreDestroyHandler = procedure (const sem: Pointer);
  54. TSemaphorePostHandler = procedure (const sem: Pointer);
  55. TSemaphoreWaitHandler = procedure (const sem: Pointer);
  56. // TThreadManager interface.
  57. TThreadManager = Record
  58. InitManager : Function : Boolean;
  59. DoneManager : Function : Boolean;
  60. BeginThread : TBeginThreadHandler;
  61. EndThread : TEndThreadHandler;
  62. SuspendThread : TThreadHandler;
  63. ResumeThread : TThreadHandler;
  64. KillThread : TThreadHandler;
  65. CloseThread : TThreadHandler;
  66. ThreadSwitch : TThreadSwitchHandler;
  67. WaitForThreadTerminate : TWaitForThreadTerminateHandler;
  68. ThreadSetPriority : TThreadSetPriorityHandler;
  69. ThreadGetPriority : TThreadGetPriorityHandler;
  70. GetCurrentThreadId : TGetCurrentThreadIdHandler;
  71. InitCriticalSection : TCriticalSectionHandler;
  72. DoneCriticalSection : TCriticalSectionHandler;
  73. EnterCriticalSection : TCriticalSectionHandler;
  74. TryEnterCriticalSection: TCriticalSectionHandlerTryEnter;
  75. LeaveCriticalSection : TCriticalSectionHandler;
  76. InitThreadVar : TInitThreadVarHandler;
  77. RelocateThreadVar : TRelocateThreadVarHandler;
  78. AllocateThreadVars : TAllocateThreadVarsHandler;
  79. ReleaseThreadVars : TReleaseThreadVarsHandler;
  80. BasicEventCreate : TBasicEventCreateHandler; // left in for a while.
  81. BasicEventDestroy : TBasicEventHandler; // we might need BasicEvent
  82. BasicEventResetEvent : TBasicEventHandler; // for a real TEvent
  83. BasicEventSetEvent : TBasicEventHandler;
  84. BasiceventWaitFOr : TBasicEventWaitForHandler;
  85. RTLEventCreate : TRTLCreateEventHandler;
  86. RTLEventDestroy : TRTLEventHandler;
  87. RTLEventSetEvent : TRTLEventHandler;
  88. RTLEventResetEvent : TRTLEventHandler;
  89. RTLEventWaitFor : TRTLEventHandler;
  90. RTLEventWaitForTimeout : TRTLEventHandlerTimeout;
  91. end;
  92. {*****************************************************************************
  93. Thread Handler routines
  94. *****************************************************************************}
  95. Function GetThreadManager(Var TM : TThreadManager) : Boolean;
  96. Function SetThreadManager(Const NewTM : TThreadManager; Var OldTM : TThreadManager) : Boolean;
  97. Function SetThreadManager(Const NewTM : TThreadManager) : Boolean;
  98. {$ifndef DISABLE_NO_THREAD_MANAGER}
  99. {$endif DISABLE_NO_THREAD_MANAGER}
  100. { Needs to be exported, so the manager can call it.
  101. It needs to be exported even if FPC_SECTION_THREADVARS is set as it relocates also the
  102. heap
  103. }
  104. procedure InitThreadVars(RelocProc : TRelocateThreadVarHandler);
  105. procedure InitThread(stklen:SizeUInt);
  106. procedure DoneThread;
  107. {*****************************************************************************
  108. Multithread Handling
  109. *****************************************************************************}
  110. function BeginThread(sa : Pointer;stacksize : SizeUInt;
  111. ThreadFunction : tthreadfunc;p : pointer;creationFlags : dword;
  112. var ThreadId : TThreadID) : TThreadID;
  113. { add some simplfied forms which make lifer easier and porting }
  114. { to other OSes too ... }
  115. function BeginThread(ThreadFunction : tthreadfunc) : TThreadID;
  116. function BeginThread(ThreadFunction : tthreadfunc;p : pointer) : TThreadID;
  117. function BeginThread(ThreadFunction : tthreadfunc;p : pointer; var ThreadId : TThreadID) : TThreadID;
  118. function BeginThread(ThreadFunction : tthreadfunc;p : pointer;
  119. var ThreadId : TThreadID; const stacksize: SizeUInt) : TThreadID;
  120. procedure EndThread(ExitCode : DWord);
  121. procedure EndThread;
  122. {some thread support functions}
  123. procedure FlushThread;
  124. function SuspendThread (threadHandle : TThreadID) : dword;
  125. function ResumeThread (threadHandle : TThreadID) : dword;
  126. function CloseThread (threadHandle : TThreadID) : dword;
  127. procedure ThreadSwitch; {give time to other threads}
  128. function KillThread (threadHandle : TThreadID) : dword;
  129. function WaitForThreadTerminate (threadHandle : TThreadID; TimeoutMs : longint) : dword; {0=no timeout}
  130. function ThreadSetPriority (threadHandle : TThreadID; Prio: longint): boolean; {-15..+15, 0=normal}
  131. function ThreadGetPriority (threadHandle : TThreadID): longint;
  132. function GetCurrentThreadId : TThreadID;
  133. { this allows to do a lot of things in MT safe way }
  134. { it is also used to make the heap management }
  135. { thread safe }
  136. procedure InitCriticalSection(var cs : TRTLCriticalSection);
  137. procedure DoneCriticalSection(var cs : TRTLCriticalSection);
  138. procedure EnterCriticalSection(var cs : TRTLCriticalSection);
  139. procedure LeaveCriticalSection(var cs : TRTLCriticalSection);
  140. function TryEnterCriticalSection(var cs : TRTLCriticalSection):longint;
  141. function BasicEventCreate(EventAttributes : Pointer; AManualReset,InitialState : Boolean;const Name : ansistring):pEventState;
  142. procedure BasicEventDestroy(state:peventstate);
  143. procedure BasicEventResetEvent(state:peventstate);
  144. procedure BasicEventSetEvent(state:peventstate);
  145. function BasicEventWaitFor(Timeout : Cardinal;state:peventstate) : longint;
  146. function RTLEventCreate :PRTLEvent;
  147. procedure RTLEventDestroy(state:pRTLEvent);
  148. procedure RTLEventSetEvent(state:pRTLEvent);
  149. procedure RTLEventResetEvent(state:pRTLEvent);
  150. procedure RTLEventWaitFor(state:pRTLEvent);
  151. procedure RTLEventWaitFor(state:pRTLEvent;timeout : longint);