blob: 7720cdfbcc224374b1bb0f67e6b976b7bb1e701f [file] [log] [blame]
Jeff Thompson3d613fd2013-10-15 15:39:04 -07001#ifndef NDNBOOST_SMART_PTR_DETAIL_ATOMIC_COUNT_GCC_HPP_INCLUDED
2#define NDNBOOST_SMART_PTR_DETAIL_ATOMIC_COUNT_GCC_HPP_INCLUDED
Jeff Thompsonf7d49942013-08-01 16:47:40 -07003
4//
Jeff Thompson9939dcd2013-10-15 15:12:24 -07005// ndnboost/detail/atomic_count_gcc.hpp
Jeff Thompsonf7d49942013-08-01 16:47:40 -07006//
7// atomic_count for GNU libstdc++ v3
8//
9// http://gcc.gnu.org/onlinedocs/porting/Thread-safety.html
10//
11// Copyright (c) 2001, 2002 Peter Dimov and Multi Media Ltd.
12// Copyright (c) 2002 Lars Gullik Bjønnes <larsbj@lyx.org>
13// Copyright 2003-2005 Peter Dimov
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
20#if __GNUC__ * 100 + __GNUC_MINOR__ >= 402
21# include <ext/atomicity.h>
22#else
23# include <bits/atomicity.h>
24#endif
25
26namespace ndnboost
27{
28
29namespace detail
30{
31
32#if defined(__GLIBCXX__) // g++ 3.4+
33
34using __gnu_cxx::__atomic_add;
35using __gnu_cxx::__exchange_and_add;
36
37#endif
38
39class atomic_count
40{
41public:
42
43 explicit atomic_count( long v ) : value_( v ) {}
44
45 long operator++()
46 {
47 return __exchange_and_add( &value_, +1 ) + 1;
48 }
49
50 long operator--()
51 {
52 return __exchange_and_add( &value_, -1 ) - 1;
53 }
54
55 operator long() const
56 {
57 return __exchange_and_add( &value_, 0 );
58 }
59
60private:
61
62 atomic_count(atomic_count const &);
63 atomic_count & operator=(atomic_count const &);
64
65 mutable _Atomic_word value_;
66};
67
68} // namespace detail
69
70} // namespace ndnboost
71
Jeff Thompson3d613fd2013-10-15 15:39:04 -070072#endif // #ifndef NDNBOOST_SMART_PTR_DETAIL_ATOMIC_COUNT_GCC_HPP_INCLUDED