Debian Codegraph @

Modules/ThirdParty/VNL/src/vxl/vcl/internal/vcl_atomic_count_pthreads.h

raw
1 #ifndef vcl_atomic_count_pthreads_h_
2 #define vcl_atomic_count_pthreads_h_
3 //:
4 // \file
5 // \brief thread/SMP safe reference counter
6 // \author www.boost.org
7 // \verbatim
8 // Modifications
9 // Gehua Yang (DualAlign) - 28 Aug. 2008 - first port from Boost 1.36.0
10 // \endverbatim
11 //
12 // boost/detail/atomic_count_pthreads.hpp
13 //
14 // Copyright (c) 2001, 2002 Peter Dimov and Multi Media Ltd.
15 //
16 // Distributed under the Boost Software License, Version 1.0. (See
17 // accompanying file LICENSE_1_0.txt or copy at
18 // http://www.boost.org/LICENSE_1_0.txt)
19 //
20
21 #include <pthread.h>
22
23 //
24 // The generic pthread_mutex-based implementation sometimes leads to
25 // inefficiencies. Example: a class with two vcl_atomic_count members
26 // can get away with a single mutex.
27 //
28 // Users can detect this situation by checking BOOST_AC_USE_PTHREADS.
29 //
30
31 class vcl_atomic_count
32 {
33 private:
34
35 class scoped_lock
36 {
37 public:
38
39 scoped_lock(pthread_mutex_t & m): m_(m)
40 {
41 pthread_mutex_lock(&m_);
42 }
43
44 ~scoped_lock()
45 {
46 pthread_mutex_unlock(&m_);
47 }
48
49 private:
50
51 pthread_mutex_t & m_;
52 };
53
54 public:
55
56 explicit vcl_atomic_count(long v): value_(v)
57 {
58 pthread_mutex_init(&mutex_, 0);
59 }
60
61 ~vcl_atomic_count()
62 {
63 pthread_mutex_destroy(&mutex_);
64 }
65
66 void operator++()
67 {
68 scoped_lock lock(mutex_);
69 ++value_;
70 }
71
72 long operator--()
73 {
74 scoped_lock lock(mutex_);
75 return --value_;
76 }
77
78 operator long() const
79 {
80 scoped_lock lock(mutex_);
81 return value_;
82 }
83
84 private:
85
86 vcl_atomic_count(vcl_atomic_count const &);
87 vcl_atomic_count & operator=(vcl_atomic_count const &);
88
89 mutable pthread_mutex_t mutex_;
90 long value_;
91 };
92
93 #endif // #ifndef vcl_atomic_count_pthreads_h_