librf

scheduler.h
1 #pragma once
2
3 namespace resumef
4 {
10 struct scheduler_t : public std::enable_shared_from_this<scheduler_t>
11 {
12 private:
13 using state_sptr = counted_ptr<state_base_t>;
14 using state_vector = std::vector<state_sptr>;
15 using lock_type = spinlock;
16 using task_dictionary_type = std::unordered_map<state_base_t*, std::unique_ptr<task_t>>;
17
18 mutable spinlock _lock_running;
19 state_vector _runing_states;
20 state_vector _cached_states;
21
22 mutable spinlock _lock_ready;
23 task_dictionary_type _ready_task;
24
25 timer_mgr_ptr _timer;
26
27 task_t* new_task(task_t* task);
28 //void cancel_all_task_();
29 public:
35 void run_one_batch();
36
41 void run_until_notask();
42
43 //void break_all();
44
53 template<class _Ty
54 #ifndef DOXYGEN_SKIP_PROPERTY
55 COMMA_RESUMEF_ENABLE_IF(traits::is_callable_v<_Ty> || traits::is_future_v<_Ty> || traits::is_generator_v<_Ty>)
56 #endif //DOXYGEN_SKIP_PROPERTY
57 >
58 #ifndef DOXYGEN_SKIP_PROPERTY
59 RESUMEF_REQUIRES(traits::is_callable_v<_Ty> || traits::is_future_v<_Ty> || traits::is_generator_v<_Ty>)
60 #endif //DOXYGEN_SKIP_PROPERTY
61 task_t* operator + (_Ty&& coro)
62 {
63 if constexpr (traits::is_callable_v<_Ty>)
64 return new_task(new task_ctx_impl_t<_Ty>(coro));
65 else
66 return new_task(new task_impl_t<_Ty>(coro));
67 }
68
76 bool empty() const
77 {
78 scoped_lock<spinlock, spinlock> __guard(_lock_ready, _lock_running);
79 return _ready_task.empty() && _runing_states.empty() && _timer->empty();
80 }
81
85 timer_manager* timer() const noexcept
86 {
87 return _timer.get();
88 }
89
90 #ifndef DOXYGEN_SKIP_PROPERTY
91 void add_generator(state_base_t* sptr);
92 void del_final(state_base_t* sptr);
93 std::unique_ptr<task_t> del_switch(state_base_t* sptr);
94 void add_switch(std::unique_ptr<task_t> task);
95 task_t* find_task(state_base_t* sptr) const noexcept;
96
97 friend struct local_scheduler_t;
98 protected:
99 scheduler_t();
100 public:
101 ~scheduler_t();
102
103 scheduler_t(scheduler_t&& right_) = delete;
104 scheduler_t& operator = (scheduler_t&& right_) = delete;
105 scheduler_t(const scheduler_t&) = delete;
106 scheduler_t& operator = (const scheduler_t&) = delete;
107
108 static scheduler_t g_scheduler;
109 #endif //DOXYGEN_SKIP_PROPERTY
110 };
111
119 struct local_scheduler_t
120 {
124 local_scheduler_t();
125
129 local_scheduler_t(scheduler_t & sch);
130
134 ~local_scheduler_t();
135
136 local_scheduler_t(local_scheduler_t&& right_) = delete;
137 local_scheduler_t& operator = (local_scheduler_t&& right_) = delete;
138 local_scheduler_t(const local_scheduler_t&) = delete;
139 local_scheduler_t& operator = (const local_scheduler_t&) = delete;
140 private:
141 scheduler_t* _scheduler_ptr;
142 };
143 }
resumef::timer_manager
定时器管理器。
Definition: timer.h:84
resumef::scheduler_t
协程调度器。
Definition: scheduler.h:10
resumef::local_scheduler_t::~local_scheduler_t
~local_scheduler_t()
如果当前线程绑定的调度器由local_scheduler_t所创建，则会销毁调度器，并解绑线程。
resumef::scheduler_t::empty
bool empty() const
判断所有协程是否运行完毕。
Definition: scheduler.h:76
resumef::scheduler_t::run_one_batch
void run_one_batch()
运行一批准备妥当的协程。
resumef::scheduler_t::run_until_notask
void run_until_notask()
循环运行所有的协程，直到所有协程都运行完成。
resumef::state_base_t
state基类，state用于在协程的promise和future之间共享数据。
Definition: state.h:8
resumef::scheduler_t::timer
timer_manager * timer() const noexcept
获得定时管理器。
Definition: scheduler.h:85
resumef::counted_ptr
专用与state的智能计数指针，通过管理state内嵌的引用计数来管理state的生存期。
Definition: counted_ptr.h:9
resumef::spinlock
一个自旋锁实现。
Definition: spinlock.h:14
resumef::local_scheduler_t
创建一个线程相关的调度器。
Definition: scheduler.h:119
resumef::local_scheduler_t::local_scheduler_t
local_scheduler_t()
尽可能的创建一个线程相关的调度器。
resumef::scheduler_t::operator+
task_t * operator+(_Ty &&coro)
将一个协程加入到调度器里开始运行。
Definition: scheduler.h:61