forked from sogou/workflow
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Workflow.h
310 lines (248 loc) · 6.89 KB
/
Workflow.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
/*
Copyright (c) 2019 Sogou, Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
Author: Xie Han ([email protected])
*/
#ifndef _WORKFLOW_H_
#define _WORKFLOW_H_
#include <assert.h>
#include <stddef.h>
#include <utility>
#include <functional>
#include <mutex>
#include "SubTask.h"
class SeriesWork;
class ParallelWork;
using series_callback_t = std::function<void (const SeriesWork *)>;
using parallel_callback_t = std::function<void (const ParallelWork *)>;
class Workflow
{
public:
static SeriesWork *
create_series_work(SubTask *first, series_callback_t callback);
static void
start_series_work(SubTask *first, series_callback_t callback);
static ParallelWork *
create_parallel_work(parallel_callback_t callback);
static ParallelWork *
create_parallel_work(SeriesWork *const all_series[], size_t n,
parallel_callback_t callback);
static void
start_parallel_work(SeriesWork *const all_series[], size_t n,
parallel_callback_t callback);
public:
static SeriesWork *
create_series_work(SubTask *first, SubTask *last,
series_callback_t callback);
static void
start_series_work(SubTask *first, SubTask *last,
series_callback_t callback);
};
class SeriesWork
{
public:
void start()
{
assert(!this->in_parallel);
this->first->dispatch();
}
/* Call dismiss() only when you don't want to start a created series.
* This operation is recursive, so only call on the "root". */
void dismiss()
{
assert(!this->in_parallel);
this->dismiss_recursive();
}
public:
void push_back(SubTask *task);
void push_front(SubTask *task);
public:
void *get_context() const { return this->context; }
void set_context(void *context) { this->context = context; }
public:
/* Cancel a running series. Typically, called in the callback of a task
* that belongs to the series. All subsequent tasks in the series will be
* destroyed immediately and recursively (ParallelWork), without callback.
* But the callback of this canceled series will still be called. */
virtual void cancel() { this->canceled = true; }
/* Parallel work's callback may check the cancellation state of each
* sub-series, and cancel it's super-series recursively. */
bool is_canceled() const { return this->canceled; }
/* 'false' until the time of callback. Mainly for sub-class. */
bool is_finished() const { return this->finished; }
public:
void set_callback(series_callback_t callback)
{
this->callback = std::move(callback);
}
public:
virtual void *get_specific(const char *key) { return NULL; }
public:
/* The following functions are intended for task implementations only. */
SubTask *pop();
void set_last_task(SubTask *last)
{
last->set_pointer(this);
this->last = last;
}
void unset_last_task() { this->last = NULL; }
const ParallelTask *get_in_parallel() const { return this->in_parallel; }
protected:
SubTask *get_last_task() const { return this->last; }
void set_in_parallel(const ParallelTask *task) { this->in_parallel = task; }
void dismiss_recursive();
protected:
void *context;
series_callback_t callback;
private:
SubTask *pop_task();
void expand_queue();
private:
SubTask *buf[4];
SubTask *first;
SubTask *last;
SubTask **queue;
int queue_size;
int front;
int back;
bool canceled;
bool finished;
const ParallelTask *in_parallel;
std::mutex mutex;
protected:
SeriesWork(SubTask *first, series_callback_t&& callback);
virtual ~SeriesWork();
friend class ParallelWork;
friend class Workflow;
};
static inline SeriesWork *series_of(const SubTask *task)
{
return (SeriesWork *)task->get_pointer();
}
static inline SeriesWork& operator *(const SubTask& task)
{
return *series_of(&task);
}
static inline SeriesWork& operator << (SeriesWork& series, SubTask *task)
{
series.push_back(task);
return series;
}
inline SeriesWork *
Workflow::create_series_work(SubTask *first, series_callback_t callback)
{
return new SeriesWork(first, std::move(callback));
}
inline void
Workflow::start_series_work(SubTask *first, series_callback_t callback)
{
new SeriesWork(first, std::move(callback));
first->dispatch();
}
inline SeriesWork *
Workflow::create_series_work(SubTask *first, SubTask *last,
series_callback_t callback)
{
SeriesWork *series = new SeriesWork(first, std::move(callback));
series->set_last_task(last);
return series;
}
inline void
Workflow::start_series_work(SubTask *first, SubTask *last,
series_callback_t callback)
{
SeriesWork *series = new SeriesWork(first, std::move(callback));
series->set_last_task(last);
first->dispatch();
}
class ParallelWork : public ParallelTask
{
public:
void start()
{
assert(!series_of(this));
Workflow::start_series_work(this, nullptr);
}
void dismiss()
{
assert(!series_of(this));
delete this;
}
public:
void add_series(SeriesWork *series);
public:
void *get_context() const { return this->context; }
void set_context(void *context) { this->context = context; }
public:
SeriesWork *series_at(size_t index)
{
if (index < this->subtasks_nr)
return this->all_series[index];
else
return NULL;
}
const SeriesWork *series_at(size_t index) const
{
if (index < this->subtasks_nr)
return this->all_series[index];
else
return NULL;
}
SeriesWork& operator[] (size_t index)
{
return *this->series_at(index);
}
const SeriesWork& operator[] (size_t index) const
{
return *this->series_at(index);
}
size_t size() const { return this->subtasks_nr; }
public:
void set_callback(parallel_callback_t callback)
{
this->callback = std::move(callback);
}
protected:
virtual SubTask *done();
protected:
void *context;
parallel_callback_t callback;
private:
void expand_buf();
private:
size_t buf_size;
SeriesWork **all_series;
protected:
ParallelWork(parallel_callback_t&& callback);
ParallelWork(SeriesWork *const all_series[], size_t n,
parallel_callback_t&& callback);
virtual ~ParallelWork();
friend class Workflow;
};
inline ParallelWork *
Workflow::create_parallel_work(parallel_callback_t callback)
{
return new ParallelWork(std::move(callback));
}
inline ParallelWork *
Workflow::create_parallel_work(SeriesWork *const all_series[], size_t n,
parallel_callback_t callback)
{
return new ParallelWork(all_series, n, std::move(callback));
}
inline void
Workflow::start_parallel_work(SeriesWork *const all_series[], size_t n,
parallel_callback_t callback)
{
ParallelWork *p = new ParallelWork(all_series, n, std::move(callback));
Workflow::start_series_work(p, nullptr);
}
#endif