tinyrpc/includes/coroutine/coroutine.hpp
2024-12-25 19:40:27 +08:00

40 lines
1.2 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#pragma once
#include "coctx.h"
#include <functional>
namespace tinyrpc {
class Coroutine {
friend void coFunction(Coroutine* co);
private:
Coroutine();
public:
// Coroutine(std::size_t stack_size, char* stack_sp);
Coroutine(std::size_t stack_size/* , char* stack_sp */, std::function<void()> cb);
// int getCorID() const {return m_cor_id;}
void operator()() const { // 调用 这个协程的回调
m_callback();
}
bool isMainCoroutine() const {return m_stack_sp == nullptr;}
// coctx* getContext() {return &m_ctx;}
static void yeild(); // 挂起当前的协程
void resume(); // 恢复 this 的运行
static Coroutine* getCurrCoroutine();
static Coroutine* getMainCoroutine();
~Coroutine();
private:
coctx m_ctx {}; // 这个协程的上下文信息
// int m_cor_id {0}; // 这个协程的 id
char* m_stack_sp {nullptr}; // 这个协程的栈空间指针
std::size_t m_stack_size {0};
bool m_is_in_cofunc {true}; // 调用 CoFunction 时为trueCoFunction 完成时为false。
std::function<void()> m_callback {}; // 这个协程的回调
};
}