C++实现协程调度

来源:互联网 发布:淘宝男士帽子专卖店 编辑:程序博客网 时间:2024/06/03 13:19

项目:https://github.com/cdh2605/server

从微信开源phxrpc项目抽离协程调度部分,并作部分小调整。

主要类简介:

1.Memony:栈内存

class Memory {    void* m_raw;    void* m_addr;    size_t m_size;    bool m_protect;};
2.Context :协程上下文

class Context{    int m_events;    int m_timerId;    void* m_args;    Memory m_stack;    Function m_function;    Callback m_callback;    ucontext_t m_context;};

3.Timer:定时管理

class Timer{    struct TimerObj {        uint64_t m_absTime;        Context* m_context;    };                                                                             std::vector m_timerHeap;};
4.Runtime:协程环境

class Runtime{    static const int EVENT_NONE = 0;    static const int EVENT_CLOSE = -1;    static const int EVENT_ERROR = -2;    static const int EVENT_TIMEOUT = 0;            struct ContextSlot {        Context * context;        int nextDoneItem;        int status;    };    Timer m_timer;    std::vector m_contextList;    int m_firstDoneItem;    int m_currentUthread;    int m_unfinishedItemCount;    size_t m_stackSize;    bool m_needStackProtect;};
5.Scheduler:协程调度器
class Scheduler {    typedef std::queue > TaskQueue;    int m_maxTask;    int m_epollFd;    int m_pipeFds[2];    bool m_closed;    Runtime m_runtime;    TaskQueue m_taskQueue;};





原创粉丝点击