]> git.lyx.org Git - lyx.git/blob - boost/boost/detail/atomic_count.hpp
Boost 1.31.0
[lyx.git] / boost / boost / detail / atomic_count.hpp
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 //  Permission to copy, use, modify, sell and distribute this software
16 //  is granted provided this copyright notice appears in all copies.
17 //  This software is provided "as is" without express or implied
18 //  warranty, and with no claim as to its suitability for any purpose.
19 //
20 //  typedef <implementation-defined> boost::detail::atomic_count;
21 //
22 //  atomic_count a(n);
23 //
24 //    (n is convertible to long)
25 //
26 //    Effects: Constructs an atomic_count with an initial value of n
27 //
28 //  a;
29 //
30 //    Returns: (long) the current value of a
31 //
32 //  ++a;
33 //
34 //    Effects: Atomically increments the value of a
35 //    Returns: nothing
36 //
37 //  --a;
38 //
39 //    Effects: Atomically decrements the value of a
40 //    Returns: (long) zero if the new value of a is zero,
41 //      unspecified non-zero value otherwise (usually the new value)
42 //
43 //    Important note: when --a returns zero, it must act as a
44 //      read memory barrier (RMB); i.e. the calling thread must
45 //      have a synchronized view of the memory
46 //
47 //    On Intel IA-32 (x86) memory is always synchronized, so this
48 //      is not a problem.
49 //
50 //    On many architectures the atomic instructions already act as
51 //      a memory barrier.
52 //
53 //    This property is necessary for proper reference counting, since
54 //      a thread can update the contents of a shared object, then
55 //      release its reference, and another thread may immediately
56 //      release the last reference causing object destruction.
57 //
58 //    The destructor needs to have a synchronized view of the
59 //      object to perform proper cleanup.
60 //
61 //    Original example by Alexander Terekhov:
62 //
63 //    Given:
64 //
65 //    - a mutable shared object OBJ;
66 //    - two threads THREAD1 and THREAD2 each holding 
67 //      a private smart_ptr object pointing to that OBJ.
68 //
69 //    t1: THREAD1 updates OBJ (thread-safe via some synchronization)
70 //      and a few cycles later (after "unlock") destroys smart_ptr;
71 //
72 //    t2: THREAD2 destroys smart_ptr WITHOUT doing any synchronization 
73 //      with respect to shared mutable object OBJ; OBJ destructors
74 //      are called driven by smart_ptr interface...
75 //
76
77 //  Note: atomic_count_linux.hpp has been disabled by default; see the
78 //        comments inside for more info.
79
80 #include <boost/config.hpp>
81
82 #ifndef BOOST_HAS_THREADS
83
84 namespace boost
85 {
86
87 namespace detail
88 {
89
90 typedef long atomic_count;
91
92 }
93
94 }
95
96 #elif defined(BOOST_USE_ASM_ATOMIC_H)
97 #  include <boost/detail/atomic_count_linux.hpp>
98 #elif defined(BOOST_AC_USE_PTHREADS)
99 #  include <boost/detail/atomic_count_pthreads.hpp>
100 #elif defined(WIN32) || defined(_WIN32) || defined(__WIN32__)
101 #  include <boost/detail/atomic_count_win32.hpp>
102 #elif defined(__GLIBCPP__)
103 #  include <boost/detail/atomic_count_gcc.hpp>
104 #elif defined(BOOST_HAS_PTHREADS)
105 #  define BOOST_AC_USE_PTHREADS
106 #  include <boost/detail/atomic_count_pthreads.hpp>
107 #else
108
109 // Use #define BOOST_DISABLE_THREADS to avoid the error
110 #error Unrecognized threading platform
111
112 #endif
113
114 #endif // #ifndef BOOST_DETAIL_ATOMIC_COUNT_HPP_INCLUDED