Tervel  1.0.0
A collection of wait-free containers and algorithms.
ring_buffer_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_RINGBUFFER_RINGBUFFER_OPREC_H_
26 #define TERVEL_CONTAINERS_WF_RINGBUFFER_RINGBUFFER_OPREC_H_
27 
28 #include <atomic>
29 #include <assert.h>
30 #include <cstddef>
31 #include <memory>
32 #include <thread>
33 #include <string>
34 
38 
39 namespace tervel {
40 namespace containers {
41 namespace wf {
42 
43 template<typename T>
44 class RingBuffer<T>::BufferOp : public util::OpRecord {
45  public:
47  rb_ = rb;
48  };
49 
51  Helper *h = helper_.load();
52  if (h != nullptr && h != fail_val_) {
53  delete h;
54  }
55  };
56 
57  virtual void * associate(Helper *h) = 0;
58 
59  bool privAssociate(Helper *h) {
60  Helper *temp = nullptr;
61  bool res = helper_.compare_exchange_strong(temp, h);
62  if (res || temp == nullptr) { // success
63  return true;
64  } else { // fail
65  return false;
66  }
67  };
68 
69  bool valid(Helper * h) {
70  return helper_.load() == h;
71  };
72 
73  void fail() {
74  Helper *temp = nullptr;
75  helper_.compare_exchange_strong(temp, fail_val_);
76  };
77 
78  bool isFail(Helper * &h) {
79  return (h=helper_.load()) == fail_val_;
80  };
81 
82 
84  return helper_.load();
85  };
86 
87  bool notDone() {
88  return helper_.load() == nullptr;
89  };
90 
91  // on_is_watched() function is not needed because helpers
92  // are removed before removing the watch on the op record
93 
94  // private:
95  static constexpr Helper * fail_val_ = reinterpret_cast<Helper *>(0x1L);
96 
98  std::atomic<Helper *> helper_{nullptr};
100 };
101 
102 } // namespace wf
103 } // namespace containers
104 } // namespace tervel
105 #endif // TERVEL_CONTAINERS_WF_RINGBUFFER_RINGBUFFER_OPREC_H_
106 
107 
Helper * getHelper()
Definition: ring_buffer_op.h:83
bool privAssociate(Helper *h)
Definition: ring_buffer_op.h:59
void fail()
Definition: ring_buffer_op.h:73
TODO(steven):
Definition: mcas.h:36
bool valid(Helper *h)
Definition: ring_buffer_op.h:69
#define DISALLOW_COPY_AND_ASSIGN(TypeName)
Definition: util.h:205
This is a non-blocking FIFO ring buffer design that was made wait-free by applying a progress assuran...
Definition: ring_buffer.h:73
Definition: ring_buffer_op.h:44
BufferOp(RingBuffer< T > *rb)
Definition: ring_buffer_op.h:46
This class is used to create Operation Records.
Definition: progress_assurance.h:52
~BufferOp()
Definition: ring_buffer_op.h:50
RingBuffer< T > * rb_
Definition: ring_buffer_op.h:97
bool isFail(Helper *&h)
Definition: ring_buffer_op.h:78
bool notDone()
Definition: ring_buffer_op.h:87