Kaynağa Gözat

改善单协程切换效率表现

tags/2.9.10
tearshark 4 yıl önce
ebeveyn
işleme
c891e67b30
2 değiştirilmiş dosya ile 18 ekleme ve 7 silme
  1. 17
    6
      librf/src/scheduler.cpp
  2. 1
    1
      librf/src/scheduler.h

+ 17
- 6
librf/src/scheduler.cpp Dosyayı Görüntüle

} }
*/ */
void scheduler_t::run_one_batch()
bool scheduler_t::run_one_batch()
{ {
this->_timer->update(); this->_timer->update();
{ {
scoped_lock<spinlock> __guard(_lock_running); scoped_lock<spinlock> __guard(_lock_running);
if (_runing_states.empty()) if (_runing_states.empty())
return;
return false;
std::swap(_cached_states, _runing_states); std::swap(_cached_states, _runing_states);
} }
sptr->resume(); sptr->resume();
_cached_states.clear(); _cached_states.clear();
return true;
} }
void scheduler_t::run_until_notask() void scheduler_t::run_until_notask()
{ {
while (!this->empty())
for(;;)
{ {
this->run_one_batch();
std::this_thread::yield();
}
//介于网上有人做评测,导致单协程切换数据很难看,那就注释掉吧。
//std::this_thread::yield();
if (this->run_one_batch()) continue; //当前运行了一个state,则认为还可能有任务未完成
{
scoped_lock<spinlock> __guard(_lock_ready);
if (!_ready_task.empty()) continue; //当前还存在task,则必然还有任务未完成
}
if (!_timer->empty()) continue; //定时器不为空,也需要等待定时器触发
break;
};
} }
scheduler_t scheduler_t::g_scheduler; scheduler_t scheduler_t::g_scheduler;

+ 1
- 1
librf/src/scheduler.h Dosyayı Görüntüle

* @details 这是协程调度器提供的主要接口。同一个调度器非线程安全,不可重入。\n * @details 这是协程调度器提供的主要接口。同一个调度器非线程安全,不可重入。\n
* 调用者要保证此函数始终在同一个线程里调用。 * 调用者要保证此函数始终在同一个线程里调用。
*/ */
void run_one_batch();
bool run_one_batch();
/** /**
* @brief 循环运行所有的协程,直到所有协程都运行完成。 * @brief 循环运行所有的协程,直到所有协程都运行完成。

Loading…
İptal
Kaydet