From 21f4f49c7aebbd3c716a4065a6d5b2c94c022008 Mon Sep 17 00:00:00 2001 From: Yuri Kunde Schlesner Date: Sat, 18 Feb 2017 18:27:37 -0800 Subject: [PATCH] SynchronizedWrapper: Add Lock convenience method --- src/common/synchronized_wrapper.h | 43 ++++++++++++++++++------------- 1 file changed, 25 insertions(+), 18 deletions(-) diff --git a/src/common/synchronized_wrapper.h b/src/common/synchronized_wrapper.h index 04b4f2e51..4a1984c46 100644 --- a/src/common/synchronized_wrapper.h +++ b/src/common/synchronized_wrapper.h @@ -9,25 +9,8 @@ namespace Common { -/** - * Wraps an object, only allowing access to it via a locking reference wrapper. Good to ensure no - * one forgets to lock a mutex before acessing an object. To access the wrapped object construct a - * SyncronizedRef on this wrapper. Inspired by Rust's Mutex type - * (http://doc.rust-lang.org/std/sync/struct.Mutex.html). - */ template -class SynchronizedWrapper { -public: - template - SynchronizedWrapper(Args&&... args) : data(std::forward(args)...) {} - -private: - template - friend class SynchronizedRef; - - std::mutex mutex; - T data; -}; +class SynchronizedWrapper; /** * Synchronized reference, that keeps a SynchronizedWrapper's mutex locked during its lifetime. This @@ -75,4 +58,28 @@ private: SynchronizedWrapper* wrapper; }; +/** + * Wraps an object, only allowing access to it via a locking reference wrapper. Good to ensure no + * one forgets to lock a mutex before acessing an object. To access the wrapped object construct a + * SyncronizedRef on this wrapper. Inspired by Rust's Mutex type + * (http://doc.rust-lang.org/std/sync/struct.Mutex.html). + */ +template +class SynchronizedWrapper { +public: + template + SynchronizedWrapper(Args&&... args) : data(std::forward(args)...) {} + + SynchronizedRef Lock() { + return {*this}; + } + +private: + template + friend class SynchronizedRef; + + std::mutex mutex; + T data; +}; + } // namespace Common