diff options
author | Max Kellermann <max@musicpd.org> | 2017-02-09 21:26:55 +0100 |
---|---|---|
committer | Max Kellermann <max@musicpd.org> | 2017-02-09 21:26:55 +0100 |
commit | 14986b153a0480aa3facceb3a1b94d946ef0a928 (patch) | |
tree | 0767a412fc46795d7df295b9d6d65f829f9e3966 /src/event | |
parent | 9e503b21c1d7bd2288bd8834ffd437b9c2200825 (diff) |
event/Loop: use std::lock_guard
Diffstat (limited to 'src/event')
-rw-r--r-- | src/event/Loop.cxx | 64 |
1 files changed, 32 insertions, 32 deletions
diff --git a/src/event/Loop.cxx b/src/event/Loop.cxx index ada820c2d..5593358a9 100644 --- a/src/event/Loop.cxx +++ b/src/event/Loop.cxx @@ -181,17 +181,17 @@ EventLoop::Run() /* try to handle DeferredMonitors without WakeFD overhead */ - mutex.lock(); - HandleDeferred(); - busy = false; - const bool _again = again; - mutex.unlock(); - - if (_again) - /* re-evaluate timers because one of the - IdleMonitors may have added a new - timeout */ - continue; + { + const std::lock_guard<Mutex> lock(mutex); + HandleDeferred(); + busy = false; + + if (again) + /* re-evaluate timers because one of + the IdleMonitors may have added a + new timeout */ + continue; + } /* wait for new event */ @@ -199,9 +199,10 @@ EventLoop::Run() now = std::chrono::steady_clock::now(); - mutex.lock(); - busy = true; - mutex.unlock(); + { + const std::lock_guard<Mutex> lock(mutex); + busy = true; + } /* invoke sockets */ for (int i = 0; i < poll_result.GetSize(); ++i) { @@ -230,23 +231,24 @@ EventLoop::Run() void EventLoop::AddDeferred(DeferredMonitor &d) { - mutex.lock(); - if (d.pending) { - mutex.unlock(); - return; - } + bool must_wake; + + { + const std::lock_guard<Mutex> lock(mutex); + if (d.pending) + return; - assert(std::find(deferred.begin(), - deferred.end(), &d) == deferred.end()); + assert(std::find(deferred.begin(), + deferred.end(), &d) == deferred.end()); - /* we don't need to wake up the EventLoop if another - DeferredMonitor has already done it */ - const bool must_wake = !busy && deferred.empty(); + /* we don't need to wake up the EventLoop if another + DeferredMonitor has already done it */ + must_wake = !busy && deferred.empty(); - d.pending = true; - deferred.push_back(&d); - again = true; - mutex.unlock(); + d.pending = true; + deferred.push_back(&d); + again = true; + } if (must_wake) wake_fd.Write(); @@ -281,9 +283,8 @@ EventLoop::HandleDeferred() deferred.pop_front(); m.pending = false; - mutex.unlock(); + const ScopeUnlock unlock(mutex); m.RunDeferred(); - mutex.lock(); } } @@ -294,9 +295,8 @@ EventLoop::OnSocketReady(gcc_unused unsigned flags) wake_fd.Read(); - mutex.lock(); + const std::lock_guard<Mutex> lock(mutex); HandleDeferred(); - mutex.unlock(); return true; } |