|
1 #ifndef BOOST_DETAIL_ATOMIC_COUNT_HPP_INCLUDED |
|
2 #define BOOST_DETAIL_ATOMIC_COUNT_HPP_INCLUDED |
|
3 |
|
4 // MS compatible compilers support #pragma once |
|
5 |
|
6 #if defined(_MSC_VER) && (_MSC_VER >= 1020) |
|
7 # pragma once |
|
8 #endif |
|
9 |
|
10 // |
|
11 // boost/detail/atomic_count.hpp - thread/SMP safe reference counter |
|
12 // |
|
13 // Copyright (c) 2001, 2002 Peter Dimov and Multi Media Ltd. |
|
14 // |
|
15 // Distributed under the Boost Software License, Version 1.0. (See |
|
16 // accompanying file LICENSE_1_0.txt or copy at |
|
17 // http://www.boost.org/LICENSE_1_0.txt) |
|
18 // |
|
19 // typedef <implementation-defined> boost::detail::atomic_count; |
|
20 // |
|
21 // atomic_count a(n); |
|
22 // |
|
23 // (n is convertible to long) |
|
24 // |
|
25 // Effects: Constructs an atomic_count with an initial value of n |
|
26 // |
|
27 // a; |
|
28 // |
|
29 // Returns: (long) the current value of a |
|
30 // |
|
31 // ++a; |
|
32 // |
|
33 // Effects: Atomically increments the value of a |
|
34 // Returns: nothing |
|
35 // |
|
36 // --a; |
|
37 // |
|
38 // Effects: Atomically decrements the value of a |
|
39 // Returns: (long) zero if the new value of a is zero, |
|
40 // unspecified non-zero value otherwise (usually the new value) |
|
41 // |
|
42 // Important note: when --a returns zero, it must act as a |
|
43 // read memory barrier (RMB); i.e. the calling thread must |
|
44 // have a synchronized view of the memory |
|
45 // |
|
46 // On Intel IA-32 (x86) memory is always synchronized, so this |
|
47 // is not a problem. |
|
48 // |
|
49 // On many architectures the atomic instructions already act as |
|
50 // a memory barrier. |
|
51 // |
|
52 // This property is necessary for proper reference counting, since |
|
53 // a thread can update the contents of a shared object, then |
|
54 // release its reference, and another thread may immediately |
|
55 // release the last reference causing object destruction. |
|
56 // |
|
57 // The destructor needs to have a synchronized view of the |
|
58 // object to perform proper cleanup. |
|
59 // |
|
60 // Original example by Alexander Terekhov: |
|
61 // |
|
62 // Given: |
|
63 // |
|
64 // - a mutable shared object OBJ; |
|
65 // - two threads THREAD1 and THREAD2 each holding |
|
66 // a private smart_ptr object pointing to that OBJ. |
|
67 // |
|
68 // t1: THREAD1 updates OBJ (thread-safe via some synchronization) |
|
69 // and a few cycles later (after "unlock") destroys smart_ptr; |
|
70 // |
|
71 // t2: THREAD2 destroys smart_ptr WITHOUT doing any synchronization |
|
72 // with respect to shared mutable object OBJ; OBJ destructors |
|
73 // are called driven by smart_ptr interface... |
|
74 // |
|
75 |
|
76 #include <boost/config.hpp> |
|
77 |
|
78 #ifndef BOOST_HAS_THREADS |
|
79 |
|
80 namespace boost |
|
81 { |
|
82 |
|
83 namespace detail |
|
84 { |
|
85 |
|
86 typedef long atomic_count; |
|
87 |
|
88 } |
|
89 |
|
90 } |
|
91 |
|
92 #elif defined(BOOST_AC_USE_PTHREADS) |
|
93 # include <boost/detail/atomic_count_pthreads.hpp> |
|
94 #elif defined(WIN32) || defined(_WIN32) || defined(__WIN32__) |
|
95 # include <boost/detail/atomic_count_win32.hpp> |
|
96 #elif defined(__GLIBCPP__) || defined(__GLIBCXX__) |
|
97 # include <boost/detail/atomic_count_gcc.hpp> |
|
98 #elif defined(BOOST_HAS_PTHREADS) |
|
99 # define BOOST_AC_USE_PTHREADS |
|
100 # include <boost/detail/atomic_count_pthreads.hpp> |
|
101 #else |
|
102 |
|
103 // Use #define BOOST_DISABLE_THREADS to avoid the error |
|
104 #error Unrecognized threading platform |
|
105 |
|
106 #endif |
|
107 |
|
108 #endif // #ifndef BOOST_DETAIL_ATOMIC_COUNT_HPP_INCLUDED |