threadh.inc 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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. { Delphi uses a longint for threadid }
  75. function BeginThread(sa : Pointer;stacksize : dword;
  76. ThreadFunction : tthreadfunc;p : pointer;creationFlags : dword;
  77. var ThreadId : Longint) : DWord;
  78. {$endif CPU64}
  79. { add some simplfied forms which make lifer easier and porting }
  80. { to other OSes too ... }
  81. function BeginThread(ThreadFunction : tthreadfunc) : DWord;
  82. function BeginThread(ThreadFunction : tthreadfunc;p : pointer) : DWord;
  83. function BeginThread(ThreadFunction : tthreadfunc;p : pointer; var ThreadId : THandle) : DWord;
  84. {$ifndef CPU64}
  85. function BeginThread(ThreadFunction : tthreadfunc;p : pointer; var ThreadId : Longint) : DWord;
  86. {$endif CPU64}
  87. procedure EndThread(ExitCode : DWord);
  88. procedure EndThread;
  89. {some thread support functions}
  90. function SuspendThread (threadHandle : dword) : dword;
  91. function ResumeThread (threadHandle : dword) : dword;
  92. procedure ThreadSwitch; {give time to other threads}
  93. function KillThread (threadHandle : dword) : dword;
  94. function WaitForThreadTerminate (threadHandle : dword; TimeoutMs : longint) : dword; {0=no timeout}
  95. function ThreadSetPriority (threadHandle : dword; Prio: longint): boolean; {-15..+15, 0=normal}
  96. function ThreadGetPriority (threadHandle : dword): Integer;
  97. function GetCurrentThreadId : dword;
  98. { this allows to do a lot of things in MT safe way }
  99. { it is also used to make the heap management }
  100. { thread safe }
  101. procedure InitCriticalSection(var cs : TRTLCriticalSection);
  102. procedure DoneCriticalsection(var cs : TRTLCriticalSection);
  103. procedure EnterCriticalsection(var cs : TRTLCriticalSection);
  104. procedure LeaveCriticalsection(var cs : TRTLCriticalSection);
  105. {
  106. $Log$
  107. Revision 1.15 2004-02-22 16:48:39 florian
  108. * several 64 bit issues fixed
  109. Revision 1.14 2003/11/29 17:29:32 michael
  110. + Added overloaded version of SetThreadManager without old parameter
  111. Revision 1.13 2003/11/27 10:28:41 michael
  112. + Patch from peter to fix make cycle
  113. Revision 1.12 2003/11/26 20:10:59 michael
  114. + New threadmanager implementation
  115. Revision 1.11 2003/10/01 21:00:09 peter
  116. * GetCurrentThreadHandle renamed to GetCurrentThreadId
  117. Revision 1.10 2003/03/27 17:14:27 armin
  118. * more platform independent thread routines, needs to be implemented for unix
  119. Revision 1.9 2002/10/16 19:04:27 michael
  120. + More system-independent thread routines
  121. Revision 1.8 2002/10/14 19:39:17 peter
  122. * threads unit added for thread support
  123. Revision 1.7 2002/09/07 15:07:46 peter
  124. * old logs removed and tabs fixed
  125. Revision 1.6 2002/07/28 20:43:48 florian
  126. * several fixes for linux/powerpc
  127. * several fixes to MT
  128. }