[core] factor out RunLoop::wake()

This commit is contained in:
Konstantin Käfer
2018-01-23 14:30:01 -08:00
parent 3ef7dab1a8
commit 78f8fd88b4
5 changed files with 16 additions and 24 deletions

View File

@@ -76,16 +76,20 @@ private:
using Queue = std::queue<std::shared_ptr<WorkTask>>;
void push(std::shared_ptr<WorkTask>);
// Wakes up the RunLoop so that it starts processing items in the queue.
void wake();
void withMutex(std::function<void()>&& fn) {
// Adds a WorkTask to the queue, and wakes it up.
void push(std::shared_ptr<WorkTask> task) {
std::lock_guard<std::mutex> lock(mutex);
fn();
queue.push(std::move(task));
wake();
}
void process() {
std::unique_lock<std::mutex> lock(mutex);
Queue queue_;
withMutex([&] { queue_.swap(queue); });
lock.unlock();
while (!queue_.empty()) {
(*(queue_.front()))();

View File

@@ -216,11 +216,8 @@ LOOP_HANDLE RunLoop::getLoopHandle() {
return Get()->impl.get();
}
void RunLoop::push(std::shared_ptr<WorkTask> task) {
withMutex([&] {
queue.push(std::move(task));
impl->wake();
});
void RunLoop::wake() {
impl->wake();
}
void RunLoop::run() {

View File

@@ -29,11 +29,8 @@ RunLoop::~RunLoop() {
Scheduler::SetCurrent(nullptr);
}
void RunLoop::push(std::shared_ptr<WorkTask> task) {
withMutex([&] {
queue.push(std::move(task));
impl->async->send();
});
void RunLoop::wake() {
impl->async->send();
}
void RunLoop::run() {

View File

@@ -129,11 +129,8 @@ LOOP_HANDLE RunLoop::getLoopHandle() {
return Get()->impl->loop;
}
void RunLoop::push(std::shared_ptr<WorkTask> task) {
withMutex([&] {
queue.push(std::move(task));
impl->async->send();
});
void RunLoop::wake() {
impl->async->send();
}
void RunLoop::run() {

View File

@@ -52,11 +52,8 @@ LOOP_HANDLE RunLoop::getLoopHandle() {
return nullptr;
}
void RunLoop::push(std::shared_ptr<WorkTask> task) {
withMutex([&] {
queue.push(std::move(task));
impl->async->send();
});
void RunLoop::wake() {
impl->async->send();
}
void RunLoop::run() {