forked from marbl/Mash
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ThreadPool.hxx
230 lines (184 loc) · 5.78 KB
/
ThreadPool.hxx
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
// Copyright © 2015, Battelle National Biodefense Institute (BNBI);
// all rights reserved. Authored by: Brian Ondov, Todd Treangen,
// Sergey Koren, and Adam Phillippy
//
// See the LICENSE.txt file included with this software for license information.
#include "ThreadPool.h"
#include <stdlib.h>
#include <stdio.h>
#include <iostream>
template <class TypeInput, class TypeOutput>
ThreadPool<TypeInput, TypeOutput>::ThreadPool(TypeOutput * (* functionNew)(TypeInput *), unsigned int threadCountNew)
:
threadCount(threadCountNew),
function(functionNew)
{
mutexInput = new pthread_mutex_t();
mutexOutput = new pthread_mutex_t();
condInput = new pthread_cond_t();
condOutput = new pthread_cond_t();
pthread_mutex_init(mutexInput, NULL);
pthread_mutex_init(mutexOutput, NULL);
pthread_cond_init(condInput, NULL);
pthread_cond_init(condOutput, NULL);
inputCurrent = 0;
outputQueueHead = 0;
outputQueueTail = 0;
finished = false;
threads = new pthread_t[threadCount];
for ( int i = 0; i < threadCount; i++ )
{
pthread_create(&threads[i], NULL, &ThreadPool::thread, this);
}
}
template <class TypeInput, class TypeOutput>
ThreadPool<TypeInput, TypeOutput>::~ThreadPool()
{
pthread_mutex_lock(mutexInput);
finished = true;
pthread_cond_broadcast(condInput);
pthread_mutex_unlock(mutexInput);
for ( int i = 0; i < threadCount; i++ )
{
pthread_join(threads[i], NULL);
}
delete [] threads;
while ( outputQueueHead != 0 )
{
OutputQueueNode * next = outputQueueHead->next;
delete outputQueueHead;
outputQueueHead = next;
}
delete mutexInput;
delete mutexOutput;
delete condInput;
delete condOutput;
}
template <class TypeInput, class TypeOutput>
bool ThreadPool<TypeInput, TypeOutput>::outputAvailable() const
{
bool available;
pthread_mutex_lock(mutexOutput);
available = outputQueueHead != 0 && outputQueueHead->ready;
pthread_mutex_unlock(mutexOutput);
return available;
}
template <class TypeInput, class TypeOutput>
TypeOutput * ThreadPool<TypeInput, TypeOutput>::popOutputWhenAvailable()
{
pthread_mutex_lock(mutexOutput);
if ( outputQueueHead == 0 )
{
// TODO: error?
std::cerr << "ERROR: waiting for output when no output queued\n";
pthread_mutex_unlock(mutexOutput);
return 0;
}
while ( ! outputQueueHead->ready )
{
pthread_cond_wait(condOutput, mutexOutput);
}
TypeOutput * output = outputQueueHead->output;
OutputQueueNode * next = outputQueueHead->next;
if ( outputQueueTail == outputQueueHead )
{
outputQueueTail = 0;
}
delete outputQueueHead;
outputQueueHead = next;
pthread_mutex_unlock(mutexOutput);
return output;
}
template <class TypeInput, class TypeOutput>
void ThreadPool<TypeInput, TypeOutput>::runWhenThreadAvailable(TypeInput * input)
{
runWhenThreadAvailable(input, function);
}
template <class TypeInput, class TypeOutput>
void ThreadPool<TypeInput, TypeOutput>::runWhenThreadAvailable(TypeInput * input, TypeOutput * (* functionNew)(TypeInput *))
{
pthread_mutex_lock(mutexInput);
while ( inputCurrent != 0 )
{
pthread_cond_wait(condInput, mutexInput);
}
inputCurrent = input;
function = functionNew;
// enqueue output while input locked (to preserve order)
//
OutputQueueNode * outputQueueNode = new OutputQueueNode();
outputQueueNode->next = 0;
outputQueueNode->ready = false;
//
pthread_mutex_lock(mutexOutput);
//
if ( outputQueueHead == 0 )
{
outputQueueHead = outputQueueNode;
}
//
outputQueueNode->prev = outputQueueTail;
//
if ( outputQueueTail != 0 )
{
outputQueueTail->next = outputQueueNode;
}
//
outputQueueTail = outputQueueNode;
//
pthread_mutex_unlock(mutexOutput);
outputQueueNodeCurrent = outputQueueNode;
pthread_mutex_unlock(mutexInput);
pthread_cond_broadcast(condInput);
}
template <class TypeInput, class TypeOutput>
bool ThreadPool<TypeInput, TypeOutput>::running() const
{
bool running;
pthread_mutex_lock(mutexOutput);
running = outputQueueHead != 0;
pthread_mutex_unlock(mutexOutput);
return running;
}
template <class TypeInput, class TypeOutput>
void * ThreadPool<TypeInput, TypeOutput>::thread(void * arg)
{
ThreadPool * threadPool = (ThreadPool *)arg;
TypeInput * input;
OutputQueueNode * outputQueueNode;
while ( ! threadPool->finished )
{
// wait for input
//
pthread_mutex_lock(threadPool->mutexInput);
//
while ( ! threadPool->finished && threadPool->inputCurrent == 0 )
{
pthread_cond_wait(threadPool->condInput, threadPool->mutexInput);
}
if ( threadPool->finished )
{
pthread_mutex_unlock(threadPool->mutexInput);
return 0;
}
//
input = threadPool->inputCurrent;
outputQueueNode = threadPool->outputQueueNodeCurrent;
threadPool->inputCurrent = 0;
TypeOutput * (* function)(TypeInput *) = threadPool->function;
pthread_mutex_unlock(threadPool->mutexInput);
pthread_cond_broadcast(threadPool->condInput);
// run function
//
outputQueueNode->output = function(input);
delete input;
// signal output
//
outputQueueNode->ready = true;
//
pthread_mutex_lock(threadPool->mutexOutput);
pthread_cond_broadcast(threadPool->condOutput);
pthread_mutex_unlock(threadPool->mutexOutput);
}
return NULL;
}