Chris@16: // Copyright (C) 2000 Stephen Cleary Chris@16: // Chris@16: // Distributed under the Boost Software License, Version 1.0. (See Chris@16: // accompanying file LICENSE_1_0.txt or copy at Chris@16: // http://www.boost.org/LICENSE_1_0.txt) Chris@16: // Chris@16: // See http://www.boost.org for updates, documentation, and revision history. Chris@16: Chris@16: #ifndef BOOST_POOL_GUARD_HPP Chris@16: #define BOOST_POOL_GUARD_HPP Chris@16: Chris@16: /*! Chris@16: \file Chris@16: \brief Extremely Light-Weight guard class. Chris@16: \details Auto-lock/unlock-er Chris@16: detail/guard.hpp provides a type guard Chris@16: that allows scoped access to the Mutex's locking and unlocking operations. Chris@16: It is used to ensure that a Mutex is unlocked, even if an exception is thrown. Chris@16: */ Chris@16: Chris@16: namespace boost { Chris@16: Chris@16: namespace details { Chris@16: namespace pool { Chris@16: Chris@16: template //!< \tparam Mutex (platform-specific) mutex class. Chris@16: class guard Chris@16: { //! Locks the mutex, binding guard to Mutex. Chris@16: /*! Example: Chris@16: Given a (platform-specific) mutex class, we can wrap code as follows: Chris@16: Chris@16: extern mutex global_lock; Chris@16: Chris@16: static void f() Chris@16: { Chris@16: boost::details::pool::guard g(global_lock); Chris@16: // g's constructor locks "global_lock" Chris@16: Chris@16: ... // do anything: Chris@16: // throw exceptions Chris@16: // return Chris@16: // or just fall through Chris@16: } // g's destructor unlocks "global_lock" Chris@16: */ Chris@16: private: Chris@16: Mutex & mtx; Chris@16: Chris@16: guard(const guard &); //!< Guards the mutex, ensuring unlocked on destruction, even if exception is thrown. Chris@16: void operator=(const guard &); Chris@16: Chris@16: public: Chris@16: explicit guard(Mutex & nmtx) Chris@16: :mtx(nmtx) Chris@16: { //! Locks the mutex of the guard class. Chris@16: mtx.lock(); Chris@16: } Chris@16: Chris@16: ~guard() Chris@16: { //! destructor unlocks the mutex of the guard class. Chris@16: mtx.unlock(); Chris@16: } Chris@16: }; // class guard Chris@16: Chris@16: } // namespace pool Chris@16: } // namespace details Chris@16: Chris@16: } // namespace boost Chris@16: Chris@16: #endif