Tervel  1.0.0
A collection of wait-free containers and algorithms.
hazard_pointer.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_MEMORY_HP_HAZARD_POINTER_H_
26 #define TERVEL_MEMORY_HP_HAZARD_POINTER_H_
27 
28 #include <atomic>
29 #include <utility>
30 
31 #include <assert.h>
32 #include <stddef.h>
33 #include <memory>
34 #include <cstdint>
35 
36 #include <tervel/util/info.h>
37 #include <tervel/util/util.h>
39 
40 namespace tervel {
41 namespace util {
42 namespace memory {
43 namespace hp {
44 
45 class Element;
46 
57  public:
58  enum class SlotID : size_t {SHORTUSE = 0, SHORTUSE2, PROG_ASSUR, END};
59 
60  explicit HazardPointer(int num_threads);
62 
63  // -------
64  // Static Functions
65  // -------
66 
81  static bool watch(SlotID slot_id, Element *elem, std::atomic<void *> *address,
82  void *expected, HazardPointer * const hazard_pointer =
83  tervel::tl_thread_info->get_hazard_pointer());
84 
97  static bool watch(SlotID slot_id, void *value, std::atomic<void *> *address
98  , void *expected, HazardPointer * const hazard_pointer =
99  tervel::tl_thread_info->get_hazard_pointer());
100 
107  static void unwatch(SlotID slot_id, HazardPointer * const hazard_pointer =
108  tervel::tl_thread_info->get_hazard_pointer());
109 
117  static void unwatch(SlotID slot_id, Element *descr,
118  HazardPointer * const hazard_pointer =
119  tervel::tl_thread_info->get_hazard_pointer());
120 
128  static bool is_watched(Element *descr, HazardPointer * const hazard_pointer =
129  tervel::tl_thread_info->get_hazard_pointer());
130 
138  static bool is_watched(void *value, HazardPointer * const hazard_pointer =
139  tervel::tl_thread_info->get_hazard_pointer());
140 
141 
142  // -------
143  // Member Functions
144  // -------
152  void watch(SlotID slot, void *value) {
153  watches_[get_slot(slot)].store(value);
154  }
155 
162  void clear_watch(SlotID slot) {
163  watches_[get_slot(slot)].store(nullptr);
164  }
165 
166 
173  bool contains(void *value) {
174  for (size_t i = 0; i < num_slots_; i++) {
175  if (watches_[i].load() == value) {
176  return true;
177  }
178  }
179  return false;
180  }
181 
182  private:
189  size_t get_slot(SlotID id) {
190  size_t s = static_cast<size_t>(id) + (static_cast<size_t>(SlotID::END) *
192  assert(s < num_slots_);
193  return s;
194  }
195 
196  std::unique_ptr<std::atomic<void *>[]> watches_;
197  const size_t num_slots_;
198 
199  public:
200  // Shared HP Element list manager
202 
203 
205 }; // HazardPointer
206 
207 
208 } // namespace hp
209 } // namespace memory
210 } // namepsace UTIL
211 } // namespace tervel
212 
213 #endif // TERVEL_MEMORY_HP_HAZARD_POINTER_H_
size_t get_slot(SlotID id)
This function calculates a the position of a threads slot for the specified SlotID.
Definition: hazard_pointer.h:189
static bool is_watched(Element *descr, HazardPointer *const hazard_pointer=tervel::tl_thread_info->get_hazard_pointer())
This method is used to determine if a hazard pointer watch exists on a passed value.
std::unique_ptr< std::atomic< void * >[]> watches_
Definition: hazard_pointer.h:196
TODO(steven):
Definition: mcas.h:36
void clear_watch(SlotID slot)
This function takes a SlotID and stores null into that the threads alloted slot for that id in the ha...
Definition: hazard_pointer.h:162
const uint64_t get_thread_id()
A unique ID among all active threads.
const size_t num_slots_
Definition: hazard_pointer.h:197
bool contains(void *value)
This function returns true of the specified value is being watched.
Definition: hazard_pointer.h:173
This class is used for the creation of Hazard Pointer Protected Objects Objects which extend it have ...
Definition: hp_element.h:53
Encapsulates a shared central 'to free list' between several thread-local lists.
Definition: list_manager.h:58
__thread ThreadContext * tl_thread_info
static void unwatch(SlotID slot_id, HazardPointer *const hazard_pointer=tervel::tl_thread_info->get_hazard_pointer())
This method is used to remove the hazard pointer watch.
void watch(SlotID slot, void *value)
This function takes a SlotID and stores the specified value into that the threads alloted slot for th...
Definition: hazard_pointer.h:152
static bool watch(SlotID slot_id, Element *elem, std::atomic< void * > *address, void *expected, HazardPointer *const hazard_pointer=tervel::tl_thread_info->get_hazard_pointer())
This method is used to achieve a hazard pointer watch on the the based descr.
util::memory::hp::ListManager hp_list_manager_
Definition: hazard_pointer.h:201
This class is used to maintain the list of hazard pointed objects.
Definition: hazard_pointer.h:56
SlotID
Definition: hazard_pointer.h:58