协程调度模块:

首先是协程任务类FiberAndThread,包括协程,函数,指定的线程;提供了五个构造函数,只传协程的智能指针,只传函数对象,传协程智能指针的指针,函数对象指针,还有一个默认构造函数,为了是STL容器中初始化时调用默认构造函数,reset方法将任务的资源重置。

 struct FiberAndThread {
        Fiber::ptr fiber;
        std::function cb;
        int thread;
        FiberAndThread(Fiber::ptr f, int thr)
            :fiber(f), thread(thr) {
        }

       
        FiberAndThread(Fiber::ptr* f, int thr)
            :thread(thr) {
            fiber.swap(*f);
        }

       
        FiberAndThread(std::function f, int thr)
            :cb(f), thread(thr) {
        }

        
        FiberAndThread(std::function* f, int thr)
            :thread(thr) {
            cb.swap(*f);
        }

        
        FiberAndThread()
            :thread(-1) {
        }

        
        void reset() {
            fiber = nullptr;
            cb = nullptr;
            thread = -1;
        }
    };

内部维护了一个线程池,以及任务队列,当线程取任务时需要枷锁,这里使用互斥锁。

提供添加调度任务的方法,添加的可以是函数或者协程,也提供批量添加任务,使用迭代器作为参数,添加任务时加锁,然后调用无锁添加任务函数进行添加操作,添加完成侯通知对应的线程有任务来了,具体没有实现。

本站无任何商业行为
个人在线分享 » Sylar—协程调度模块
E-->