2
$Id: threadh.inc,v 1.18 2004/05/23 20:26:20 marco Exp $
3
This file is part of the Free Pascal Run time library.
4
Copyright (c) 2000 by the Free Pascal development team
6
This File contains the OS indenpendend declartions for multi
7
threading support in FPC
9
See the File COPYING.FPC, included in this distribution,
10
for details about the copyright.
12
This program is distributed in the hope that it will be useful,
13
but WITHOUT ANY WARRANTY; without even the implied warranty of
14
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
16
**********************************************************************}
19
DefaultStackSize = 32768; { including 16384 margin for stackchecking }
23
PEventState = pointer;
24
TThreadFunc = function(parameter : pointer) : longint;
26
// Function prototypes for TThreadManager Record.
27
TBeginThreadHandler = Function (sa : Pointer;stacksize : dword; ThreadFunction : tthreadfunc;p : pointer;creationFlags : dword; var ThreadId : THandle) : DWord;
28
TEndThreadHandler = Procedure (ExitCode : DWord);
29
// Used for Suspend/Resume/Kill
30
TThreadHandler = Function (threadHandle : dword) : dword;
31
TThreadSwitchHandler = Procedure;
32
TWaitForThreadTerminateHandler = Function (threadHandle : dword; TimeoutMs : longint) : dword; {0=no timeout}
33
TThreadSetPriorityHandler = Function (threadHandle : dword; Prio: longint): boolean; {-15..+15, 0=normal}
34
TThreadGetPriorityHandler = Function (threadHandle : dword): Integer;
35
TGetCurrentThreadIdHandler = Function : dword;
36
TCriticalSectionHandler = Procedure (var cs);
37
TInitThreadVarHandler = Procedure(var offset : dword;size : dword);
38
TRelocateThreadVarHandler = Function(offset : dword) : pointer;
39
TAllocateThreadVarsHandler = Procedure;
40
TReleaseThreadVarsHandler = Procedure;
41
TBasicEventHandler = procedure(state:peventstate);
42
TBasicEventWaitForHandler = function (timeout:cardinal;state:peventstate):longint;
43
TBasicEventCreateHandler = function (EventAttributes :Pointer; AManualReset,InitialState : Boolean;const Name:ansistring):pEventState;
45
// TThreadManager interface.
46
TThreadManager = Record
47
InitManager : Function : Boolean;
48
DoneManager : Function : Boolean;
49
BeginThread : TBeginThreadHandler;
50
EndThread : TEndThreadHandler;
51
SuspendThread : TThreadHandler;
52
ResumeThread : TThreadHandler;
53
KillThread : TThreadHandler;
54
ThreadSwitch : TThreadSwitchHandler;
55
WaitForThreadTerminate : TWaitForThreadTerminateHandler;
56
ThreadSetPriority : TThreadSetPriorityHandler;
57
ThreadGetPriority : TThreadGetPriorityHandler;
58
GetCurrentThreadId : TGetCurrentThreadIdHandler;
59
InitCriticalSection : TCriticalSectionHandler;
60
DoneCriticalSection : TCriticalSectionHandler;
61
EnterCriticalSection : TCriticalSectionHandler;
62
LeaveCriticalSection : TCriticalSectionHandler;
63
InitThreadVar : TInitThreadVarHandler;
64
RelocateThreadVar : TRelocateThreadVarHandler;
65
AllocateThreadVars : TAllocateThreadVarsHandler;
66
ReleaseThreadVars : TReleaseThreadVarsHandler;
67
BasicEventCreate : TBasicEventCreateHandler;
68
BasicEventDestroy : TBasicEventHandler;
69
BasicEventResetEvent : TBasicEventHandler;
70
BasicEventSetEvent : TBasicEventHandler;
71
BasiceventWaitFOr : TBasicEventWaitForHandler;
74
{*****************************************************************************
75
Thread Handler routines
76
*****************************************************************************}
79
Function GetThreadManager(Var TM : TThreadManager) : Boolean;
80
Function SetThreadManager(Const NewTM : TThreadManager; Var OldTM : TThreadManager) : Boolean;
81
Function SetThreadManager(Const NewTM : TThreadManager) : Boolean;
82
Procedure SetNoThreadManager;
83
// Needs to be exported, so the manager can call it.
85
procedure InitThreadVars(RelocProc : Pointer);
87
procedure InitThread(stklen:cardinal);
89
{*****************************************************************************
91
*****************************************************************************}
93
function BeginThread(sa : Pointer;stacksize : dword;
94
ThreadFunction : tthreadfunc;p : pointer;creationFlags : dword;
95
var ThreadId : THandle) : DWord;
98
{ Delphi uses a longint for threadid }
99
function BeginThread(sa : Pointer;stacksize : dword;
100
ThreadFunction : tthreadfunc;p : pointer;creationFlags : dword;
101
var ThreadId : Longint) : DWord;
105
{ add some simplfied forms which make lifer easier and porting }
106
{ to other OSes too ... }
107
function BeginThread(ThreadFunction : tthreadfunc) : DWord;
108
function BeginThread(ThreadFunction : tthreadfunc;p : pointer) : DWord;
109
function BeginThread(ThreadFunction : tthreadfunc;p : pointer; var ThreadId : THandle) : DWord;
112
function BeginThread(ThreadFunction : tthreadfunc;p : pointer; var ThreadId : Longint) : DWord;
116
procedure EndThread(ExitCode : DWord);
119
{some thread support functions}
120
function SuspendThread (threadHandle : dword) : dword;
121
function ResumeThread (threadHandle : dword) : dword;
122
procedure ThreadSwitch; {give time to other threads}
123
function KillThread (threadHandle : dword) : dword;
124
function WaitForThreadTerminate (threadHandle : dword; TimeoutMs : longint) : dword; {0=no timeout}
125
function ThreadSetPriority (threadHandle : dword; Prio: longint): boolean; {-15..+15, 0=normal}
126
function ThreadGetPriority (threadHandle : dword): Integer;
127
function GetCurrentThreadId : dword;
130
{ this allows to do a lot of things in MT safe way }
131
{ it is also used to make the heap management }
133
procedure InitCriticalSection(var cs : TRTLCriticalSection);
134
procedure DoneCriticalsection(var cs : TRTLCriticalSection);
135
procedure EnterCriticalsection(var cs : TRTLCriticalSection);
136
procedure LeaveCriticalsection(var cs : TRTLCriticalSection);
138
function BasicEventCreate(EventAttributes : Pointer; AManualReset,InitialState : Boolean;const Name : ansistring):pEventState;
139
procedure basiceventdestroy(state:peventstate);
140
procedure basiceventResetEvent(state:peventstate);
141
procedure basiceventSetEvent(state:peventstate);
142
function basiceventWaitFor(Timeout : Cardinal;state:peventstate) : longint;
145
$Log: threadh.inc,v $
146
Revision 1.18 2004/05/23 20:26:20 marco
147
* wrappers and nothread prototypes for the basic* functions
149
Revision 1.17 2004/05/23 15:30:29 marco
150
* first try basicevent
152
Revision 1.16 2004/02/22 23:22:49 florian
153
* fixed BeginThread on unix
155
Revision 1.15 2004/02/22 16:48:39 florian
156
* several 64 bit issues fixed
158
Revision 1.14 2003/11/29 17:29:32 michael
159
+ Added overloaded version of SetThreadManager without old parameter
161
Revision 1.13 2003/11/27 10:28:41 michael
162
+ Patch from peter to fix make cycle
164
Revision 1.12 2003/11/26 20:10:59 michael
165
+ New threadmanager implementation
167
Revision 1.11 2003/10/01 21:00:09 peter
168
* GetCurrentThreadHandle renamed to GetCurrentThreadId
170
Revision 1.10 2003/03/27 17:14:27 armin
171
* more platform independent thread routines, needs to be implemented for unix
173
Revision 1.9 2002/10/16 19:04:27 michael
174
+ More system-independent thread routines
176
Revision 1.8 2002/10/14 19:39:17 peter
177
* threads unit added for thread support
179
Revision 1.7 2002/09/07 15:07:46 peter
180
* old logs removed and tabs fixed
182
Revision 1.6 2002/07/28 20:43:48 florian
183
* several fixes for linux/powerpc
184
* several fixes to MT