blob: 9d55f251ccdd146d97404e3482a690541118f269 [file] [log] [blame]
Jeff Thompson3d613fd2013-10-15 15:39:04 -07001#ifndef NDNBOOST_SMART_PTR_DETAIL_ATOMIC_COUNT_HPP_INCLUDED
2#define NDNBOOST_SMART_PTR_DETAIL_ATOMIC_COUNT_HPP_INCLUDED
Jeff Thompsonf7d49942013-08-01 16:47:40 -07003
4// MS compatible compilers support #pragma once
5
6#if defined(_MSC_VER) && (_MSC_VER >= 1020)
7# pragma once
8#endif
9
10//
Jeff Thompson9939dcd2013-10-15 15:12:24 -070011// ndnboost/detail/atomic_count.hpp - thread/SMP safe reference counter
Jeff Thompsonf7d49942013-08-01 16:47:40 -070012//
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> ndnboost::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: (long) the new value of a
35//
36// --a;
37//
38// Effects: Atomically decrements the value of a
39// Returns: (long) the new value of a
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
Jeff Thompson2277ce52013-08-01 17:34:11 -070075#include <ndnboost/config.hpp>
76#include <ndnboost/smart_ptr/detail/sp_has_sync.hpp>
Jeff Thompsonf7d49942013-08-01 16:47:40 -070077
Jeff Thompson3d613fd2013-10-15 15:39:04 -070078#ifndef NDNBOOST_HAS_THREADS
Jeff Thompsonf7d49942013-08-01 16:47:40 -070079
80namespace ndnboost
81{
82
83namespace detail
84{
85
86typedef long atomic_count;
87
88}
89
90}
91
Jeff Thompson3d613fd2013-10-15 15:39:04 -070092#elif defined(NDNBOOST_AC_USE_PTHREADS)
Jeff Thompson2277ce52013-08-01 17:34:11 -070093# include <ndnboost/smart_ptr/detail/atomic_count_pthreads.hpp>
Jeff Thompsonf7d49942013-08-01 16:47:40 -070094
95#elif defined( __GNUC__ ) && ( defined( __i386__ ) || defined( __x86_64__ ) )
Jeff Thompson2277ce52013-08-01 17:34:11 -070096# include <ndnboost/smart_ptr/detail/atomic_count_gcc_x86.hpp>
Jeff Thompsonf7d49942013-08-01 16:47:40 -070097
98#elif defined(WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(__CYGWIN__)
Jeff Thompson2277ce52013-08-01 17:34:11 -070099# include <ndnboost/smart_ptr/detail/atomic_count_win32.hpp>
Jeff Thompsonf7d49942013-08-01 16:47:40 -0700100
Jeff Thompson3d613fd2013-10-15 15:39:04 -0700101#elif defined( NDNBOOST_SP_HAS_SYNC )
Jeff Thompson2277ce52013-08-01 17:34:11 -0700102# include <ndnboost/smart_ptr/detail/atomic_count_sync.hpp>
Jeff Thompsonf7d49942013-08-01 16:47:40 -0700103
104#elif defined(__GLIBCPP__) || defined(__GLIBCXX__)
Jeff Thompson2277ce52013-08-01 17:34:11 -0700105# include <ndnboost/smart_ptr/detail/atomic_count_gcc.hpp>
Jeff Thompsonf7d49942013-08-01 16:47:40 -0700106
Jeff Thompson3d613fd2013-10-15 15:39:04 -0700107#elif defined(NDNBOOST_HAS_PTHREADS)
Jeff Thompsonf7d49942013-08-01 16:47:40 -0700108
Jeff Thompson3d613fd2013-10-15 15:39:04 -0700109# define NDNBOOST_AC_USE_PTHREADS
Jeff Thompson2277ce52013-08-01 17:34:11 -0700110# include <ndnboost/smart_ptr/detail/atomic_count_pthreads.hpp>
Jeff Thompsonf7d49942013-08-01 16:47:40 -0700111
112#else
113
Jeff Thompson3d613fd2013-10-15 15:39:04 -0700114// Use #define NDNBOOST_DISABLE_THREADS to avoid the error
Jeff Thompsonf7d49942013-08-01 16:47:40 -0700115#error Unrecognized threading platform
116
117#endif
118
Jeff Thompson3d613fd2013-10-15 15:39:04 -0700119#endif // #ifndef NDNBOOST_SMART_PTR_DETAIL_ATOMIC_COUNT_HPP_INCLUDED