Imported Upstream version 1.57.0
[platform/upstream/boost.git] / boost / lockfree / detail / tagged_ptr_dcas.hpp
1 //  tagged pointer, for aba prevention
2 //
3 //  Copyright (C) 2008 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/lockfree/detail/branch_hints.hpp>
16
17 namespace boost {
18 namespace lockfree {
19 namespace detail {
20
21 template <class T>
22 class BOOST_LOCKFREE_DCAS_ALIGNMENT tagged_ptr
23 {
24 public:
25     typedef std::size_t tag_t;
26
27     /** uninitialized constructor */
28     tagged_ptr(void) BOOST_NOEXCEPT//: ptr(0), tag(0)
29     {}
30
31 #ifdef BOOST_NO_CXX11_DEFAULTED_FUNCTIONS
32     tagged_ptr(tagged_ptr const & p):
33         ptr(p.ptr), tag(p.tag)
34     {}
35 #else
36     tagged_ptr(tagged_ptr const & p) = default;
37 #endif
38
39     explicit tagged_ptr(T * p, tag_t t = 0):
40         ptr(p), tag(t)
41     {}
42
43     /** unsafe set operation */
44     /* @{ */
45 #ifdef BOOST_NO_CXX11_DEFAULTED_FUNCTIONS
46     tagged_ptr & operator= (tagged_ptr const & p)
47     {
48         set(p.ptr, p.tag);
49         return *this;
50     }
51 #else
52     tagged_ptr & operator= (tagged_ptr const & p) = default;
53 #endif
54
55     void set(T * p, tag_t t)
56     {
57         ptr = p;
58         tag = t;
59     }
60     /* @} */
61
62     /** comparing semantics */
63     /* @{ */
64     bool operator== (volatile tagged_ptr const & p) const
65     {
66         return (ptr == p.ptr) && (tag == p.tag);
67     }
68
69     bool operator!= (volatile tagged_ptr const & p) const
70     {
71         return !operator==(p);
72     }
73     /* @} */
74
75     /** pointer access */
76     /* @{ */
77     T * get_ptr(void) const
78     {
79         return ptr;
80     }
81
82     void set_ptr(T * p)
83     {
84         ptr = p;
85     }
86     /* @} */
87
88     /** tag access */
89     /* @{ */
90     tag_t get_tag() const
91     {
92         return tag;
93     }
94
95     tag_t get_next_tag() const
96     {
97         tag_t next = (get_tag() + 1) & (std::numeric_limits<tag_t>::max)();
98         return next;
99     }
100
101     void set_tag(tag_t t)
102     {
103         tag = t;
104     }
105     /* @} */
106
107     /** smart pointer support  */
108     /* @{ */
109     T & operator*() const
110     {
111         return *ptr;
112     }
113
114     T * operator->() const
115     {
116         return ptr;
117     }
118
119     operator bool(void) const
120     {
121         return ptr != 0;
122     }
123     /* @} */
124
125 protected:
126     T * ptr;
127     tag_t tag;
128 };
129
130 } /* namespace detail */
131 } /* namespace lockfree */
132 } /* namespace boost */
133
134 #endif /* BOOST_LOCKFREE_TAGGED_PTR_DCAS_HPP_INCLUDED */