Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:51:42

0001 #ifndef BOOST_SMART_PTR_DETAIL_ATOMIC_COUNT_PTHREADS_HPP_INCLUDED
0002 #define BOOST_SMART_PTR_DETAIL_ATOMIC_COUNT_PTHREADS_HPP_INCLUDED
0003 
0004 //
0005 //  boost/detail/atomic_count_pthreads.hpp
0006 //
0007 //  Copyright (c) 2001, 2002 Peter Dimov and Multi Media Ltd.
0008 //
0009 // Distributed under the Boost Software License, Version 1.0. (See
0010 // accompanying file LICENSE_1_0.txt or copy at
0011 // http://www.boost.org/LICENSE_1_0.txt)
0012 //
0013 
0014 #include <boost/assert.hpp>
0015 #include <pthread.h>
0016 
0017 #if defined(BOOST_SP_REPORT_IMPLEMENTATION)
0018 
0019 #include <boost/config/pragma_message.hpp>
0020 BOOST_PRAGMA_MESSAGE("Using pthread_mutex atomic_count")
0021 
0022 #endif
0023 
0024 //
0025 //  The generic pthread_mutex-based implementation sometimes leads to
0026 //    inefficiencies. Example: a class with two atomic_count members
0027 //    can get away with a single mutex.
0028 //
0029 //  Users can detect this situation by checking BOOST_AC_USE_PTHREADS.
0030 //
0031 
0032 namespace boost
0033 {
0034 
0035 namespace detail
0036 {
0037 
0038 class atomic_count
0039 {
0040 private:
0041 
0042     class scoped_lock
0043     {
0044     public:
0045 
0046         scoped_lock(pthread_mutex_t & m): m_(m)
0047         {
0048             BOOST_VERIFY( pthread_mutex_lock( &m_ ) == 0 );
0049         }
0050 
0051         ~scoped_lock()
0052         {
0053             BOOST_VERIFY( pthread_mutex_unlock( &m_ ) == 0 );
0054         }
0055 
0056     private:
0057 
0058         pthread_mutex_t & m_;
0059     };
0060 
0061 public:
0062 
0063     explicit atomic_count(long v): value_(v)
0064     {
0065         BOOST_VERIFY( pthread_mutex_init( &mutex_, 0 ) == 0 );
0066     }
0067 
0068     ~atomic_count()
0069     {
0070         BOOST_VERIFY( pthread_mutex_destroy( &mutex_ ) == 0 );
0071     }
0072 
0073     long operator++()
0074     {
0075         scoped_lock lock(mutex_);
0076         return ++value_;
0077     }
0078 
0079     long operator--()
0080     {
0081         scoped_lock lock(mutex_);
0082         return --value_;
0083     }
0084 
0085     operator long() const
0086     {
0087         scoped_lock lock(mutex_);
0088         return value_;
0089     }
0090 
0091 private:
0092 
0093     atomic_count(atomic_count const &);
0094     atomic_count & operator=(atomic_count const &);
0095 
0096     mutable pthread_mutex_t mutex_;
0097     long value_;
0098 };
0099 
0100 } // namespace detail
0101 
0102 } // namespace boost
0103 
0104 #endif // #ifndef BOOST_SMART_PTR_DETAIL_ATOMIC_COUNT_PTHREADS_HPP_INCLUDED