1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
|
{
This file is part of the Free Component Library (FCL)
Copyright (c) 2003-2004 Armin Diehl, member of the Free Pascal
development team
Netware clib TThread implementation
See the file COPYING.FPC, included in this distribution,
for details about the copyright.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
**********************************************************************}
type
PThreadRec=^TThreadRec;
TThreadRec=record
thread : TThread;
next : PThreadRec;
end;
var
ThreadRoot : PThreadRec;
ThreadsInited : boolean;
DisableRemoveThread : boolean;
Const
ThreadCount: longint = 0;
{function ThreadSelf:TThread;
var
hp : PThreadRec;
sp : longint;
begin
sp:=SPtr;
hp:=ThreadRoot;
while assigned(hp) do
begin
if (sp<=hp^.Thread.FStackPointer) and
(sp>(hp^.Thread.FStackPointer-hp^.Thread.FStackSize)) then
begin
Result:=hp^.Thread;
exit;
end;
hp:=hp^.next;
end;
Result:=nil;
end;}
procedure InitThreads;
begin
ThreadRoot:=nil;
ThreadsInited:=true;
DisableRemoveThread:=false;
end;
{DoneThreads will terminate all remaining threads}
procedure DoneThreads;
var
hp,next : PThreadRec;
begin
DisableRemoveThread := true; {to avoid that Destroy calling RemoveThread modifies Thread List}
while assigned(ThreadRoot) do
begin
ThreadRoot^.Thread.Destroy;
hp:=ThreadRoot;
ThreadRoot:=ThreadRoot^.Next;
dispose(hp);
{$ifdef DEBUG_MT}
ConsolePrintf(#13'DoneThreads: called destroy, remaining threads: %d ThreadRoot: %x'#13#10,ThreadCount,longint(ThreadRoot));
{$endif}
end;
ThreadsInited:=false;
end;
procedure AddThread(t:TThread);
var
hp : PThreadRec;
begin
{ Need to initialize threads ? }
if not ThreadsInited then
InitThreads;
{ Put thread in the linked list }
new(hp);
hp^.Thread:=t;
hp^.next:=ThreadRoot;
ThreadRoot:=hp;
inc(ThreadCount);
end;
procedure RemoveThread(t:TThread);
var
lasthp,hp : PThreadRec;
begin
if not DisableRemoveThread then {disabled while in DoneThreads}
begin
hp:=ThreadRoot;
lasthp:=nil;
while assigned(hp) do
begin
if hp^.Thread=t then
begin
if assigned(lasthp) then
lasthp^.next:=hp^.next
else
ThreadRoot:=hp^.next;
dispose(hp);
Dec(ThreadCount);
if ThreadCount = 0 then ThreadsInited := false;
exit;
end;
lasthp:=hp;
hp:=hp^.next;
end;
end else
dec(ThreadCount);
end;
procedure TThread.SysCreate(CreateSuspended: Boolean;
const StackSize: SizeUInt);
var
Flags: Integer;
begin
AddThread(self);
FSuspended := CreateSuspended;
{ Create new thread }
FHandle := BeginThread (@ThreadProc,pointer(self));
if FSuspended then Suspend;
FThreadID := FHandle;
FFatalException := nil;
end;
procedure TThread.SysDestroy;
begin
if not FFinished then
begin
Terminate;
if Suspended then
ResumeThread (FHandle); {netware can not kill a thread, the thread has to}
{leave it's execute routine if terminated is true}
WaitFor; {wait for the thread to terminate}
end;
FFatalException.Free;
FFatalException := nil;
RemoveThread(self); {remove it from the list of active threads}
end;
procedure TThread.CallOnTerminate;
begin
FOnTerminate(Self);
end;
procedure TThread.DoTerminate;
begin
if Assigned(FOnTerminate) then
Synchronize(@CallOnTerminate);
end;
const
Priorities: array [TThreadPriority] of Integer =
(THREAD_PRIORITY_IDLE, THREAD_PRIORITY_LOWEST, THREAD_PRIORITY_BELOW_NORMAL,
THREAD_PRIORITY_NORMAL, THREAD_PRIORITY_ABOVE_NORMAL,
THREAD_PRIORITY_HIGHEST, THREAD_PRIORITY_TIME_CRITICAL);
function TThread.GetPriority: TThreadPriority;
var
P: Integer;
I: TThreadPriority;
begin
P := ThreadGetPriority(FHandle);
Result := tpNormal;
for I := Low(TThreadPriority) to High(TThreadPriority) do
if Priorities[I] = P then Result := I;
end;
procedure TThread.SetPriority(Value: TThreadPriority);
begin
ThreadSetPriority(FHandle, Priorities[Value]);
end;
procedure TThread.SetSuspended(Value: Boolean);
begin
if Value <> FSuspended then
if Value then
Suspend
else
Resume;
end;
procedure TThread.Suspend;
begin
SuspendThread (FHandle);
FSuspended := true;
end;
procedure TThread.Resume;
begin
ResumeThread (FHandle);
FSuspended := False;
end;
function TThread.WaitFor: Integer;
begin
Result := WaitForThreadTerminate (FHandle,0);
if Result = 0 then
FHandle := 0;
end;
|