Tervel  1.0.0
A collection of wait-free containers and algorithms.
enqueue_op_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_ENQUEUEOP_IMP_H_
26 #define TERVEL_CONTAINERS_WF_RINGBUFFER_RINGBUFFER_ENQUEUEOP_IMP_H_
27 
29 
30 namespace tervel {
31 namespace containers {
32 namespace wf {
33 
34 template<typename T>
35 void
38  int64_t tail = this->rb_->getTail();
39  while(this->BufferOp::notDone()) {
40  if (this->rb_->isFull(tail, this->rb_->getHead())) {
41  this->fail();
42  return;
43  }
44  int64_t seqid = tail++;
45  uint64_t pos = this->rb_->getPos(seqid);
46  uintptr_t val;
47 
48  while(this->BufferOp::notDone()) {
49  if (!this->rb_->readValue(pos, val)) {
50  continue;
51  }
52 
53  int64_t val_seqid;
54  bool val_isValueType;
55  bool val_isDelayedMarked;
56  this->rb_->getInfo(val, val_seqid, val_isValueType, val_isDelayedMarked);
57 
58 
59  if (val_seqid > tail) {
60  // We are lagging, so iterate until we find a matching seqid.
61  break;
62  } else if (val_isDelayedMarked) {
63  // We don't want a delayed marked anything, too complicated, let some
64  // one else deal with it.
65  break;
66  } else if (val_isValueType) {
67  // it is a valueType, with seqid <= to the one we are working with...
68  // skip?
69  break;
70  } else {
71  // Its an EmptyType with a seqid <= to the one we are working with
72  // so lets take it!
73  Helper * helper = new Helper(this, val);
74  uintptr_t helper_int = Helper::HelperType(helper);
75 
76  bool res = this->rb_->array_[pos].compare_exchange_strong(val, helper_int);
77  if (res) {
78  // Success!
79  // The following line is not hacky if you ignore the function name...
80  // it associates and then removes the object.
81  // It is also called by the memory protection watch function...
82  std::atomic<void *> *temp1;
83  temp1 = reinterpret_cast<std::atomic<void *> *>(&(this->rb_->array_[pos]));
84  void *temp2 = reinterpret_cast<void *>(helper_int);
85  helper->on_watch(temp1, temp2);
86  if (!helper->valid()) {
87  helper->safe_delete();
88  }
89 
90  return; // Op is Done!
91  } else {
92  // Failure :(
93  delete helper;
94  continue; // re-examine position on the next loop
95  }
96  }
97  }
98  }
99 }
100 
101 template<typename T>
102 void*
105  bool res = BufferOp::privAssociate(h);
106  uintptr_t new_val = h->old_value_;
107  if (res) {
108  int64_t ev_seqid = reinterpret_cast<int64_t>(this) * -1;
109  int64_t seqid = this->rb_->getEmptyTypeSeqId(h->old_value_);
110  value_->atomic_change_seqid(ev_seqid, seqid);
111 
112  uintptr_t temp = reinterpret_cast<uintptr_t>(value_);
113  assert((temp & clear_lsb) == 0 && " reserved bits are not 0?");
114  new_val = temp;
115  }
116  // Now we need to ensure the sequence counter does not false report
117  // empty
118  if (result()){
119  int64_t seqid = value_->func_seqid() + 1;
120  int64_t temp = this->rb_->getTail();
121  while (temp < seqid) {
122  if (this->rb_->casTail(temp, seqid))
123  break;
124  }
125  }
126  return reinterpret_cast<void *>(new_val);
127 
128 }
129 
130 
131 template<typename T>
132 bool
135  Helper * h;
136  if (BufferOp::isFail(h)) {
137  return false;
138  } else {
139  return true;
140  }
141 };
142 
143 } // namespace wf
144 } // namespace containers
145 } // namespace tervel
146 
147 #endif // TERVEL_CONTAINERS_WF_RINGBUFFER_RINGBUFFER_ENQUEUEOP_IMP_H_
bool privAssociate(Helper *h)
Definition: ring_buffer_op.h:59
void fail()
Definition: ring_buffer_op.h:73
TODO(steven):
Definition: mcas.h:36
const uintptr_t old_value_
Definition: helper.h:62
void * associate(Helper *h)
Definition: enqueue_op_imp.h:104
bool result()
Definition: enqueue_op_imp.h:134
void safe_delete(bool no_check=false, ElementList *const element_list=tervel::tl_thread_info->get_hp_element_list())
This function is used to free a hazard pointer protected object if it is safe to do so OR add it to a...
Definition: hp_element.h:67
void help_complete()
Implementations of this function that upon its return the operation described in the OpRecord has bee...
Definition: enqueue_op_imp.h:37
static const uintptr_t clear_lsb
Definition: ring_buffer.h:78
RingBuffer< T > * rb_
Definition: ring_buffer_op.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
bool isFail(Helper *&h)
Definition: ring_buffer_op.h:78
bool notDone()
Definition: ring_buffer_op.h:87