CUBRID Engine  latest
lockfree_address_marker.hpp
Go to the documentation of this file.
1 /*
2  * Copyright 2008 Search Solution Corporation
3  * Copyright 2016 CUBRID Corporation
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  */
18 
19 //
20 // lock-free address marker
21 //
22 
23 #ifndef _LOCKFREE_ADDRESS_MARKER_HPP_
24 #define _LOCKFREE_ADDRESS_MARKER_HPP_
25 
26 #include <atomic>
27 #include <cstdint>
28 
29 namespace lockfree
30 {
31  template <class T>
33  {
34  private:
35  using convert_type = std::uint64_t;
36  static const convert_type MARK = 0x1;
37 
38  static convert_type to_cnv_type (T *addr);
39  static T *to_addr (convert_type ct);
40 
41  public:
42  address_marker ();
43  address_marker (T *addr);
44 
45  bool is_marked () const;
46  T *get_address () const;
47  T *get_address_no_strip () const;
48 
49  static T *set_adress_mark (T *addr);
50  static T *strip_address_mark (T *addr);
51  static bool is_address_marked (T *addr);
52 
53  static T *atomic_strip_address_mark (T *addr);
54 
55  private:
56  static bool is_ct_marked (convert_type ct);
59 
60  std::atomic<T *> m_addr;
61  };
62 } // namespace lockfree
63 
64 //
65 // implementation
66 //
67 
68 namespace lockfree
69 {
70  //
71  // address marker
72  //
73  template <class T>
75  : m_addr { NULL }
76  {
77  }
78 
79  template <class T>
81  : m_addr (addr)
82  {
83  }
84 
85  template <class T>
88  {
89  return (convert_type) addr;
90  }
91 
92  template <class T>
93  T *
95  {
96  return (T *) ct;
97  }
98 
99  template <class T>
100  bool
102  {
103  return is_address_marked (m_addr.load ());
104  }
105 
106  template <class T>
107  T *
109  {
110  return strip_address_mark (m_addr.load ());
111  }
112 
113  template <class T>
114  T *
116  {
117  return m_addr.load ();
118  }
119 
120  template <class T>
121  bool
123  {
124  return (ct & MARK) != 0;
125  }
126 
127  template <class T>
130  {
131  return (ct | MARK);
132  }
133 
134  template <class T>
137  {
138  return (ct & (~MARK));
139  }
140 
141  template <class T>
142  T *
144  {
145  return to_addr (set_ct_mark (to_cnv_type (addr)));
146  }
147 
148  template <class T>
149  T *
151  {
152  return to_addr (strip_ct_mark (to_cnv_type (addr)));
153  }
154 
155  template <class T>
156  bool
158  {
159  return is_ct_marked (to_cnv_type (addr));
160  }
161 
162  template <class T>
163  T *
165  {
166  return address_marker (addr).get_address ();
167  }
168 } // namespace lockfree
169 #endif // !_LOCKFREE_ADDRESS_MARKER_HPP_
static bool is_address_marked(T *addr)
static T * to_addr(convert_type ct)
static T * strip_address_mark(T *addr)
static convert_type to_cnv_type(T *addr)
#define NULL
Definition: freelistheap.h:34
static T * atomic_strip_address_mark(T *addr)
static const convert_type MARK
static bool is_ct_marked(convert_type ct)
static convert_type set_ct_mark(convert_type ct)
static convert_type strip_ct_mark(convert_type ct)