Tervel  1.0.0
A collection of wait-free containers and algorithms.
read_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_READ_OP_H
26 #define __TERVEL_CONTAINERS_WF_VECTOR_READ_OP_H
27 
28 #include <tervel/util/info.h>
30 
31 #include <tervel/containers/wf/vector/vector.hpp>
32 
33 namespace tervel {
34 namespace containers {
35 namespace wf {
36 namespace vector {
37 
38 
39 template<typename T>
41  public:
42  ReadOp(Vector<T> *vec, size_t idx)
43  : vec_(vec)
44  , idx_(idx) {}
45 
46  void help_complete() {
47  tervel::tl_control_word = reinterpret_cast< std::atomic<void *> *>(&value_);
48  if (idx_ < vec_->capacity()) {
49  std::atomic<T> *spot = vec_->internal_array.get_spot(idx_, false);
50 
51  while (value_.load() == Vector<T>::c_not_value_) {
52  T cvalue = spot->load();
53 
54 
55  if (cvalue == Vector<T>::c_not_value_) {
56  value_.store(c_fail_value_);
57  return;
58  } else if (vec_->internal_array.is_descriptor(cvalue, spot)) {
59  continue;
60  } else {
61  assert(vec_->internal_array.is_valid(cvalue));
62  value_.store(cvalue);
63  return;
64  }
65  } // while value_ is c_not_value
66  } // if idx < capacity()
67 
68  value_.store(c_fail_value_);
69  };
70 
71  bool result(T &expected) {
72  T temp = value_.load();
73  if (temp == c_fail_value_) {
74  return false;
75  } else {
76  expected = temp;
77  return true;
78  }
79  };
80 
81 
82  private:
83  Vector<T> *vec_;
84  size_t idx_;
85  std::atomic<T> value_ {Vector<T>::c_not_value_};
86  static const T c_fail_value_ {static_cast<T>(~0x1L)};
87 }; // class ReadOp
88 } // namespace vector
89 } // namespace wf
90 } // namespace containers
91 } // namespace tervel
92 
93 #endif //__TERVEL_CONTAINERS_WF_VECTOR_READ_OP_H
__thread void * tl_control_word
bool result(T &expected)
Definition: read_op.h:71
std::atomic< T > value_
Definition: read_op.h:85
static const T c_fail_value_
Definition: read_op.h:86
TODO(steven):
Definition: mcas.h:36
ReadOp(Vector< T > *vec, size_t idx)
Definition: read_op.h:42
void help_complete()
Implementations of this function that upon its return the operation described in the OpRecord has bee...
Definition: read_op.h:46
This class is used to create Operation Records.
Definition: progress_assurance.h:52
size_t idx_
Definition: read_op.h:84
Vector< T > * vec_
Definition: read_op.h:79