Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- void Scheduler::RunLoop() {
- while (!run_queue_.IsEmpty() || !sleep_queue_.IsEmpty()) {
- if (!sleep_queue_.IsEmpty()) {
- SleepyFiber next_sleepy = sleep_queue_.GetTopFiber();
- if (std::chrono::steady_clock::now() >= next_sleepy.expected_time_) {
- next_sleepy.fiber_->SetState(FiberState::Runnable);
- Schedule(next_sleepy.fiber_);
- sleep_queue_.PopTopFiber();
- }
- }
- if (!run_queue_.IsEmpty()) {
- Fiber* next = run_queue_.PopFront();
- SwitchTo(next);
- Reschedule(next);
- }
- if (run_queue_.IsEmpty() && !sleep_queue_.IsEmpty()) {
- SleepyFiber next_sleepy = sleep_queue_.GetTopFiber();
- auto sleep_time =
- next_sleepy.expected_time_ - std::chrono::steady_clock::now();
- if (next_sleepy.expected_time_ - sleep_time <
- next_sleepy.expected_time_) {
- std::this_thread::sleep_for(next_sleepy.expected_time_ -
- std::chrono::steady_clock::now());
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement