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