Tervel  1.0.0
A collection of wait-free containers and algorithms.
mcas.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_MCAS_MCAS_H_
26 #define TERVEL_MCAS_MCAS_H_
27 
28 #include <tervel/util/info.h>
29 #include <tervel/util/util.h>
32 
35 
36 namespace tervel {
37 namespace algorithms {
38 namespace wf {
39 namespace mcas {
46 template<class T>
47 inline T read(std::atomic<T> *address) {
48  void *value = util::memory::rc::descriptor_read_first(address);
49  return reinterpret_cast<T>(value);
50 }
51 
60 template<class T>
62  public:
63  static constexpr void * MCAS_FAIL_CONST = reinterpret_cast<void *>(0x1L);
64 
65  explicit MultiWordCompareAndSwap<T>(int max_rows)
66  : cas_rows_(new CasRow<T>[max_rows])
67  , max_rows_ {max_rows} {}
68 
71  for (int i = 0; i < row_count_; i++) {
72  Helper<T>* helper = cas_rows_[i].helper_.load();
73  // The No check flag is true because each was check prior
74  // to the call of this descructor.
75  if (helper == reinterpret_cast<Helper<T> *>(MCAS_FAIL_CONST)) {
76  break;
77  }
79  }
80  }
81 
97  bool add_cas_triple(std::atomic<T> *address, T expected_value, T new_value);
98 
105  bool execute();
106 
112  void help_complete() {
113  mcas_complete(0, true);
114  }
115 
124  bool on_is_watched() {
125  for (int i = 0; i < row_count_; i++) {
126  Helper<T>* helper = cas_rows_[i].helper_.load();
127  // The No check flag is true because each was check prior
128  // to the call of this destructor.
129  if (helper == reinterpret_cast<Helper<T> *>(MCAS_FAIL_CONST)) {
130  break;
131  } else if (util::memory::rc::is_watched(helper)) {
132  return true;
133  }
134  }
135  return false;
136  }
137 
138  private:
143  enum class MCasState : std::int8_t {IN_PROGRESS = 0, PASS = 1 , FAIL = 2,
144  DELETED = 3};
158  bool mcas_complete(int start_pos, bool wfmode = false);
159 
167  bool mcas_complete(CasRow<T> *current_row);
168 
174  void cleanup(bool success);
175 
188  T mcas_remove(const int pos, T value);
189 
190  // ------
191  // Member Data
192  // ------
193 
194  /* THe array of CAS triples to complete*/
195  std::unique_ptr<CasRow<T>[]> cas_rows_;
196  /* The state of the mcas operation */
197  std::atomic<MCasState> state_ {MCasState::IN_PROGRESS};
198  /* The number of cas rows */
199  int row_count_ {0};
200  /* The max number of rows supported this instance is expecting*/
202 
203  friend Helper<T>;
204  friend CasRow<T>;
205 }; // MCAS class
206 
207 
208 } // namespace mcas
209 } // namespace wf
210 } // namespace algorithms
211 } // namespace tervel
212 
214 #endif // TERVEL_MCAS_MCAS_H_
T mcas_remove(const int pos, T value)
This function insures that upon its return that *(cas_rows_[pos].address) no longer equals value...
Definition: mcas_imp.h:223
This class is used to represent a one of the M CAS operations performed by an MCAS operation...
Definition: mcas_casrow.h:45
void help_complete()
THis function overrides the virtual function in the OpRecord class It is called by the progress assur...
Definition: mcas.h:112
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
TODO(steven):
Definition: mcas.h:36
bool add_cas_triple(std::atomic< T > *address, T expected_value, T new_value)
This function is used to add a CAS triple to the MCAS operation.
Definition: mcas_imp.h:35
MCasState
This enum is used to indicate the state of an mcas operation DELETED is used in debugging procedures...
Definition: mcas.h:143
bool execute()
This function is called after all the CAS triples have been added to the operation.
Definition: mcas_imp.h:72
std::atomic< MCasState > state_
Definition: mcas.h:197
MultiWordCompareAndSwap class is used to perform a Multi-Word Compare-and-Swap (MCAS) To execute an M...
Definition: mcas.h:61
static constexpr void * MCAS_FAIL_CONST
Definition: mcas.h:63
T read(std::atomic< T > *address)
This function determines the logical value of the address.
Definition: mcas.h:47
bool is_watched(tervel::util::Descriptor *descr)
This method is used to determine if the passed descriptor is under rc protection. ...
Definition: descriptor_util.h:78
bool mcas_complete(int start_pos, bool wfmode=false)
This function is used to complete a currently executing MCAS operation It is most likely that this op...
Definition: mcas_imp.h:90
void cleanup(bool success)
This function is used to cleanup a completed MCAS operation It removes each MCH placed during this op...
Definition: mcas_imp.h:255
void * descriptor_read_first(std::atomic< void * > *address)
This function determines the logical value of an address which may have either a RC descriptor or a n...
Definition: descriptor_util.h:233
This class is used to create Operation Records.
Definition: progress_assurance.h:52
std::unique_ptr< CasRow< T >[]> cas_rows_
Definition: mcas.h:195
This class is the MCAS operation's helper.
Definition: mcas_casrow.h:37
bool on_is_watched()
This function overrides the virtual function in the HP::Element class It returns whether or not this ...
Definition: mcas.h:124