-
Notifications
You must be signed in to change notification settings - Fork 1
/
jobscheduler.h
331 lines (290 loc) · 11.1 KB
/
jobscheduler.h
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
#pragma once
#include <thread>
#include <string>
#include <mutex>
#include <list>
#include <map>
#include <queue>
#include <iostream>
#include <functional>
#include <condition_variable>
#include <utility>
#include "events.h"
namespace Tempo {
/**
* Semaphore for waking the worker up when a new job is available
*/
class Semaphore {
private:
std::mutex mutex;
unsigned int counter = 0;
std::condition_variable cv;
public:
Semaphore() = default;
void wait() {
std::unique_lock<std::mutex> lock(mutex);
while (!counter) {
cv.wait(lock);
}
--counter;
}
void post() {
std::lock_guard<std::mutex> guard(mutex);
++counter;
cv.notify_one();
}
};
typedef uint64_t jobId;
typedef uint64_t workerId;
/**
* Struct for processing the results of a job after it
* has been executed.
*
* It is possible to store some data in the shared_ptr
*/
struct JobResult {
bool success = false;
jobId id;
std::string err;
JobResult() = default;
virtual ~JobResult() = default;
};
/**
* Typedef for the lambda function that will be executed
*
* First argument is the progress of the function
* Second argument is the abort bool of the function
* If this is set to true, the function should abort itself
*
* The function should return true if successful, false if not
*/
typedef std::function<std::shared_ptr<JobResult>(float&, bool&)> jobFct;
typedef std::function<void(std::shared_ptr<JobResult>)> jobResultFct;
/*
* Job description
*/
struct Job {
enum jobState {
JOB_STATE_PENDING, JOB_STATE_RUNNING, JOB_STATE_FINISHED,
JOB_STATE_ERROR, JOB_STATE_CANCELED, JOB_STATE_ABORTED, JOB_STATE_NOTEXISTING
};
enum jobPriority { JOB_PRIORITY_LOWEST, JOB_PRIORITY_LOW, JOB_PRIORITY_NORMAL, JOB_PRIORITY_HIGH, JOB_PRIORITY_HIGHEST };
std::string name;
jobId id;
jobFct fct;
jobResultFct result_fct = [](const std::shared_ptr<JobResult>&) {};
jobState state = JOB_STATE_PENDING;
jobPriority priority = JOB_PRIORITY_NORMAL;
float progress = 0.f;
std::exception exception;
bool abort = false;
bool success = false;
std::shared_ptr<JobResult> result;
};
class JobEvent: public Event {
private:
std::shared_ptr<Job> job_;
public:
JobEvent(std::string& name, std::shared_ptr<Job> job): Event(name), job_(std::move(job)) {}
std::shared_ptr<Job> getJob() { return job_; }
};
#define JOBEVENT_PTRCAST(job) (reinterpret_cast<JobEvent*>((job)))
/**
* Custom Job reference to give ability to compare priorities between
* operators
*/
struct JobReference {
std::list<std::shared_ptr<Job>>::iterator it;
const std::shared_ptr<Job>& getJob() const {
return *it;
}
bool operator()(const JobReference& lhs, const JobReference& rhs) {
if ((*lhs.it)->priority == (*rhs.it)->priority)
return (*lhs.it)->id > (*rhs.it)->id;
else
return (*lhs.it)->priority < (*rhs.it)->priority;
}
};
/**
* @brief The JobScheduler class is there to allow multi-threading in the app\n
*
* We don't want that the GUI freeze whenever a heavy calculation is done.
* The JobScheduler helps by launching jobs in other thread(s), called Workers. First the user
* must define a number of workers that will always wait for new jobs to execute.
* The class allows for a syntax that permits to read the progress of a job, or send
* a command to abort a job. \n
*
* Here is a sample code using the scheduler :
* @code{.cpp}
*
* namespace Rendering {
* class MyWindow : public AbstractLayout {
* private:
* JobScheduler &scheduler_;
* int counter_ = 0;
* jobId job_id_;
* std::vector<jobId> jobs_;
* public:
* MyWindow() : scheduler_(JobScheduler::getInstance()) {
* scheduler_.setWorkerPoolSize(3);
* }
*
* void draw(GLFWwindow* window) override {
* ImGui::Begin("My Window");
* jobFct job;
* if(ImGui::Button("Launch job")) {
* std::string name = "myJob";
* int counter = counter_;
*
* // Dummy job
* jobFct job = [counter] (float &progress, bool &abort) -> bool {
* // Simulate a progression of some kind, update every 0.5 second
* for(int i = 0;i < 20;i++) {
* usleep(0.5*1e6);
* glfwPostEmptyEvent();
* if (abort)
* return false;
* progress = float(i+1)/20.;
* }
* return true ;
* };
* jobs_.push_back(scheduler_.addJob(name, job));
* counter_++;
* }
* ImGui::End();
* }
* }
* }
* @endcode
*
* @note if you are changing the Worker pool size often, it is important to regularly call the method clean()
* of the JobScheduler, because once a thread has been killed, its respective pointer is not automatically
* freed by the class.
*/
class JobScheduler {
private:
enum workerState { WORKER_STATE_IDLE, WORKER_STATE_WORKING, WORKER_STATE_KILLED };
struct Worker {
workerState state = WORKER_STATE_IDLE;
workerId id;
std::thread* thread;
};
jobId job_counter_ = 0;
workerId worker_counter_ = 0;
int num_active_workers_ = 0;
int thread_pool_size_ = 0;
int kill_x_workers_ = 0;
std::mutex kill_mutex_;
std::list<std::shared_ptr<Job>> jobs_list_;
std::recursive_mutex jobs_mutex_;
std::vector<std::shared_ptr<Job>> finalize_jobs_list_;
std::priority_queue<JobReference, std::vector<JobReference>, JobReference> priority_queue_;
Semaphore semaphore_;
std::list<Worker> workers_;
EventQueue& event_queue_;
/**
* Post an JobEvent to the event queue
* If nobody was listening to the event corresponding of this job, the function returns false
*/
void post_event(std::shared_ptr<Job> job);
/**
* Removes the job from the list
* Should only be called when jobs_mutex_ is already hold
* @param job
*/
inline void remove_job_from_list(JobReference& jobReference);
static jobResultFct no_op_fct;
JobScheduler(): event_queue_(EventQueue::getInstance()) {
setWorkerPoolSize(4);
}
public:
/**
* Copy constructors stay empty, because of the Singleton
*/
JobScheduler(JobScheduler const&) = delete;
void operator=(JobScheduler const&) = delete;
/**
* @return instance of the Singleton of the Job Scheduler
*/
static JobScheduler& getInstance() {
static JobScheduler instance;
return instance;
}
/**
* Sets the number of threads (workers) available
* If the given size is less than the number of active jobs, the function will first wait
* that some of the jobs are finished before killing the excess workers
* @param size of the worker pool
*/
void setWorkerPoolSize(int size);
/**
* Adds a new job to the scheduler
* The job starts whenever a thread is available and search for a new job
* Job fairness is not guaranteed
*
* Once a job is launched, whenever a job stops (FINISHED, ABORTED, CANCELED, ERROR),
* the JobScheduler will send two events : `jobs/names/[name]` and `jobs/ids/[job_id]`
* The user can subscribe to either of these events
*
* @param name name of the job
* @param function lambda function to be executed by the job. The function should be in this format :
* std::function<bool (float &progress, bool &abort)>, progress should be between 0 and 1 and indicate
* to outsiders the progress of the job, and abort can be read to see if an abort command has been
* carried on. It is recommended to implement these two arguments for efficient execution
* @param expect_acknowledge if it is set to true, then the job will retire under the condition
* that all listeners have acknowledged the JobEvent. If there are no listeners, then the job
* retires automatically
* @return id of the given job
*/
std::shared_ptr<Job>& addJob(std::string name, jobFct& function, jobResultFct& result_fct = no_op_fct, Job::jobPriority priority = Job::JOB_PRIORITY_NORMAL);
/**
* Stops the job with the given JobReference (if the jobs has implemented bool &abort of the lambda function)
* @param id id of the job
* @return true if the job is already stopped, false if not
*/
bool stopJob(jobId jobId);
void finalizeJobs();
/**
* Get the information about a certain job at a given time (copy of the job)
*
* If there is no job in the queue with the given id, the function will return a job with
* a state of JOB_STATE_NOTEXISTING
* @param id id of the job
* @return a copy of the Job structure which should contain informations about the job's state, success, ...
*/
Job getJobInfo(jobId id);
/**
* @return number of active workers
*/
int getNumberOfWorkers() const { return num_active_workers_; }
/**
* Function to check if there are any pending or running jobs
* @return true if any job is pending or running
*/
bool isBusy();
/**
* Cancels all jobs that are still in pending
*/
void cancelAllPendingJobs();
/**
* Function that is executed by each thread to look, wait and execute new jobs
* This function should have been private, but had to be made public for std::thread
*/
void worker_fct(Worker& worker);
/**
* Aborts all jobs, whether running or not.
*/
void abortAll();
/**
* Garbage collector of the workers
* Once threads have been killed, the JobScheduler does not automatically frees the pointer on the thread
* This function should be called regularly to clean the dandling pointers of the killed threads
* TODO : avoid garbage collecting
*/
void quit();
~JobScheduler() {
abortAll();
quit();
}
};
}