threadh.inc 6.4 KB

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