sl@0: #ifndef BOOST_DETAIL_ATOMIC_COUNT_HPP_INCLUDED sl@0: #define BOOST_DETAIL_ATOMIC_COUNT_HPP_INCLUDED sl@0: sl@0: // MS compatible compilers support #pragma once sl@0: sl@0: #if defined(_MSC_VER) && (_MSC_VER >= 1020) sl@0: # pragma once sl@0: #endif sl@0: sl@0: // sl@0: // boost/detail/atomic_count.hpp - thread/SMP safe reference counter sl@0: // sl@0: // Copyright (c) 2001, 2002 Peter Dimov and Multi Media Ltd. sl@0: // sl@0: // Distributed under the Boost Software License, Version 1.0. (See sl@0: // accompanying file LICENSE_1_0.txt or copy at sl@0: // http://www.boost.org/LICENSE_1_0.txt) sl@0: // sl@0: // typedef <implementation-defined> boost::detail::atomic_count; sl@0: // sl@0: // atomic_count a(n); sl@0: // sl@0: // (n is convertible to long) sl@0: // sl@0: // Effects: Constructs an atomic_count with an initial value of n sl@0: // sl@0: // a; sl@0: // sl@0: // Returns: (long) the current value of a sl@0: // sl@0: // ++a; sl@0: // sl@0: // Effects: Atomically increments the value of a sl@0: // Returns: nothing sl@0: // sl@0: // --a; sl@0: // sl@0: // Effects: Atomically decrements the value of a sl@0: // Returns: (long) zero if the new value of a is zero, sl@0: // unspecified non-zero value otherwise (usually the new value) sl@0: // sl@0: // Important note: when --a returns zero, it must act as a sl@0: // read memory barrier (RMB); i.e. the calling thread must sl@0: // have a synchronized view of the memory sl@0: // sl@0: // On Intel IA-32 (x86) memory is always synchronized, so this sl@0: // is not a problem. sl@0: // sl@0: // On many architectures the atomic instructions already act as sl@0: // a memory barrier. sl@0: // sl@0: // This property is necessary for proper reference counting, since sl@0: // a thread can update the contents of a shared object, then sl@0: // release its reference, and another thread may immediately sl@0: // release the last reference causing object destruction. sl@0: // sl@0: // The destructor needs to have a synchronized view of the sl@0: // object to perform proper cleanup. sl@0: // sl@0: // Original example by Alexander Terekhov: sl@0: // sl@0: // Given: sl@0: // sl@0: // - a mutable shared object OBJ; sl@0: // - two threads THREAD1 and THREAD2 each holding sl@0: // a private smart_ptr object pointing to that OBJ. sl@0: // sl@0: // t1: THREAD1 updates OBJ (thread-safe via some synchronization) sl@0: // and a few cycles later (after "unlock") destroys smart_ptr; sl@0: // sl@0: // t2: THREAD2 destroys smart_ptr WITHOUT doing any synchronization sl@0: // with respect to shared mutable object OBJ; OBJ destructors sl@0: // are called driven by smart_ptr interface... sl@0: // sl@0: sl@0: #include <boost/config.hpp> sl@0: sl@0: #ifndef BOOST_HAS_THREADS sl@0: sl@0: namespace boost sl@0: { sl@0: sl@0: namespace detail sl@0: { sl@0: sl@0: typedef long atomic_count; sl@0: sl@0: } sl@0: sl@0: } sl@0: sl@0: #elif defined(BOOST_AC_USE_PTHREADS) sl@0: # include <boost/detail/atomic_count_pthreads.hpp> sl@0: #elif defined(WIN32) || defined(_WIN32) || defined(__WIN32__) sl@0: # include <boost/detail/atomic_count_win32.hpp> sl@0: #elif defined(__GLIBCPP__) || defined(__GLIBCXX__) sl@0: # include <boost/detail/atomic_count_gcc.hpp> sl@0: #elif defined(BOOST_HAS_PTHREADS) sl@0: # define BOOST_AC_USE_PTHREADS sl@0: # include <boost/detail/atomic_count_pthreads.hpp> sl@0: #else sl@0: sl@0: // Use #define BOOST_DISABLE_THREADS to avoid the error sl@0: #error Unrecognized threading platform sl@0: sl@0: #endif sl@0: sl@0: #endif // #ifndef BOOST_DETAIL_ATOMIC_COUNT_HPP_INCLUDED