#pragma once #if 0 #include #include #include #include #include "util/thread.h" #include "util/queue.hpp" class co_thread_pool; namespace ylib::co { class coroutine { public: struct promise_type { coroutine get_return_object() { return coroutine{ std::coroutine_handle::from_promise(*this) }; } std::suspend_always initial_suspend() { return {}; } // 协程初始化时挂起 std::suspend_never final_suspend() noexcept { return {}; } // 协程结束时挂起 void return_void() {} void unhandled_exception() { std::exit(1); } }; coroutine(std::coroutine_handle h) : coro(h) {} ~coroutine() { if (coro) coro.destroy(); } std::coroutine_handle coro; }; /// /// 协程调度器 /// class scheduler :public ylib::ithread { public: // 任务信息 struct task_info { // 任务回调 std::function callback; // 任务参数 void* param = nullptr; // 唤醒协程coco std::coroutine_handle<>* coco = nullptr; }; public: scheduler(); ~scheduler(); /// /// 启动 /// /// 挂起协程执行任务线程池大小 /// bool start(uint32 thread_size); void stop(); /// /// 投递协程 /// /// void push(const task_info& info); /// /// 投递线程任务 /// /// void push_t(std::function callback); /// /// 唤醒协程 /// /// void resume(std::coroutine_handle<>* continuation); private: // 通过 ithread 继承 bool run() override; /// /// 处理队列 /// void exec_queue(); private: // 协程处理队列 ylib::queue m_queue; // 线程池 co_thread_pool* m_pool = nullptr; }; } #endif