Fix virtual struct bug
[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   template <typename> friend class hazptr_owner;
57
58   /** Constant -- May be changed to parameter in the future */
59   enum { kScanThreshold = 3 };
60
61   memory_resource* mr_;
62   std::atomic<hazptr_rec*> hazptrs_ = {nullptr};
63   std::atomic<hazptr_obj*> retired_ = {nullptr};
64   std::atomic<int> hcount_ = {0};
65   std::atomic<int> rcount_ = {0};
66
67   void objRetire(hazptr_obj*);
68   hazptr_rec* hazptrAcquire();
69   void hazptrRelease(hazptr_rec*) noexcept;
70   int pushRetired(hazptr_obj* head, hazptr_obj* tail, int count);
71   void tryBulkReclaim();
72   void bulkReclaim();
73   void try_reclaim();
74 };
75
76 /** Get the default hazptr_domain */
77 hazptr_domain& default_hazptr_domain();
78
79 /** Definition of hazptr_obj */
80 class hazptr_obj {
81   friend class hazptr_domain;
82   template <typename, typename>
83   friend class hazptr_obj_base;
84
85   void (*reclaim_)(hazptr_obj*);
86   hazptr_obj* next_;
87   const void* getObjPtr() const;
88 };
89
90 /** Definition of hazptr_obj_base */
91 template <typename T, typename Deleter = std::default_delete<T>>
92 class hazptr_obj_base : public hazptr_obj {
93  public:
94   /* Retire a removed object and pass the responsibility for
95    * reclaiming it to the hazptr library */
96   void retire(
97       hazptr_domain& domain = default_hazptr_domain(),
98       Deleter reclaim = {});
99
100  private:
101   Deleter deleter_;
102 };
103
104 /** hazptr_owner: Template for automatic acquisition and release of
105  *  hazard pointers, and interface for hazard pointer operations. */
106 template <typename T> class hazptr_owner {
107  public:
108   /* Constructor automatically acquires a hazard pointer. */
109   explicit hazptr_owner(hazptr_domain& domain = default_hazptr_domain());
110   /* Destructor automatically clears and releases the owned hazard pointer. */
111   ~hazptr_owner();
112
113   /* Copy and move constructors and assignment operators are
114    * disallowed because:
115    * - Each hazptr_owner owns exactly one hazard pointer at any time.
116    * - Each hazard pointer may have up to one owner at any time. */
117   hazptr_owner(const hazptr_owner&) = delete;
118   hazptr_owner(hazptr_owner&&) = delete;
119   hazptr_owner& operator=(const hazptr_owner&) = delete;
120   hazptr_owner& operator=(hazptr_owner&&) = delete;
121
122   /** Hazard pointer operations */
123   /* Returns a protected pointer from the source */
124   template <typename A = std::atomic<T*>>
125   T* get_protected(const A& src) noexcept;
126   /* Return true if successful in protecting ptr if src == ptr after
127    * setting the hazard pointer.  Otherwise sets ptr to src. */
128   template <typename A = std::atomic<T*>>
129   bool try_protect(T*& ptr, const A& src) noexcept;
130   /* Set the hazard pointer to ptr */
131   void set(const T* ptr) noexcept;
132   /* Clear the hazard pointer */
133   void clear() noexcept;
134
135   /* Swap ownership of hazard pointers between hazptr_owner-s. */
136   /* Note: The owned hazard pointers remain unmodified during the swap
137    * and continue to protect the respective objects that they were
138    * protecting before the swap, if any. */
139   void swap(hazptr_owner&) noexcept;
140
141  private:
142   hazptr_domain* domain_;
143   hazptr_rec* hazptr_;
144 };
145
146 template <typename T>
147 void swap(hazptr_owner<T>&, hazptr_owner<T>&) noexcept;
148
149 } // namespace hazptr
150 } // namespace folly
151
152 #include "hazptr-impl.h"