Commit 14986b15 authored by Max Kellermann's avatar Max Kellermann

event/Loop: use std::lock_guard

parent 9e503b21
...@@ -181,17 +181,17 @@ EventLoop::Run() ...@@ -181,17 +181,17 @@ EventLoop::Run()
/* try to handle DeferredMonitors without WakeFD /* try to handle DeferredMonitors without WakeFD
overhead */ overhead */
mutex.lock(); {
HandleDeferred(); const std::lock_guard<Mutex> lock(mutex);
busy = false; HandleDeferred();
const bool _again = again; busy = false;
mutex.unlock();
if (again)
if (_again) /* re-evaluate timers because one of
/* re-evaluate timers because one of the the IdleMonitors may have added a
IdleMonitors may have added a new new timeout */
timeout */ continue;
continue; }
/* wait for new event */ /* wait for new event */
...@@ -199,9 +199,10 @@ EventLoop::Run() ...@@ -199,9 +199,10 @@ EventLoop::Run()
now = std::chrono::steady_clock::now(); now = std::chrono::steady_clock::now();
mutex.lock(); {
busy = true; const std::lock_guard<Mutex> lock(mutex);
mutex.unlock(); busy = true;
}
/* invoke sockets */ /* invoke sockets */
for (int i = 0; i < poll_result.GetSize(); ++i) { for (int i = 0; i < poll_result.GetSize(); ++i) {
...@@ -230,23 +231,24 @@ EventLoop::Run() ...@@ -230,23 +231,24 @@ EventLoop::Run()
void void
EventLoop::AddDeferred(DeferredMonitor &d) EventLoop::AddDeferred(DeferredMonitor &d)
{ {
mutex.lock(); bool must_wake;
if (d.pending) {
mutex.unlock(); {
return; const std::lock_guard<Mutex> lock(mutex);
} if (d.pending)
return;
assert(std::find(deferred.begin(), assert(std::find(deferred.begin(),
deferred.end(), &d) == deferred.end()); deferred.end(), &d) == deferred.end());
/* we don't need to wake up the EventLoop if another /* we don't need to wake up the EventLoop if another
DeferredMonitor has already done it */ DeferredMonitor has already done it */
const bool must_wake = !busy && deferred.empty(); must_wake = !busy && deferred.empty();
d.pending = true; d.pending = true;
deferred.push_back(&d); deferred.push_back(&d);
again = true; again = true;
mutex.unlock(); }
if (must_wake) if (must_wake)
wake_fd.Write(); wake_fd.Write();
...@@ -281,9 +283,8 @@ EventLoop::HandleDeferred() ...@@ -281,9 +283,8 @@ EventLoop::HandleDeferred()
deferred.pop_front(); deferred.pop_front();
m.pending = false; m.pending = false;
mutex.unlock(); const ScopeUnlock unlock(mutex);
m.RunDeferred(); m.RunDeferred();
mutex.lock();
} }
} }
...@@ -294,9 +295,8 @@ EventLoop::OnSocketReady(gcc_unused unsigned flags) ...@@ -294,9 +295,8 @@ EventLoop::OnSocketReady(gcc_unused unsigned flags)
wake_fd.Read(); wake_fd.Read();
mutex.lock(); const std::lock_guard<Mutex> lock(mutex);
HandleDeferred(); HandleDeferred();
mutex.unlock();
return true; return true;
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment