]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/boost/lockfree/detail/tagged_ptr_dcas.hpp
import new upstream nautilus stable release 14.2.8
[ceph.git] / ceph / src / boost / boost / lockfree / detail / tagged_ptr_dcas.hpp
1 // tagged pointer, for aba prevention
2 //
3 // Copyright (C) 2008, 2016 Tim Blechmann
4 //
5 // Distributed under the Boost Software License, Version 1.0. (See
6 // accompanying file LICENSE_1_0.txt or copy at
7 // http://www.boost.org/LICENSE_1_0.txt)
8
9 #ifndef BOOST_LOCKFREE_TAGGED_PTR_DCAS_HPP_INCLUDED
10 #define BOOST_LOCKFREE_TAGGED_PTR_DCAS_HPP_INCLUDED
11
12 #include <cstddef> /* for std::size_t */
13 #include <limits>
14
15 #include <boost/predef.h>
16
17 namespace boost {
18 namespace lockfree {
19 namespace detail {
20
21
22
23 template <class T>
24 class
25 #if BOOST_COMP_MSVC && BOOST_ARCH_X86_64
26 BOOST_ALIGNMENT(16)
27 #elif BOOST_COMP_MSVC && BOOST_ARCH_X86_32
28 BOOST_ALIGNMENT(8)
29 #else
30 BOOST_ALIGNMENT(2 * sizeof(void*))
31 #endif
32 tagged_ptr
33 {
34 public:
35 typedef std::size_t tag_t;
36
37 /** uninitialized constructor */
38 tagged_ptr(void) BOOST_NOEXCEPT//: ptr(0), tag(0)
39 {}
40
41 #ifdef BOOST_NO_CXX11_DEFAULTED_FUNCTIONS
42 tagged_ptr(tagged_ptr const & p):
43 ptr(p.ptr), tag(p.tag)
44 {}
45 #else
46 tagged_ptr(tagged_ptr const & p) = default;
47 #endif
48
49 explicit tagged_ptr(T * p, tag_t t = 0):
50 ptr(p), tag(t)
51 {}
52
53 /** unsafe set operation */
54 /* @{ */
55 #ifdef BOOST_NO_CXX11_DEFAULTED_FUNCTIONS
56 tagged_ptr & operator= (tagged_ptr const & p)
57 {
58 set(p.ptr, p.tag);
59 return *this;
60 }
61 #else
62 tagged_ptr & operator= (tagged_ptr const & p) = default;
63 #endif
64
65 void set(T * p, tag_t t)
66 {
67 ptr = p;
68 tag = t;
69 }
70 /* @} */
71
72 /** comparing semantics */
73 /* @{ */
74 bool operator== (volatile tagged_ptr const & p) const
75 {
76 return (ptr == p.ptr) && (tag == p.tag);
77 }
78
79 bool operator!= (volatile tagged_ptr const & p) const
80 {
81 return !operator==(p);
82 }
83 /* @} */
84
85 /** pointer access */
86 /* @{ */
87 T * get_ptr(void) const
88 {
89 return ptr;
90 }
91
92 void set_ptr(T * p)
93 {
94 ptr = p;
95 }
96 /* @} */
97
98 /** tag access */
99 /* @{ */
100 tag_t get_tag() const
101 {
102 return tag;
103 }
104
105 tag_t get_next_tag() const
106 {
107 tag_t next = (get_tag() + 1) & (std::numeric_limits<tag_t>::max)();
108 return next;
109 }
110
111 void set_tag(tag_t t)
112 {
113 tag = t;
114 }
115 /* @} */
116
117 /** smart pointer support */
118 /* @{ */
119 T & operator*() const
120 {
121 return *ptr;
122 }
123
124 T * operator->() const
125 {
126 return ptr;
127 }
128
129 operator bool(void) const
130 {
131 return ptr != 0;
132 }
133 /* @} */
134
135 protected:
136 T * ptr;
137 tag_t tag;
138 };
139
140 } /* namespace detail */
141 } /* namespace lockfree */
142 } /* namespace boost */
143
144 #endif /* BOOST_LOCKFREE_TAGGED_PTR_DCAS_HPP_INCLUDED */