spinlock_pt.hpp
Go to the documentation of this file.
00001 #ifndef BOOST_SMART_PTR_DETAIL_SPINLOCK_PT_HPP_INCLUDED
00002 #define BOOST_SMART_PTR_DETAIL_SPINLOCK_PT_HPP_INCLUDED
00003 
00004 // MS compatible compilers support #pragma once
00005 
00006 #if defined(_MSC_VER) && (_MSC_VER >= 1020)
00007 # pragma once
00008 #endif
00009 
00010 //
00011 //  Copyright (c) 2008 Peter Dimov
00012 //
00013 //  Distributed under the Boost Software License, Version 1.0.
00014 //  See accompanying file LICENSE_1_0.txt or copy at
00015 //  http://www.boost.org/LICENSE_1_0.txt)
00016 //
00017 
00018 #include <pthread.h>
00019 
00020 namespace boost
00021 {
00022 
00023 namespace detail
00024 {
00025 
00026 class spinlock
00027 {
00028 public:
00029 
00030     pthread_mutex_t v_;
00031 
00032 public:
00033 
00034     bool try_lock()
00035     {
00036         return pthread_mutex_trylock( &v_ ) == 0;
00037     }
00038 
00039     void lock()
00040     {
00041         pthread_mutex_lock( &v_ );
00042     }
00043 
00044     void unlock()
00045     {
00046         pthread_mutex_unlock( &v_ );
00047     }
00048 
00049 public:
00050 
00051     class scoped_lock
00052     {
00053     private:
00054 
00055         spinlock & sp_;
00056 
00057         scoped_lock( scoped_lock const & );
00058         scoped_lock & operator=( scoped_lock const & );
00059 
00060     public:
00061 
00062         explicit scoped_lock( spinlock & sp ): sp_( sp )
00063         {
00064             sp.lock();
00065         }
00066 
00067         ~scoped_lock()
00068         {
00069             sp_.unlock();
00070         }
00071     };
00072 };
00073 
00074 } // namespace detail
00075 } // namespace boost
00076 
00077 #define BOOST_DETAIL_SPINLOCK_INIT { PTHREAD_MUTEX_INITIALIZER }
00078 
00079 #endif // #ifndef BOOST_SMART_PTR_DETAIL_SPINLOCK_PT_HPP_INCLUDED


appl
Author(s): petercai
autogenerated on Tue Jan 7 2014 11:02:29