Update hazard pointer interface to standard proposal P0233R4
[folly.git] / folly / experimental / hazptr / hazptr.h
1 /*
2  * Copyright 2017 Facebook, Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *   http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 #pragma once
17 #define HAZPTR_H
18
19 #include <atomic>
20 #include <functional>
21 #include <memory>
22 #include <type_traits>
23
24 /* Stand-in for C++17 std::pmr::memory_resource */
25 #include <folly/experimental/hazptr/memory_resource.h>
26
27 namespace folly {
28 namespace hazptr {
29
30 /** hazptr_rec: Private class that contains hazard pointers. */
31 class hazptr_rec;
32
33 /** hazptr_obj: Private class for objects protected by hazard pointers. */
34 class hazptr_obj;
35
36 /** hazptr_obj_base: Base template for objects protected by hazard pointers. */
37 template <typename T, typename Deleter>
38 class hazptr_obj_base;
39
40 /** hazptr_domain: Class of hazard pointer domains. Each domain manages a set
41  *  of hazard pointers and a set of retired objects. */
42 class hazptr_domain {
43  public:
44   constexpr explicit hazptr_domain(
45       memory_resource* = get_default_resource()) noexcept;
46   ~hazptr_domain();
47
48   hazptr_domain(const hazptr_domain&) = delete;
49   hazptr_domain(hazptr_domain&&) = delete;
50   hazptr_domain& operator=(const hazptr_domain&) = delete;
51   hazptr_domain& operator=(hazptr_domain&&) = delete;
52
53  private:
54   template <typename, typename>
55   friend class hazptr_obj_base;
56   friend class hazptr_holder;
57
58   memory_resource* mr_;
59   std::atomic<hazptr_rec*> hazptrs_ = {nullptr};
60   std::atomic<hazptr_obj*> retired_ = {nullptr};
61   std::atomic<int> hcount_ = {0};
62   std::atomic<int> rcount_ = {0};
63
64   void objRetire(hazptr_obj*);
65   hazptr_rec* hazptrAcquire();
66   void hazptrRelease(hazptr_rec*) noexcept;
67   int pushRetired(hazptr_obj* head, hazptr_obj* tail, int count);
68   void tryBulkReclaim();
69   void bulkReclaim();
70 };
71
72 /** Get the default hazptr_domain */
73 hazptr_domain& default_hazptr_domain();
74
75 /** Definition of hazptr_obj */
76 class hazptr_obj {
77   friend class hazptr_domain;
78   template <typename, typename>
79   friend class hazptr_obj_base;
80
81   void (*reclaim_)(hazptr_obj*);
82   hazptr_obj* next_;
83   const void* getObjPtr() const;
84 };
85
86 /** Definition of hazptr_obj_base */
87 template <typename T, typename Deleter = std::default_delete<T>>
88 class hazptr_obj_base : public hazptr_obj {
89  public:
90   /* Retire a removed object and pass the responsibility for
91    * reclaiming it to the hazptr library */
92   void retire(
93       hazptr_domain& domain = default_hazptr_domain(),
94       Deleter reclaim = {});
95
96  private:
97   Deleter deleter_;
98 };
99
100 /** hazptr_holder: Class for automatic acquisition and release of
101  *  hazard pointers, and interface for hazard pointer operations. */
102 class hazptr_holder {
103  public:
104   /* Constructor automatically acquires a hazard pointer. */
105   explicit hazptr_holder(hazptr_domain& domain = default_hazptr_domain());
106   /* Destructor automatically clears and releases the owned hazard pointer. */
107   ~hazptr_holder();
108
109   /* Copy and move constructors and assignment operators are
110    * disallowed because:
111    * - Each hazptr_holder owns exactly one hazard pointer at any time.
112    * - Each hazard pointer may have up to one owner at any time. */
113   hazptr_holder(const hazptr_holder&) = delete;
114   hazptr_holder(hazptr_holder&&) = delete;
115   hazptr_holder& operator=(const hazptr_holder&) = delete;
116   hazptr_holder& operator=(hazptr_holder&&) = delete;
117
118   /** Hazard pointer operations */
119   /* Returns a protected pointer from the source */
120   template <typename T>
121   T* get_protected(const std::atomic<T*>& src) noexcept;
122   /* Return true if successful in protecting ptr if src == ptr after
123    * setting the hazard pointer.  Otherwise sets ptr to src. */
124   template <typename T>
125   bool try_protect(T*& ptr, const std::atomic<T*>& src) noexcept;
126   /* Set the hazard pointer to ptr */
127   template <typename T>
128   void reset(const T* ptr) noexcept;
129   /* Set the hazard pointer to nullptr */
130   void reset(std::nullptr_t = nullptr) noexcept;
131
132   /* Swap ownership of hazard pointers between hazptr_holder-s. */
133   /* Note: The owned hazard pointers remain unmodified during the swap
134    * and continue to protect the respective objects that they were
135    * protecting before the swap, if any. */
136   void swap(hazptr_holder&) noexcept;
137
138  private:
139   hazptr_domain* domain_;
140   hazptr_rec* hazptr_;
141 };
142
143 void swap(hazptr_holder&, hazptr_holder&) noexcept;
144
145 } // namespace hazptr
146 } // namespace folly
147
148 #include "hazptr-impl.h"