Chris@16: #ifndef BOOST_SMART_PTR_DETAIL_SPINLOCK_PT_HPP_INCLUDED Chris@16: #define BOOST_SMART_PTR_DETAIL_SPINLOCK_PT_HPP_INCLUDED Chris@16: Chris@16: // MS compatible compilers support #pragma once Chris@16: Chris@16: #if defined(_MSC_VER) && (_MSC_VER >= 1020) Chris@16: # pragma once Chris@16: #endif Chris@16: Chris@16: // Chris@16: // Copyright (c) 2008 Peter Dimov Chris@16: // Chris@16: // Distributed under the Boost Software License, Version 1.0. Chris@16: // See accompanying file LICENSE_1_0.txt or copy at Chris@16: // http://www.boost.org/LICENSE_1_0.txt) Chris@16: // Chris@16: Chris@16: #include Chris@16: Chris@16: namespace boost Chris@16: { Chris@16: Chris@16: namespace detail Chris@16: { Chris@16: Chris@16: class spinlock Chris@16: { Chris@16: public: Chris@16: Chris@16: pthread_mutex_t v_; Chris@16: Chris@16: public: Chris@16: Chris@16: bool try_lock() Chris@16: { Chris@16: return pthread_mutex_trylock( &v_ ) == 0; Chris@16: } Chris@16: Chris@16: void lock() Chris@16: { Chris@16: pthread_mutex_lock( &v_ ); Chris@16: } Chris@16: Chris@16: void unlock() Chris@16: { Chris@16: pthread_mutex_unlock( &v_ ); Chris@16: } Chris@16: Chris@16: public: Chris@16: Chris@16: class scoped_lock Chris@16: { Chris@16: private: Chris@16: Chris@16: spinlock & sp_; Chris@16: Chris@16: scoped_lock( scoped_lock const & ); Chris@16: scoped_lock & operator=( scoped_lock const & ); Chris@16: Chris@16: public: Chris@16: Chris@16: explicit scoped_lock( spinlock & sp ): sp_( sp ) Chris@16: { Chris@16: sp.lock(); Chris@16: } Chris@16: Chris@16: ~scoped_lock() Chris@16: { Chris@16: sp_.unlock(); Chris@16: } Chris@16: }; Chris@16: }; Chris@16: Chris@16: } // namespace detail Chris@16: } // namespace boost Chris@16: Chris@16: #define BOOST_DETAIL_SPINLOCK_INIT { PTHREAD_MUTEX_INITIALIZER } Chris@16: Chris@16: #endif // #ifndef BOOST_SMART_PTR_DETAIL_SPINLOCK_PT_HPP_INCLUDED