00001 #ifndef BOOST_DETAIL_ATOMIC_COUNT_PTHREADS_HPP_INCLUDED 00002 #define BOOST_DETAIL_ATOMIC_COUNT_PTHREADS_HPP_INCLUDED 00003 00004 // 00005 // boost/detail/atomic_count_pthreads.hpp 00006 // 00007 // Copyright (c) 2001, 2002 Peter Dimov and Multi Media Ltd. 00008 // 00009 // Permission to copy, use, modify, sell and distribute this software 00010 // is granted provided this copyright notice appears in all copies. 00011 // This software is provided "as is" without express or implied 00012 // warranty, and with no claim as to its suitability for any purpose. 00013 // 00014 00015 #include <pthread.h> 00016 00017 // 00018 // The generic pthread_mutex-based implementation sometimes leads to 00019 // inefficiencies. Example: a class with two atomic_count members 00020 // can get away with a single mutex. 00021 // 00022 // Users can detect this situation by checking BOOST_AC_USE_PTHREADS. 00023 // 00024 00025 namespace boost 00026 { 00027 00028 namespace detail 00029 { 00030 00031 class atomic_count 00032 { 00033 private: 00034 00035 class scoped_lock 00036 { 00037 public: 00038 00039 scoped_lock(pthread_mutex_t & m): m_(m) 00040 { 00041 pthread_mutex_lock(&m_); 00042 } 00043 00044 ~scoped_lock() 00045 { 00046 pthread_mutex_unlock(&m_); 00047 } 00048 00049 private: 00050 00051 pthread_mutex_t & m_; 00052 }; 00053 00054 public: 00055 00056 explicit atomic_count(long v): value_(v) 00057 { 00058 pthread_mutex_init(&mutex_, 0); 00059 } 00060 00061 ~atomic_count() 00062 { 00063 pthread_mutex_destroy(&mutex_); 00064 } 00065 00066 void operator++() 00067 { 00068 scoped_lock lock(mutex_); 00069 ++value_; 00070 } 00071 00072 long operator--() 00073 { 00074 scoped_lock lock(mutex_); 00075 return --value_; 00076 } 00077 00078 operator long() const 00079 { 00080 scoped_lock lock(mutex_); 00081 return value_; 00082 } 00083 00084 private: 00085 00086 atomic_count(atomic_count const &); 00087 atomic_count & operator=(atomic_count const &); 00088 00089 mutable pthread_mutex_t mutex_; 00090 long value_; 00091 }; 00092 00093 } // namespace detail 00094 00095 } // namespace boost 00096 00097 #endif // #ifndef BOOST_DETAIL_ATOMIC_COUNT_PTHREADS_HPP_INCLUDED
1.5.1