-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathschedule.h
More file actions
62 lines (39 loc) · 1.32 KB
/
schedule.h
File metadata and controls
62 lines (39 loc) · 1.32 KB
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
// This is a public header file.
#ifndef __COROUTINE_SCHEDULE_H__
#define __COROUTINE_SCHEDULE_H__
#include "noncopyable.h"
#include "coroutine.h"
#include <ucontext.h>
#include <map>
#include <vector>
#include <stdint.h>
namespace coroutine {
class Schedule : Noncopyable {
public:
static const int kStackSize = 1024 * 1024;
typedef std::function<void (Schedule*)> CoroutineFunc; //返回值是void (*foo) (Schedule*)
Schedule(int size = 16);
~Schedule();
// Return new coroutine's id.
// If the schedule is full, return -1.
int createCoroutine(const CoroutineFunc& func);
void runCoroutineById(int id);
void suspendCurrentCoroutine();
Coroutine::State getCoroutineStateById(int id) const;
// If there is no coroutine running, return -1.
int getRunningCoroutineId() const { return running_id_; }
private:
friend void coroutineFunc(uint32_t low32, uint32_t high32);
CoroutinePtr getCoroutineById(int id) const;
void deleteCoroutineById(int id);
void setRunningCoroutineId(int id) { running_id_ = id; }
typedef std::map<int, CoroutinePtr> CoroutineMap;
const int kCapacity_;
int running_id_;
ucontext_t main_context_;
char stack_[kStackSize];
CoroutineMap coroutines_;
std::vector<int> flags_;
};
} // namespace coroutine
#endif