diff --git a/beast/thread/SharedData.h b/beast/thread/SharedData.h index 6039785517..8c4b8e921e 100644 --- a/beast/thread/SharedData.h +++ b/beast/thread/SharedData.h @@ -48,13 +48,20 @@ class CriticalSection; ConstAccess acquires a shared lock on the mutex associated with the container. - - UnlockedAccess + - ConstUnlockedAccess Provides access to the shared state via a const reference or pointer. No locking is performed. It is the callers responsibility to ensure that the operation is synchronized. This can be useful for diagnostics or assertions, or for when it is known that no other threads can access the data. + - UnlockedAccess + Provides access to the shared state via a reference or pointer. + No locking is performed. It is the callers responsibility to ensure that + the operation is synchronized. This can be useful for diagnostics or + assertions, or for when it is known that no other threads can access + the data. + Usage example: @code @@ -115,6 +122,7 @@ public: class Access; class ConstAccess; class UnlockedAccess; + class ConstUnlockedAccess; /** Create a shared data container. Up to 8 parameters can be specified in the constructor. These parameters @@ -174,44 +182,14 @@ public: explicit Access (SharedData& state) : m_state (state) , m_lock (m_state.m_mutex) - { - } + { } - /** Returns a const reference to the data. */ - Data const& get () const - { - return m_state.m_value; - } - - /** Returns a const reference to the data. */ - Data const& operator* () const - { - return get (); - } - - /** Returns a const pointer to the data. */ - Data const* operator-> () const - { - return &get (); - } - - /** Returns a non-const reference to the data. */ - Data& get () - { - return m_state.m_value; - } - - /** Returns a non-const reference to the data. */ - Data& operator* () - { - return get (); - } - - /** Returns a non-const pointer to the data. */ - Data* operator-> () - { - return &get (); - } + Data const& get () const { return m_state.m_value; } + Data const& operator* () const { return get (); } + Data const* operator-> () const { return &get (); } + Data& get () { return m_state.m_value; } + Data& operator* () { return get (); } + Data* operator-> () { return &get (); } private: SharedData& m_state; @@ -231,26 +209,11 @@ public: explicit ConstAccess (SharedData const volatile& state) : m_state (const_cast (state)) , m_lock (m_state.m_mutex) - { - } + { } - /** Returns a const reference to the data. */ - Data const& get () const - { - return m_state.m_value; - } - - /** Returns a const reference to the data. */ - Data const& operator* () const - { - return get (); - } - - /** Returns a const pointer to the data. */ - Data const* operator-> () const - { - return &get (); - } + Data const& get () const { return m_state.m_value; } + Data const& operator* () const { return get (); } + Data const* operator-> () const { return &get (); } private: SharedData const& m_state; @@ -263,37 +226,47 @@ private: This acquires a shared lock on the underlying mutex. */ template -class SharedData ::UnlockedAccess : public Uncopyable +class SharedData ::ConstUnlockedAccess : public Uncopyable { public: /** Create an UnlockedAccess from the specified SharedData */ - explicit UnlockedAccess (SharedData const volatile& state) + explicit ConstUnlockedAccess (SharedData const volatile& state) : m_state (const_cast (state)) - { - } + { } - /** Returns a const reference to the data. */ - Data const& get () const - { - return m_state.m_value; - } - - /** Returns a const reference to the data. */ - Data const& operator* () const - { - return get (); - } - - /** Returns a const pointer to the data. */ - Data const* operator-> () const - { - return &get (); - } + Data const& get () const { return m_state.m_value; } + Data const& operator* () const { return get (); } + Data const* operator-> () const { return &get (); } private: SharedData const& m_state; }; +//------------------------------------------------------------------------------ + +/** Provides access to the contents of a SharedData. + This acquires a shared lock on the underlying mutex. +*/ +template +class SharedData ::UnlockedAccess : public Uncopyable +{ +public: + /** Create an UnlockedAccess from the specified SharedData */ + explicit UnlockedAccess (SharedData& state) + : m_state (state) + { } + + Data const& get () const { return m_state.m_value; } + Data const& operator* () const { return get (); } + Data const* operator-> () const { return &get (); } + Data& get () { return m_state.m_value; } + Data& operator* () { return get (); } + Data* operator-> () { return &get (); } + +private: + SharedData& m_state; +}; + } #endif diff --git a/modules/beast_core/thread/impl/TrackedMutex.cpp b/modules/beast_core/thread/impl/TrackedMutex.cpp index bf62599dca..98da8cf0d5 100644 --- a/modules/beast_core/thread/impl/TrackedMutex.cpp +++ b/modules/beast_core/thread/impl/TrackedMutex.cpp @@ -395,7 +395,7 @@ void TrackedMutex::acquired (char const* fileName, int lineNumber) const noexcep else { // Thread already had ownership of the mutex. - bassert (SharedState::UnlockedAccess (m_state)->thread == &thread); + bassert (SharedState::ConstUnlockedAccess (m_state)->thread == &thread); // If this goes off it means we counted wrong. bassert (thread.refCount >= m_count);