East - Coroutine Module

协程模块

协程是什么,参考之前Coroutine的那一篇博客,这里不再赘述。

East中采用的协程模块是通过封装ucontext来实现协程的。
我们看看Fiber类的定义:

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

class Fiber
: public std::enable_shared_from_this<Fiber> { //only create on heap
public:
using sptr = std::shared_ptr<Fiber>;

enum State { INIT = 0, HOLD = 1, EXEC = 2, TERM = 3, READY = 4, EXCEPT = 5 };

private:
Fiber();

public:
/// @brief Create a fiber with a function
/// @param cb
/// @param stack_size, if 0, use default stack size
/// @param run_in_scheduler, please set false if you are not in scheduler
Fiber(std::function<void()> cb, size_t stack_size = 0,
bool run_in_scheduler = true);
~Fiber();

//重置协程函数,并重置状态
void reset(std::function<void()> cb);
//切换到当前协程执行
void resume();
//切换到后台执行
void yield();

public:
//设置当前协程
static void SetThis(Fiber*);
//返回当前协程
static Fiber::sptr GetThis();
//协程切换到后台,并且设为READY状态
static void YieldToReady();
//协程切换到后台,并且设置为HOLD状态
static void YieldToHold();
//总协程数
static uint64_t TotalFibers();
//获取当前协程id
static uint64_t GetFiberId();

static void MainFunc();

private:
uint64_t m_id{0}; //协程id
uint32_t m_stacksize{0}; //协程栈大小
State m_state{INIT}; //协程状态
ucontext_t m_ctx; //协程上下文
void* m_stack{nullptr}; // 协程栈指针
std::function<void()> m_cb; //协程函数
bool m_run_in_scheduler{false}; //是否在调度器中运行
};

核心变量:

1
2
static thread_local Fiber* t_fiber{nullptr};  //当前正在执行的协程
static thread_local Fiber::sptr t_master_fiber { nullptr }; //当前线程中的主协程,切换到这里面,就相当于切换到了主协程中运行
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
//设置当前线程正在运行的协程
void Fiber::SetThis(Fiber* p) {
t_fiber = p;
}

//获取当前线程正在运行的协程,函数的副作用是如果没有协程就创建协程
Fiber::sptr Fiber::GetThis() {
if (nullptr != t_fiber) {
return t_fiber->shared_from_this();
}

//如果当前线程没有协程,则创建一个主协程
Fiber::sptr main_fiber(new Fiber());
EAST_ASSERT(main_fiber.get() == t_fiber); //主协程理论也是当前协程
t_master_fiber = main_fiber;
return t_fiber->shared_from_this();
}

如果我们要协程运行我们指定的函数,我们需要申请额外的空间,以存放函数运行过程中需要保存的上下文。
所以我们的Fiber是有栈协程,也可以借此来判断一个协程是不是主协程-主协程没有额外申请空间,他的栈就是当前的线程栈,不需要我们额外处理,他只是作为一个返回点的标记!!

所以我们可以看到这两个构造函数的区别:

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
//主协程的构造函数, private funcion,只会在GetThis中调用
Fiber::Fiber() {
m_state = EXEC;
SetThis(this);

//主协程的上下文就是当前运行的上下文
if (getcontext(&m_ctx)) {
EAST_ASSERT2(false, "getcontext");
}

++s_fiber_count;
ELOG_INFO(g_logger) << "Main Fiber created, id: " << m_id
<< ", thread id: " << GetThreadId(); //main fiber id is 0
}

Fiber::Fiber(std::function<void()> cb, size_t stack_size, bool run_in_scheduler)
: m_id(++s_fiber_id), m_cb(cb), m_run_in_scheduler(run_in_scheduler) {

++s_fiber_count;
m_stacksize = stack_size != 0 ? stack_size : g_fiber_stack_size->getValue();

m_stack = StackAllocator::Alloc(m_stacksize);

//这个协程时需要运行我们指定的函数,我们需要申请额外的空间
if (getcontext(&m_ctx)) {
EAST_ASSERT2(false, "getcontext");
}

// if (!use_caller) {
// m_ctx.uc_link = &t_master_fiber->m_ctx; //diff
// } else {
// m_ctx.uc_link = &Scheduler::GetMainFiber()->m_ctx; //diff
// }

//设置协程函数
m_ctx.uc_link = nullptr;
m_ctx.uc_stack.ss_sp = m_stack;
m_ctx.uc_stack.ss_size = m_stacksize;

makecontext(&m_ctx, &Fiber::MainFunc, 0);

//setState(INIT);
ELOG_INFO(g_logger) << "Fiber created, id: " << m_id
<< ", thread id: " << GetThreadId()
<< ", run in scheduler: " << m_run_in_scheduler;
}

其他函数我们放到Scheduler模块继续讨论。

  • Copyright: Copyright is owned by the author. For commercial reprints, please contact the author for authorization. For non-commercial reprints, please indicate the source.
  • Copyrights © 2015-2025 Xudong0722
  • Visitors: | Views:

请我喝杯咖啡吧~

支付宝
微信