-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscheduler.h
282 lines (225 loc) · 9.08 KB
/
scheduler.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
#pragma once
#include "config.h"
#include "context.h"
#include "task.h"
#include "block_object.h"
#include "co_mutex.h"
#include "timer.h"
#include "sleep_wait.h"
#include "processer.h"
#include "debugger.h"
#include "linux/io_wait.h"
namespace co {
struct ThreadLocalInfo
{
int thread_id = -1; // Run thread index, increment from 1.
uint8_t sleep_ms = 0;
Processer *proc = nullptr;
};
class ThreadPool;
class Scheduler
{
public:
// Run鏃舵墽琛岀殑鍐呭
enum eRunFlags
{
erf_do_coroutines = 0x1,
erf_do_timer = 0x2,
erf_do_sleeper = 0x4,
erf_do_eventloop = 0x8,
erf_idle_cpu = 0x10,
erf_signal = 0x20,
erf_all = 0x7fffffff,
};
typedef std::deque<Processer*> ProcList;
typedef std::pair<uint32_t, TSQueue<Task, false>> WaitPair;
typedef std::unordered_map<uint64_t, WaitPair> WaitZone;
typedef std::unordered_map<int64_t, WaitZone> WaitTable;
static Scheduler& getInstance();
// 鑾峰彇閰嶇疆閫夐」
CoroutineOptions& GetOptions();
// 鍒涘缓涓�涓崗绋�
void CreateTask(TaskF const& fn, std::size_t stack_size,
const char* file, int lineno, int dispatch);
// 褰撳墠鏄惁澶勪簬鍗忕▼涓�
bool IsCoroutine();
// 鏄惁娌℃湁鍗忕▼鍙墽琛�
bool IsEmpty();
// 褰撳墠鍗忕▼璁╁嚭鎵ц鏉�
void CoYield();
// 璋冨害鍣ㄨ皟搴﹀嚱鏁�, 鍐呴儴鎵ц鍗忕▼銆佽皟搴﹀崗绋�
uint32_t Run(int flags = erf_all);
// 寰幆Run鐩村埌娌℃湁鍗忕▼涓烘
// @loop_task_count: 涓嶈鏁扮殑甯搁┗鍗忕▼.
// 渚嬪锛歭oop_task_count == 2鏃�, 杩樺墿鏈�鍚�2涓崗绋嬬殑鏃跺�欒繖涓嚱鏁板氨浼歳eturn.
// @remarks: 杩欎釜鎺ュ彛浼氳嚦灏戞墽琛屼竴娆un.
void RunUntilNoTask(uint32_t loop_task_count = 0);
// 鏃犻檺寰幆鎵цRun
void RunLoop();
// 褰撳墠鍗忕▼鎬绘暟閲�
uint32_t TaskCount();
// 褰撳墠鍗忕▼ID, ID浠�1寮�濮嬶紙涓嶅湪鍗忕▼涓垯杩斿洖0锛�
uint64_t GetCurrentTaskID();
// 褰撳墠鍗忕▼鍒囨崲鐨勬鏁�
uint64_t GetCurrentTaskYieldCount();
// 璁剧疆褰撳墠鍗忕▼璋冭瘯淇℃伅, 鎵撳嵃璋冭瘯淇℃伅鏃跺皢鍥炴樉
void SetCurrentTaskDebugInfo(std::string const& info);
// 鑾峰彇褰撳墠鍗忕▼鐨勮皟璇曚俊鎭�, 杩斿洖鐨勫唴瀹瑰寘鎷敤鎴疯嚜瀹氫箟鐨勪俊鎭拰鍗忕▼ID
const char* GetCurrentTaskDebugInfo();
// 鑾峰彇褰撳墠绾跨▼ID.(鎸夋墽琛岃皟搴﹀櫒璋冨害鐨勯『搴忚)
uint32_t GetCurrentThreadID();
// 鑾峰彇褰撳墠杩涚▼ID.
uint32_t GetCurrentProcessID();
public:
/// sleep switch
// \timeout_ms min value is 0.
void SleepSwitch(int timeout_ms);
/// ------------------------------------------------------------------------
// @{ 瀹氭椂鍣�
template <typename DurationOrTimepoint>
TimerId ExpireAt(DurationOrTimepoint const& dur_or_tp, CoTimer::fn_t const& fn)
{
TimerId id = timer_mgr_.ExpireAt(dur_or_tp, fn);
DebugPrint(dbg_timer, "add timer id=%llu", (long long unsigned)id->GetId());
return id;
}
bool CancelTimer(TimerId timer_id);
bool BlockCancelTimer(TimerId timer_id);
// }@
/// ------------------------------------------------------------------------
/// ------------------------------------------------------------------------
// @{ 绾跨▼姹�
ThreadPool& GetThreadPool();
// }@
/// ------------------------------------------------------------------------
// iowait瀵硅薄
IoWait& GetIoWait() { return io_wait_; }
public:
Task* GetCurrentTask();
private:
Scheduler();
~Scheduler();
Scheduler(Scheduler const&) = delete;
Scheduler(Scheduler &&) = delete;
Scheduler& operator=(Scheduler const&) = delete;
Scheduler& operator=(Scheduler &&) = delete;
// 灏嗕竴涓崗绋嬪姞鍏ュ彲鎵ц闃熷垪涓�
void AddTaskRunnable(Task* tk, int dispatch = egod_default);
// Run鍑芥暟鐨勪竴閮ㄥ垎, 澶勭悊runnable鐘舵�佺殑鍗忕▼
uint32_t DoRunnable(bool allow_steal = true);
// Run鍑芥暟鐨勪竴閮ㄥ垎, 澶勭悊epoll鐩稿叧
int DoEpoll(int wait_milliseconds);
// Run鍑芥暟鐨勪竴閮ㄥ垎, 澶勭悊sleep鐩稿叧
// @next_ms: 璺濈涓嬩竴涓猼imer瑙﹀彂鐨勬绉掓暟
uint32_t DoSleep(long long &next_ms);
// Run鍑芥暟鐨勪竴閮ㄥ垎, 澶勭悊瀹氭椂鍣�
// @next_ms: 璺濈涓嬩竴涓猼imer瑙﹀彂鐨勬绉掓暟
uint32_t DoTimer(long long &next_ms);
// 鑾峰彇绾跨▼灞�閮ㄤ俊鎭�
ThreadLocalInfo& GetLocalInfo();
Processer* GetProcesser(std::size_t index);
// List of Processer
LFLock proc_init_lock_;
ProcList run_proc_list_;
atomic_t<uint32_t> dispatch_robin_index_{0};
// io block waiter.
IoWait io_wait_;
// sleep block waiter.
SleepWait sleep_wait_;
// Timer manager.
CoTimerMgr timer_mgr_;
ThreadPool *thread_pool_;
LFLock thread_pool_init_;
atomic_t<uint32_t> task_count_{0};
atomic_t<uint32_t> thread_id_{0};
private:
friend class CoMutex;
friend class BlockObject;
friend class IoWait;
friend class SleepWait;
friend class Processer;
friend class FileDescriptorCtx;
friend class CoDebugger;
public:
/**
* 鍗忕▼浜嬩欢鐩戝惉鍣�
* 娉ㄦ剰锛氬叾涓墍鏈夌殑鍥炶皟鏂规硶閮戒笉鍏佽鎶涘嚭寮傚父
*/
class TaskListener {
public:
/**
* 鍗忕▼琚垱寤烘椂琚皟鐢�
* 锛堟敞鎰忔鏃跺苟鏈繍琛屽湪鍗忕▼涓級
*
* @prarm task_id 鍗忕▼ID
* @prarm eptr
*/
virtual void onCreated(uint64_t task_id) noexcept {
}
/**
* 鍗忕▼寮�濮嬭繍琛�
* 锛堟湰鏂规硶杩愯鍦ㄥ崗绋嬩腑锛�
*
* @prarm task_id 鍗忕▼ID
* @prarm eptr
*/
virtual void onStart(uint64_t task_id) noexcept {
}
/**
* 鍗忕▼姝e父杩愯缁撴潫锛堟棤寮傚父鎶涘嚭锛�
* 锛堟湰鏂规硶杩愯鍦ㄥ崗绋嬩腑锛�
*
* @prarm task_id 鍗忕▼ID
*/
virtual void onCompleted(uint64_t task_id) noexcept {
}
/**
* 鍗忕▼鎶涘嚭鏈鎹曡幏鐨勫紓甯革紙鏈柟娉曡繍琛屽湪鍗忕▼涓級
* @prarm task_id 鍗忕▼ID
* @prarm eptr 鎶涘嚭鐨勫紓甯稿璞℃寚閽堬紝鍙鏈寚閽堣祴鍊间互淇敼寮傚父瀵硅薄锛�
* 寮傚父灏嗕娇鐢� CoroutineOptions.exception_handle 涓�
* 閰嶇疆鐨勬柟寮忓鐞嗭紱璧嬪�间负nullptr鍒欒〃绀哄拷鐣ユ寮傚父
* 锛侊紒娉ㄦ剰锛氬綋 exception_handle 閰嶇疆涓� immedaitely_throw 鏃舵湰浜嬩欢
* 锛侊紒涓� onFinished() 鍧囧け鏁堬紝寮傚父鍙戠敓鏃跺皢鐩存帴鎶涘嚭骞朵腑鏂▼搴忕殑杩愯锛屽悓鏃剁敓鎴恈oredump
*/
virtual void onException(uint64_t task_id, std::exception_ptr& eptr) noexcept {
}
/**
* 鍗忕▼杩愯瀹屾垚锛宨f(eptr) 涓篺alse璇存槑鍗忕▼姝e父缁撴潫锛屼负true璇存槑鍗忕▼鎶涘嚭浜嗕簡寮傚父
*
* @prarm task_id 鍗忕▼ID
* @prarm eptr 鎶涘嚭鐨勫紓甯稿璞℃寚閽�
*/
virtual void onFinished(uint64_t task_id, const std::exception_ptr eptr) noexcept {
}
virtual ~TaskListener() noexcept = default;
// ---> onCompleted -->
// | |
// onCreated --> onStart ---> onFinished
// | |
// ---> onException -->
};
private:
TaskListener* task_listener = nullptr;
public:
inline TaskListener* GetTaskListener() {
return task_listener;
}
inline void SetTaskListener(TaskListener* listener) {
this->task_listener = listener;
}
};
} //namespace co
#define g_Scheduler ::co::Scheduler::getInstance()
namespace co
{
inline Scheduler& Scheduler::getInstance()
{
static Scheduler obj;
return obj;
}
inline CoroutineOptions& Scheduler::GetOptions()
{
return CoroutineOptions::getInstance();
}
} //namespace co