-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathvxl_threads.pas
174 lines (151 loc) · 3.97 KB
/
vxl_threads.pas
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
//------------------------------------------------------------------------------
//
// Voxelizer
// Copyright (C) 2021-2022 by Jim Valavanis
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
// 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. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, inc., 59 Temple Place - Suite 330, Boston, MA
// 02111-1307, USA.
//
// DESCRIPTION:
// Thread class
//
//------------------------------------------------------------------------------
// Site : https://sourceforge.net/projects/voxelizer/
//------------------------------------------------------------------------------
unit vxl_threads;
interface
type
threadfunc_t = function(p: pointer): integer; stdcall;
type
TDThread = class;
threadinfo_t = record
thread: TDThread;
end;
Pthreadinfo_t = ^threadinfo_t;
TDThread = class
private
suspended: boolean;
protected
ffunc: threadfunc_t;
fparms: Pointer;
fid: Integer;
info: threadinfo_t;
fstatus: integer;
fterminated: boolean;
public
constructor Create(const func: threadfunc_t = nil);
destructor Destroy; override;
procedure Activate(const parms: pointer); overload;
procedure Activate(const func: threadfunc_t; const parms: pointer); overload;
procedure Wait;
function CheckJobDone: Boolean;
function IsIdle: Boolean;
property func: threadfunc_t read ffunc write ffunc;
end;
const
THR_DEAD = 0;
THR_ACTIVE = 1;
THR_IDLE = 2;
implementation
uses
Windows;
type
process_t = function(p: pointer): LongInt; stdcall;
function I_CreateProcess(p: process_t; parm: pointer; suspended: boolean): integer;
var
id: LongWord;
begin
if suspended then
result := CreateThread(nil, $1000, @p, parm, CREATE_SUSPENDED, id)
else
result := CreateThread(nil, $1000, @p, parm, 0, id);
end;
function ThreadWorker(p: Pointer): integer; stdcall;
begin
result := 0;
while true do
begin
while (Pthreadinfo_t(p).thread.fstatus = THR_IDLE) and (not Pthreadinfo_t(p).thread.fterminated) do
begin
sleep(0);
end;
if Pthreadinfo_t(p).thread.fterminated then
exit;
Pthreadinfo_t(p).thread.ffunc(Pthreadinfo_t(p).thread.fparms);
if Pthreadinfo_t(p).thread.fterminated then
exit;
Pthreadinfo_t(p).thread.fstatus := THR_IDLE;
end;
end;
constructor TDThread.Create(const func: threadfunc_t);
begin
fterminated := false;
ffunc := func;
fparms := nil;
fstatus := THR_IDLE;
info.thread := Self;
fid := I_CreateProcess(@ThreadWorker, @info, true);
suspended := true;
end;
destructor TDThread.Destroy;
begin
fterminated := true;
fstatus := THR_DEAD;
WaitForSingleObject(fid, 100);
Inherited Destroy;
end;
procedure TDThread.Activate(const func: threadfunc_t; const parms: pointer);
begin
ffunc := func;
Activate(parms);
end;
// JVAL: Should check for fstatus, but it is not called while active
procedure TDThread.Activate(const parms: pointer);
begin
fparms := parms;
fstatus := THR_ACTIVE;
suspended := false;
ResumeThread(fid);
end;
procedure TDThread.Wait;
begin
if suspended then
Exit;
while fstatus = THR_ACTIVE do
begin
sleep(0);
end;
suspended := true;
SuspendThread(fid);
end;
function TDThread.CheckJobDone: Boolean;
begin
if fstatus = THR_IDLE then
begin
if not suspended then
begin
suspended := true;
SuspendThread(fid);
end;
result := true;
end
else
result := false;
end;
function TDThread.IsIdle: Boolean;
begin
result := fstatus = THR_IDLE;
end;
end.