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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
|
#include "ThreadPool.h"
ThreadPool::ThreadPool()
{
for ( int i = 0; i < THREAD_TYPES; i++ )
{
num_threads_available[ i ] = 0;
max_load_event[ i ] = CreateEvent( NULL, TRUE, FALSE, NULL );
}
killswitch = CreateEvent( NULL, TRUE, FALSE, NULL );
// one thread of each type to start
for ( int i = 0; i < 2; i++ )
CreateNewThread_Internal( i );
watchdog_thread_handle = CreateThread( 0, 0, WatchDogThreadProcedure_stub, this, 0, 0 );
}
void ThreadPool::Kill()
{
SetEvent( killswitch );
WaitForSingleObject( watchdog_thread_handle, INFINITE );
CloseHandle( watchdog_thread_handle );
for ( ThreadID *l_thread : threads )
{
l_thread->Kill();
delete l_thread;
}
CloseHandle( killswitch );
for ( int i = 0; i < THREAD_TYPES; i++ )
CloseHandle( max_load_event[ i ] );
}
DWORD ThreadPool::WatchDogThreadProcedure_stub( LPVOID param )
{
ThreadPool *_this = (ThreadPool *)param;
return _this->WatchDogThreadProcedure();
}
/*
watchdog will get woken up when number of available threads hits zero
it creates a new thread, sleeps for a bit to let things "settle" and then reset the event
it will need a copy of all "any-thread" handles to build the new thread, and will need to manage in a thread safe way
(so a new thread doesn't "miss" a handle that is added in the interim)
*/
DWORD CALLBACK ThreadPool::WatchDogThreadProcedure()
{
// we ignore the max load event for reserved threads
HANDLE events[ 3 ] = { killswitch, max_load_event[ TYPE_MT ], max_load_event[ TYPE_STA ] };
while ( 1 )
{
DWORD ret = WaitForMultipleObjects( 3, events, FALSE, INFINITE );
if ( ret == WAIT_OBJECT_0 )
{
break;
}
else if ( ret == WAIT_OBJECT_0 + 1 || ret == WAIT_OBJECT_0 + 2 )
{
int thread_type = ret - ( WAIT_OBJECT_0 + 1 );
// this signal is for "full thread load reached"
// lets make sure we're actually at max capacity
Sleep( 10 ); // sleep a bit
if ( num_threads_available[ thread_type ] != 0 ) // see if we're still fully-loaded
continue;
guard.Lock();
CreateNewThread_Internal( thread_type );
guard.Unlock();
Sleep( 250 ); // give the system time to 'settle down' so we don't spawn a ton of threads in a row
ResetEvent( max_load_event[ thread_type ] );
}
}
return 0;
}
ThreadID *ThreadPool::ReserveThread( int flags )
{
// first, check to see if there's any released threads we can grab
Nullsoft::Utility::AutoLock threadlock( guard );
for ( ThreadID *t : threads )
{
if ( t->IsReserved() && t->IsReleased() && t->CanRunCOM( flags ) )
{
t->Reserve();
return t;
}
}
// TODO: if there are enough free threads available, mark one as reserved
// this will involve signalling the thread to switch to 'reserved' mode
// swapping out the 'function list' semaphore with a local one
// and removing all 'busy handles' from the queue
// can probably use the 'wake' handle to synchronize this
/*
int thread_type = GetThreadType(flags);
if (num_threads_available[thread_type > 2])
{
for (size_t i=0;i!=threads.size();i++)
{
if (threads[i]->IsReserved() == false && threads[i]->CanRunCOM(flags))
{
}
}
}
*/
ThreadID *new_thread = CreateNewThread_Internal( GetThreadType( flags, 1 ) );
return new_thread;
}
void ThreadPool::ReleaseThread( ThreadID *thread_id )
{
if ( thread_id )
{
// lock so there's no race condition between ReserveThread() and ReleaseThread()
Nullsoft::Utility::AutoLock threadlock( guard );
thread_id->Release();
}
}
int ThreadPool::AddHandle( ThreadID *thread_id, HANDLE handle, api_threadpool::ThreadPoolFunc func, void *user_data, intptr_t id, int flags )
{
// TODO: need to ensure that handles are not duplicated
thread_functions.Add( handle, func, user_data, id );
if ( thread_id )
{
if ( thread_id->CanRunCOM( flags ) )
thread_id->AddHandle( handle );
else
return 1;
return 0;
}
else
{
/* increment thread counts temporarily - because the massive wake-up
causes thread counts to go to 0 */
for ( int i = 0; i < THREAD_TYPES; i++ )
InterlockedIncrement( &num_threads_available[ i ] );
guard.Lock();
AddHandle_Internal( 0, handle, flags );
bool thread_types[ THREAD_TYPES ];
GetThreadTypes( flags, thread_types );
for ( int i = 0; i < THREAD_TYPES; i++ )
{
if ( thread_types[ i ] )
any_thread_handles[ i ].push_back( handle );
}
guard.Unlock();
for ( int i = 0; i < THREAD_TYPES; i++ )
InterlockedDecrement( &num_threads_available[ i ] );
}
return 0;
}
/* helper functions for adding/removing handles,
we keep going through the list as long as we can add/remove immediately.
once we have to block, we recurse the function starting at the next handle
when the function returns, we wait.
this lets us do some work rather than sit and wait for each thread's lock */
void ThreadPool::RemoveHandle_Internal(size_t start, HANDLE handle)
{
for (;start!=threads.size();start++)
{
ThreadID *t = threads[start];
if (!t->TryRemoveHandle(handle)) // try to remove
{
// have to wait
RemoveHandle_Internal(start+1, handle); // recurse start with the next thread
t->WaitRemoveHandle(handle); // finish the job
return;
}
}
}
void ThreadPool::AddHandle_Internal(size_t start, HANDLE handle, int flags)
{
for (;start<threads.size();start++)
{
ThreadID *t = threads[start];
if ((flags & api_threadpool::FLAG_LONG_EXECUTION) && t->IsReserved())
continue;
if (!t->CanRunCOM(flags))
continue;
if (!t->TryAddHandle(handle)) // try to add
{
// have to wait,
AddHandle_Internal(start+1, handle, flags); // recurse start with the next thread
t->WaitAddHandle(handle); // finish the job
return;
}
}
}
void ThreadPool::RemoveHandle(ThreadID *thread_id, HANDLE handle)
{
if (thread_id)
{
thread_id->RemoveHandle(handle);
}
else
{
/* increment thread counts temporarily - because the massive wake-up
causes thread counts to go to 0 */
for (int i=0;i<THREAD_TYPES;i++)
InterlockedIncrement(&num_threads_available[i]);
guard.Lock();
RemoveHandle_Internal(0, handle);
for (int j=0;j<THREAD_TYPES;j++)
{
//for (ThreadPoolTypes::HandleList::iterator itr = any_thread_handles[j].begin();
// itr != any_thread_handles[j].end();
// itr++)
ThreadPoolTypes::HandleList::iterator itr = any_thread_handles[j].begin();
while(itr != any_thread_handles[j].end())
{
if (*itr == handle)
{
itr = any_thread_handles[j].erase(itr);
}
else
{
itr++;
}
}
}
guard.Unlock();
for (int i=0;i<THREAD_TYPES;i++)
InterlockedDecrement(&num_threads_available[i]);
}
}
int ThreadPool::RunFunction(ThreadID *threadid, api_threadpool::ThreadPoolFunc func, void *user_data, intptr_t id, int flags)
{
if (threadid)
threadid->QueueFunction(func, user_data, id);
else
thread_functions.QueueFunction(func, user_data, id);
return 0;
}
ThreadID *ThreadPool::CreateNewThread_Internal(int thread_type)
{
int reserved=0;
int com_type = api_threadpool::FLAG_REQUIRE_COM_MT; // default
switch(thread_type)
{
case TYPE_STA_RESERVED:
reserved=1;
case TYPE_STA:
com_type = api_threadpool::FLAG_REQUIRE_COM_STA;
break;
case TYPE_MT_RESERVED:
reserved=1;
case TYPE_MT:
com_type = api_threadpool::FLAG_REQUIRE_COM_MT;
break;
}
Nullsoft::Utility::AutoLock threadlock(guard); // lock here (rather than after new ThreadID) to protect any_thread_handles
ThreadID *new_thread = new ThreadID(&thread_functions, killswitch, thread_functions.functions_semaphore,
any_thread_handles[thread_type],
&num_threads_available[thread_type], max_load_event[thread_type],
reserved, com_type);
threads.push_back(new_thread);
return new_thread;
}
size_t ThreadPool::GetNumberOfThreads()
{
Nullsoft::Utility::AutoLock threadlock(guard);
return threads.size();
}
size_t ThreadPool::GetNumberOfActiveThreads()
{
size_t numThreads = GetNumberOfThreads();
for (int i=0;i<THREAD_TYPES;i++)
numThreads -= num_threads_available[i];
return numThreads;
}
int ThreadPool::GetThreadType(int flags, int reserved)
{
flags &= api_threadpool::MASK_COM_FLAGS;
int thread_type=TYPE_MT;
switch(flags)
{
case api_threadpool::FLAG_REQUIRE_COM_STA:
thread_type = reserved?TYPE_STA_RESERVED:TYPE_STA;
break;
case 0: // default
case api_threadpool::FLAG_REQUIRE_COM_MT:
thread_type = reserved?TYPE_MT_RESERVED:TYPE_MT;
break;
}
return thread_type;
}
void ThreadPool::GetThreadTypes(int flags, bool types[THREAD_TYPES])
{
for (int i=0;i<THREAD_TYPES;i++)
{
types[i]=true;
}
if (flags & api_threadpool::FLAG_REQUIRE_COM_STA)
{
types[TYPE_MT] = false;
types[TYPE_MT] = false;
}
if (flags & api_threadpool::FLAG_REQUIRE_COM_STA)
{
types[TYPE_STA] = false;
types[TYPE_STA_RESERVED] = false;
}
if (flags & api_threadpool::FLAG_LONG_EXECUTION)
{
types[TYPE_STA_RESERVED] = false;
types[TYPE_MT_RESERVED] = false;
}
}
#define CBCLASS ThreadPool
START_DISPATCH;
CB(RESERVETHREAD, ReserveThread)
VCB(RELEASETHREAD, ReleaseThread)
CB(ADDHANDLE, AddHandle)
VCB(REMOVEHANDLE, RemoveHandle)
CB(RUNFUNCTION, RunFunction)
CB(GETNUMBEROFTHREADS, GetNumberOfThreads)
CB(GETNUMBEROFACTIVETHREADS, GetNumberOfActiveThreads)
END_DISPATCH;
#undef CBCLASS
|