Tervel  1.0.0
A collection of wait-free containers and algorithms.
popbackwra_op.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_VECTOR_POPWRA_OP_H
26 #define __TERVEL_CONTAINERS_WF_VECTOR_POPWRA_OP_H
27 
28 
29 #include <tervel/util/info.h>
30 #include <tervel/util/descriptor.h>
34 
35 
36 #include <tervel/containers/wf/vector/vector.hpp>
37 
38 namespace tervel {
39 namespace containers {
40 namespace wf {
41 namespace vector {
42 
43 template<typename T>
45 
46 template<typename T>
48  public:
49  static constexpr PopWRAOpHelper<T> * is_empty_const {reinterpret_cast<PopWRAOpHelper<T> *>(0x1L)};
50 
51  PopWRAOp(Vector<T> *vec)
52  : vec_(vec) {}
53 
55  PopWRAOpHelper<T> * temp = helper_.load();
56  assert(temp != nullptr);
57  if (temp != is_empty_const) {
59  }
60 
61  }
62 
63  void set_failed() {
64  PopWRAOpHelper<T> *temp = helper_.load();
65  if (temp == nullptr) {
66  helper_.compare_exchange_strong(temp, is_empty_const);
67  }
68  }
69 
70  void help_complete() {
71  size_t placed_pos = vec_->size();
72  if (placed_pos <= 0) {
73  this->set_failed();
74  return;
75  }
76  std::atomic<T> *spot = vec_->internal_array.get_spot(placed_pos - 1);
77  T current_prev = spot->load();
78 
80  PopWRAOpHelper<T> >(this, current_prev);
81  T help_t = reinterpret_cast<T>(util::memory::rc::mark_first(helper));
83 
84  while (helper_.load() == nullptr) {
85  if (current_prev == Vector<T>::c_not_value_) {
86  placed_pos--;
87  if (placed_pos <= 0) {
88  this->set_failed();
89  break;
90  } else{
91  spot = vec_->internal_array.get_spot(placed_pos - 1);
92  current_prev = spot->load();
93  helper->val_ = current_prev;
94  continue;
95  }
96  } else if (vec_->internal_array.is_descriptor(current_prev, spot)) {
97  continue;
98  } else { // its a valid value
99 
100  if (!spot->compare_exchange_strong(current_prev, help_t)) {
101  continue;
102  }
103  helper->complete(reinterpret_cast<void *>(help_t),
104  reinterpret_cast< std::atomic<void *> *>(spot));
105  assert(helper_.load() == helper);
106  return;
107  }
108  } // while not complete
109  util::memory::rc::free_descriptor(helper, true);
110  };
111 
112  bool result(T &val) {
113  PopWRAOpHelper<T> *temp = helper_.load();
114  assert(temp != nullptr);
115  if (temp == is_empty_const) {
116  return false;
117  } else {
118  return temp->result(val);
119  }
120  }
121 
122  bool result() {
123  PopWRAOpHelper<T> *temp = helper_.load();
124  assert(temp != nullptr);
125  if (temp == is_empty_const) {
126  return false;
127  } else {
128  return temp->result();
129  }
130  }
131 
132 
133  private:
134  friend class PopWRAOpHelper<T>;
135  Vector<T> *vec_;
136  std::atomic<T> value_ {Vector<T>::c_not_value_};
137  std::atomic<T> * prev_spot_ {nullptr};
138  std::atomic<PopWRAOpHelper<T> *> helper_ {nullptr};
139 }; // class PopOp
140 
141 template<typename T>
142 class PopWRAOpHelper: public tervel::util::Descriptor {
143  public:
144 
146  : val_(val)
147  , op_(op) {}
148 
150  }
151 
153  void * complete(void *value, std::atomic<void *> *address) {
154 
155  assert(value == util::memory::rc::mark_first(this));
156 
157  bool is_valid = associate();
158 
159  void * new_val = reinterpret_cast<void *>(Vector<T>::c_not_value_);
160 // if (is_valid) {
161 // } else {
162 // new_val = reinterpret_cast<void *>(val_);
163 // }
164  if (address->compare_exchange_strong(value, new_val)) {
165  return value;
166  } else {
167  return new_val;
168  }
169  } // complete
170 
171  bool associate() {
172 
173  PopWRAOpHelper *temp_null = nullptr;
174  bool res = op_->helper_.compare_exchange_strong(temp_null, this);
175  if (res || temp_null == this) {
176  assert(op_->helper_.load() == this);
177  return true;
178  } else {
179  assert(false);
180  return false;
181  }
182  };
183 
184  bool result(T &val) {
185  if (op_->helper_.load() == nullptr) {
186  bool res = associate();
187  if(res)
188  val = val_;
189  return res;
190  } else if (op_->helper_.load() == this) {
191  val = val_;
192  return true;
193  } else {
194  assert(op_->helper_.load() != nullptr);
195  return false;
196  }
197  }
198 
199  bool result() {
200 
201  if (op_->helper_.load() == nullptr) {
202  return associate();
203  } else if (op_->helper_.load() == this) {
204  return true;
205  } else {
206  assert(op_->helper_.load() != nullptr);
207  return false;
208  }
209  }
210 
211 
212  private:
213  T val_;
214  PopWRAOp<T> *op_ {nullptr};
215 };
216 
217 
218 } // namespace vector
219 } // namespace wf
220 } // namespace containers
221 } // namespace tervel
222 #endif // __TERVEL_CONTAINERS_WF_VECTOR_POPWRA_OP_H
DescrType * get_descriptor(Args &&...args)
Constructs and returns a descriptor.
Definition: descriptor_util.h:50
void set_failed()
Definition: popbackwra_op.h:63
bool result()
Definition: popbackwra_op.h:122
__thread void * tl_control_word
~PopWRAOpHelper()
Definition: popbackwra_op.h:149
void * mark_first(tervel::util::Descriptor *descr)
This returns the passed reference with its least signifcant bit set to 1.
Definition: descriptor_util.h:157
bool associate()
Definition: popbackwra_op.h:171
void help_complete()
Implementations of this function that upon its return the operation described in the OpRecord has bee...
Definition: popbackwra_op.h:70
T val_
Definition: popbackwra_op.h:213
void free_descriptor(tervel::util::Descriptor *descr, bool dont_check=false)
Once a user is done with a descriptor, they should free it with this method.
Definition: descriptor_util.h:65
void * complete(void *value, std::atomic< void * > *address)
This method is implemented by each sub class and must guarantee that upon return that the descriptor ...
Definition: popbackwra_op.h:153
TODO(steven):
Definition: mcas.h:36
std::atomic< T > * prev_spot_
Definition: popbackwra_op.h:137
Vector< T > * vec_
Definition: popbackwra_op.h:135
std::atomic< PopWRAOpHelper< T > * > helper_
Definition: popbackwra_op.h:138
PopWRAOpHelper(PopWRAOp< T > *op, T val)
Definition: popbackwra_op.h:145
This defines the Descriptor class, this class is designed to be extend and be used in conjunction wit...
Definition: descriptor.h:60
bool result()
Definition: popbackwra_op.h:199
virtual void * complete(void *current, std::atomic< void * > *address)=0
This method is implemented by each sub class and must guarantee that upon return that the descriptor ...
static constexpr PopWRAOpHelper< T > * is_empty_const
Definition: popbackwra_op.h:49
This class is used to create Operation Records.
Definition: progress_assurance.h:52
bool result(T &val)
Definition: popbackwra_op.h:184
PopWRAOp(Vector< T > *vec)
Definition: popbackwra_op.h:51
std::atomic< T > value_
Definition: popbackwra_op.h:136
Definition: popbackwra_op.h:47
bool result(T &val)
Definition: popbackwra_op.h:112
~PopWRAOp()
Definition: popbackwra_op.h:54