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