threadh.inc 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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. TThreadFunc = function(parameter : pointer) : longint;
  17. // Function prototypes for TThreadManager Record.
  18. TBeginThreadHandler = Function (sa : Pointer;stacksize : dword; ThreadFunction : tthreadfunc;p : pointer;creationFlags : dword; var ThreadId : THandle) : DWord;
  19. TEndThreadHandler = Procedure (ExitCode : DWord);
  20. // Used for Suspend/Resume/Kill
  21. TThreadHandler = Function (threadHandle : dword) : dword;
  22. TThreadSwitchHandler = Procedure;
  23. TWaitForThreadTerminateHandler = Function (threadHandle : dword; TimeoutMs : longint) : dword; {0=no timeout}
  24. TThreadSetPriorityHandler = Function (threadHandle : dword; Prio: longint): boolean; {-15..+15, 0=normal}
  25. TThreadGetPriorityHandler = Function (threadHandle : dword): Integer;
  26. TGetCurrentThreadIdHandler = Function : dword;
  27. TCriticalSectionHandler = Procedure (var cs);
  28. TInitThreadVarHandler = Procedure(var offset : dword;size : dword);
  29. TRelocateThreadVarHandler = Function(offset : dword) : pointer;
  30. TAllocateThreadVarsHandler = Procedure;
  31. TReleaseThreadVarsHandler = Procedure;
  32. // TThreadManager interface.
  33. TThreadManager = Record
  34. InitManager : Function : Boolean;
  35. DoneManager : Function : Boolean;
  36. BeginThread : TBeginThreadHandler;
  37. EndThread : TEndThreadHandler;
  38. SuspendThread : TThreadHandler;
  39. ResumeThread : TThreadHandler;
  40. KillThread : TThreadHandler;
  41. ThreadSwitch : TThreadSwitchHandler;
  42. WaitForThreadTerminate : TWaitForThreadTerminateHandler;
  43. ThreadSetPriority : TThreadSetPriorityHandler;
  44. ThreadGetPriority : TThreadGetPriorityHandler;
  45. GetCurrentThreadId : TGetCurrentThreadIdHandler;
  46. InitCriticalSection : TCriticalSectionHandler;
  47. DoneCriticalSection : TCriticalSectionHandler;
  48. EnterCriticalSection : TCriticalSectionHandler;
  49. LeaveCriticalSection : TCriticalSectionHandler;
  50. InitThreadVar : TInitThreadVarHandler;
  51. RelocateThreadVar : TRelocateThreadVarHandler;
  52. AllocateThreadVars : TAllocateThreadVarsHandler;
  53. ReleaseThreadVars : TReleaseThreadVarsHandler;
  54. end;
  55. {*****************************************************************************
  56. Thread Handler routines
  57. *****************************************************************************}
  58. Function GetThreadManager(Var TM : TThreadManager) : Boolean;
  59. Function SetThreadManager(Const NewTM : TThreadManager; Var OldTM : TThreadManager) : Boolean;
  60. Function SetThreadManager(Const NewTM : TThreadManager) : Boolean;
  61. Procedure SetNoThreadManager;
  62. // Needs to be exported, so the manager can call it.
  63. {$ifdef HASTHREADVAR}
  64. procedure InitThreadVars(RelocProc : Pointer);
  65. {$endif HASTHREADVAR}
  66. procedure InitThread(stklen:cardinal);
  67. {*****************************************************************************
  68. Multithread Handling
  69. *****************************************************************************}
  70. function BeginThread(sa : Pointer;stacksize : dword;
  71. ThreadFunction : tthreadfunc;p : pointer;creationFlags : dword;
  72. var ThreadId : THandle) : DWord;
  73. {$ifndef CPU64}
  74. {$ifndef unix}
  75. { Delphi uses a longint for threadid }
  76. function BeginThread(sa : Pointer;stacksize : dword;
  77. ThreadFunction : tthreadfunc;p : pointer;creationFlags : dword;
  78. var ThreadId : Longint) : DWord;
  79. {$endif unix}
  80. {$endif CPU64}
  81. { add some simplfied forms which make lifer easier and porting }
  82. { to other OSes too ... }
  83. function BeginThread(ThreadFunction : tthreadfunc) : DWord;
  84. function BeginThread(ThreadFunction : tthreadfunc;p : pointer) : DWord;
  85. function BeginThread(ThreadFunction : tthreadfunc;p : pointer; var ThreadId : THandle) : DWord;
  86. {$ifndef CPU64}
  87. {$ifndef unix}
  88. function BeginThread(ThreadFunction : tthreadfunc;p : pointer; var ThreadId : Longint) : DWord;
  89. {$endif unix}
  90. {$endif CPU64}
  91. procedure EndThread(ExitCode : DWord);
  92. procedure EndThread;
  93. {some thread support functions}
  94. function SuspendThread (threadHandle : dword) : dword;
  95. function ResumeThread (threadHandle : dword) : dword;
  96. procedure ThreadSwitch; {give time to other threads}
  97. function KillThread (threadHandle : dword) : dword;
  98. function WaitForThreadTerminate (threadHandle : dword; TimeoutMs : longint) : dword; {0=no timeout}
  99. function ThreadSetPriority (threadHandle : dword; Prio: longint): boolean; {-15..+15, 0=normal}
  100. function ThreadGetPriority (threadHandle : dword): Integer;
  101. function GetCurrentThreadId : dword;
  102. { this allows to do a lot of things in MT safe way }
  103. { it is also used to make the heap management }
  104. { thread safe }
  105. procedure InitCriticalSection(var cs : TRTLCriticalSection);
  106. procedure DoneCriticalsection(var cs : TRTLCriticalSection);
  107. procedure EnterCriticalsection(var cs : TRTLCriticalSection);
  108. procedure LeaveCriticalsection(var cs : TRTLCriticalSection);
  109. {
  110. $Log$
  111. Revision 1.16 2004-02-22 23:22:49 florian
  112. * fixed BeginThread on unix
  113. Revision 1.15 2004/02/22 16:48:39 florian
  114. * several 64 bit issues fixed
  115. Revision 1.14 2003/11/29 17:29:32 michael
  116. + Added overloaded version of SetThreadManager without old parameter
  117. Revision 1.13 2003/11/27 10:28:41 michael
  118. + Patch from peter to fix make cycle
  119. Revision 1.12 2003/11/26 20:10:59 michael
  120. + New threadmanager implementation
  121. Revision 1.11 2003/10/01 21:00:09 peter
  122. * GetCurrentThreadHandle renamed to GetCurrentThreadId
  123. Revision 1.10 2003/03/27 17:14:27 armin
  124. * more platform independent thread routines, needs to be implemented for unix
  125. Revision 1.9 2002/10/16 19:04:27 michael
  126. + More system-independent thread routines
  127. Revision 1.8 2002/10/14 19:39:17 peter
  128. * threads unit added for thread support
  129. Revision 1.7 2002/09/07 15:07:46 peter
  130. * old logs removed and tabs fixed
  131. Revision 1.6 2002/07/28 20:43:48 florian
  132. * several fixes for linux/powerpc
  133. * several fixes to MT
  134. }