Tervel  1.0.0
A collection of wait-free containers and algorithms.
helper_imp.h
Go to the documentation of this file.
1 /*
2 The MIT License (MIT)
3 
4 Copyright (c) 2015 University of Central Florida's Computer Software Engineering
5 Scalable & Secure Systems (CSE - S3) Lab
6 
7 Permission is hereby granted, free of charge, to any person obtaining a copy
8 of this software and associated documentation files (the "Software"), to deal
9 in the Software without restriction, including without limitation the rights
10 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 copies of the Software, and to permit persons to whom the Software is
12 furnished to do so, subject to the following conditions:
13 
14 The above copyright notice and this permission notice shall be included in
15 all copies or substantial portions of the Software.
16 
17 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23 THE SOFTWARE.
24 */
25 #ifndef TERVEL_CONTAINERS_WF_RINGBUFFER_RINGBUFFER_HELPER_IMP_H_
26 #define TERVEL_CONTAINERS_WF_RINGBUFFER_RINGBUFFER_HELPER_IMP_H_
27 
30 
32 
33 namespace tervel {
34 namespace containers {
35 namespace wf {
36 
37 template<typename T>
38 bool
40 on_watch(std::atomic<void *> *address, void *expected) {
42  SlotID pos = SlotID::SHORTUSE2;
44  address, expected);
45  if (!res) {
46  return false;
47  }
48 
49  void *val = associate();
50  res = address->compare_exchange_strong(expected, val);
51  if (!res) {
52  // we failed, could be because of delayed mark.
53  void *temp = reinterpret_cast<void *>(
55  if (expected == temp) {
56  address->compare_exchange_strong(expected, val);
57  }
58  }
59 
60  #ifdef DEBUG
61  expected = address->load();
62  assert(expected != reinterpret_cast<void *>(HelperType(this)));
63  assert(expected !=
64  reinterpret_cast<void *>(
66  #endif
67 
68  return false;
69 }
70 
71 template<typename T>
72 void *
75  return op_->associate(this);
76 }
77 
78 template<typename T>
79 bool
81 valid() {
82  return op_->valid(this);
83 }
84 
85 template<typename T>
86 uintptr_t
89  uintptr_t res = reinterpret_cast<uintptr_t>(h);
90  res = res | RingBuffer<T>::oprec_lsb; // 3LSB now 100
91  return res;
92 }
93 
94 template<typename T>
95 bool
97 isHelperType(uintptr_t val) {
98  val = val & RingBuffer<T>::oprec_lsb;
99  return (val != 0);
100 }
101 
102 
103 template<typename T>
104 typename RingBuffer<T>::Helper *
106 getHelperType(uintptr_t val) {
107  val = val & (~RingBuffer<T>::oprec_lsb); // clear oprec_lsb
108  val = val & (~RingBuffer<T>::delayMark_lsb); // clear delayMark_lsb
109  return reinterpret_cast<Helper *>(val);
110 }
111 
112 } // namespace wf
113 } // namespace containers
114 } // namespace tervel
115 
116 #endif // TERVEL_CONTAINERS_WF_RINGBUFFER_RINGBUFFER_HELPER_IMP_H_
static Helper * getHelperType(uintptr_t val)
Definition: helper_imp.h:106
TODO(steven):
Definition: mcas.h:36
BufferOp * op_
Definition: helper.h:61
static uintptr_t DelayMarkValue(uintptr_t val)
Takes a uintptr_t and places a bitmark on the delayMark_lsb.
Definition: ring_buffer_imp.h:374
static bool watch(SlotID slot_id, Element *elem, std::atomic< void * > *address, void *expected, HazardPointer *const hazard_pointer=tervel::tl_thread_info->get_hazard_pointer())
This method is used to achieve a hazard pointer watch on the the based descr.
This is a non-blocking FIFO ring buffer design that was made wait-free by applying a progress assuran...
Definition: ring_buffer.h:73
void * associate()
Definition: helper_imp.h:74
static bool isHelperType(uintptr_t val)
Definition: helper_imp.h:97
bool on_watch(std::atomic< void * > *address, void *expected)
This function is used to achieve a strong watch on an Element.
Definition: helper_imp.h:40
static uintptr_t HelperType(Helper *h)
Returns a uintptr_t for the passed helper object.
Definition: helper_imp.h:88
bool valid()
Definition: helper_imp.h:81
SlotID
Definition: hazard_pointer.h:58