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