• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #ifndef BOOST_SMART_PTR_DETAIL_ATOMIC_COUNT_PTHREADS_HPP_INCLUDED
2 #define BOOST_SMART_PTR_DETAIL_ATOMIC_COUNT_PTHREADS_HPP_INCLUDED
3 
4 //
5 //  boost/detail/atomic_count_pthreads.hpp
6 //
7 //  Copyright (c) 2001, 2002 Peter Dimov and Multi Media Ltd.
8 //
9 // Distributed under the Boost Software License, Version 1.0. (See
10 // accompanying file LICENSE_1_0.txt or copy at
11 // http://www.boost.org/LICENSE_1_0.txt)
12 //
13 
14 #include <boost/assert.hpp>
15 #include <pthread.h>
16 
17 #if defined(BOOST_SP_REPORT_IMPLEMENTATION)
18 
19 #include <boost/config/pragma_message.hpp>
20 BOOST_PRAGMA_MESSAGE("Using pthread_mutex atomic_count")
21 
22 #endif
23 
24 //
25 //  The generic pthread_mutex-based implementation sometimes leads to
26 //    inefficiencies. Example: a class with two atomic_count members
27 //    can get away with a single mutex.
28 //
29 //  Users can detect this situation by checking BOOST_AC_USE_PTHREADS.
30 //
31 
32 namespace boost
33 {
34 
35 namespace detail
36 {
37 
38 class atomic_count
39 {
40 private:
41 
42     class scoped_lock
43     {
44     public:
45 
scoped_lock(pthread_mutex_t & m)46         scoped_lock(pthread_mutex_t & m): m_(m)
47         {
48             BOOST_VERIFY( pthread_mutex_lock( &m_ ) == 0 );
49         }
50 
~scoped_lock()51         ~scoped_lock()
52         {
53             BOOST_VERIFY( pthread_mutex_unlock( &m_ ) == 0 );
54         }
55 
56     private:
57 
58         pthread_mutex_t & m_;
59     };
60 
61 public:
62 
atomic_count(long v)63     explicit atomic_count(long v): value_(v)
64     {
65         BOOST_VERIFY( pthread_mutex_init( &mutex_, 0 ) == 0 );
66     }
67 
~atomic_count()68     ~atomic_count()
69     {
70         BOOST_VERIFY( pthread_mutex_destroy( &mutex_ ) == 0 );
71     }
72 
operator ++()73     long operator++()
74     {
75         scoped_lock lock(mutex_);
76         return ++value_;
77     }
78 
operator --()79     long operator--()
80     {
81         scoped_lock lock(mutex_);
82         return --value_;
83     }
84 
operator long() const85     operator long() const
86     {
87         scoped_lock lock(mutex_);
88         return value_;
89     }
90 
91 private:
92 
93     atomic_count(atomic_count const &);
94     atomic_count & operator=(atomic_count const &);
95 
96     mutable pthread_mutex_t mutex_;
97     long value_;
98 };
99 
100 } // namespace detail
101 
102 } // namespace boost
103 
104 #endif // #ifndef BOOST_SMART_PTR_DETAIL_ATOMIC_COUNT_PTHREADS_HPP_INCLUDED
105