-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathExecutor.cpp
217 lines (184 loc) · 5.17 KB
/
Executor.cpp
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
#include "Executor.h"
#include "Task.h"
#include "stdafx.h"
using namespace std;
/**
* Implementation of executor
*/
class ExecutorImpl : public Executor {
public:
ExecutorImpl( int maxThreads );
void addTask( shared_ptr<Task> task );
void join();
/* The maximum number of threads that may run at once */
int maxThreads;
/* The current number of running threads */
int numRunningThreads;
/* Mutex to coordinate threads */
mutex mtx;
/* Condition variable used to signal when no tasks are running */
condition_variable cv;
/* Queue where we add tasks we aren't ready to run */
vector< shared_ptr<Task> > queue;
/* Threads we have created */
vector< thread* > threads;
/* Weak pointer to the executor */
weak_ptr<ExecutorImpl> self;
/* Add Functional Task */
void addFunctionalTask(std::function<void()> f);
};
/**
* Each thread calls the runTasks method which takes
* this form of data as a parameter
*/
class RunData {
public:
/* The associated executor */
shared_ptr<ExecutorImpl> executor;
/* The current task */
shared_ptr<Task> firstTask;
/* The thread that is running */
thread* runningThread;
RunData( shared_ptr<ExecutorImpl> executor, shared_ptr<Task> firstTask ) :
executor( executor),
firstTask( firstTask ),
runningThread(NULL) {}
};
/**
* When a thread exits, we must delete the associated thread object
* This class performs this task
*/
class ThreadCleanup {
public:
mutex& mtx;
RunData* runData;
ThreadCleanup( mutex& mtx,
RunData* runData ) :
mtx(mtx),
runData(runData) {
}
~ThreadCleanup() {
lock_guard<mutex> lock(mtx);
runData->runningThread->detach();
delete runData->runningThread;
delete runData;
}
};
/**
* Run tasks that have been added to the executor
*/
void runTasks( RunData* runData ) {
shared_ptr<ExecutorImpl> executor = runData->executor;
shared_ptr<Task> currentTask = runData->firstTask;
runData->firstTask.reset();
ThreadCleanup tc(executor->mtx, runData);
bool taskToPerform = true;
while (taskToPerform) {
currentTask->execute();
taskToPerform = false;
lock_guard<mutex> lock( executor->mtx );
if (executor->queue.size()>0) {
taskToPerform = true;
currentTask = executor->queue.back();
executor->queue.pop_back();
}
}
lock_guard<mutex> lock( executor->mtx );
executor->numRunningThreads--;
if (executor->numRunningThreads==0) {
executor->cv.notify_all();
}
}
/**
* Creates an executor
*/
ExecutorImpl::ExecutorImpl( int maxThreads ) :
maxThreads( maxThreads ),
numRunningThreads(0)
{
}
/**
* Add a task to the executor
*/
void ExecutorImpl::addTask( shared_ptr<Task> task ) {
lock_guard<mutex> lock( mtx );
if (numRunningThreads>=maxThreads) {
queue.push_back( task );
} else {
numRunningThreads++;
shared_ptr<ExecutorImpl> sharedPtr( self );
RunData* runData = new RunData( sharedPtr, task );
thread* t = new thread( runTasks, runData );
runData->runningThread = t;
}
}
void ExecutorImpl::addFunctionalTask(std::function<void()> f) {
class FunctionalTask : public Task {
public:
FunctionalTask(std::function<void()> f) : func(f) {};
std::function<void()> func;
void execute() {
func();
}
};
// shared_ptr<FunctionalTask> fTask = make_shared<FunctionalTask>(f);
shared_ptr<FunctionalTask> fTask(new FunctionalTask(f));
addTask(fTask);
}
/**
* Wait until all threads have completed
*/
void ExecutorImpl::join() {
unique_lock<mutex> lock( mtx );
while (numRunningThreads>0) {
cv.wait( lock );
}
}
/**
* Returns an executor
*/
shared_ptr<Executor> Executor::newInstance() {
return newInstance(thread::hardware_concurrency());
}
/**
* Returns an executor
*/
shared_ptr<Executor> Executor::newInstance( int maxThreads ) {
shared_ptr<ExecutorImpl> ret = make_shared<ExecutorImpl>(maxThreads);
ret->self = ret;
return ret;
}
static void test100Tasks() {
class MyTask : public Task {
public:
bool run;
void execute() {
run = true;
}
MyTask() : run(false) {}
};
shared_ptr<Executor> executor = Executor::newInstance();
vector<shared_ptr<MyTask> > tasks;
int nTasks = 100;
for (int i=0; i<nTasks; i++) {
shared_ptr<MyTask> task = make_shared<MyTask>();
tasks.push_back( task );
executor->addTask (task );
}
executor->join();
for (int i=0; i<nTasks; i++) {
ASSERT( tasks[i]->run );
}
}
static void testFunctionalTask() {
shared_ptr<Executor> executor = Executor::newInstance();
auto sayHello = [](){cout << "Hello!\n";};
for (int i=0; i<3; i++) {
executor->addFunctionalTask(sayHello);
}
executor->join();
}
void testExecutor() {
TEST( test100Tasks );
TEST( testFunctionalTask );
}