Optimize local and bulk management of hazptr_holder-s
[folly.git] / folly / experimental / hazptr / example / SWMRList.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
18 #include <folly/experimental/hazptr/debug.h>
19 #include <folly/experimental/hazptr/hazptr.h>
20
21 namespace folly {
22 namespace hazptr {
23
24 /** Set implemented as an ordered singly-linked list.
25  *
26  *  A single writer thread may add or remove elements. Multiple reader
27  *  threads may search the set concurrently with each other and with
28  *  the writer's operations.
29  */
30 template <typename T>
31 class SWMRListSet {
32   template <typename Node>
33   struct Reclaimer {
34     void operator()(Node* p) {
35       DEBUG_PRINT(p << " " << sizeof(Node));
36       delete p;
37     }
38   };
39
40   class Node : public hazptr_obj_base<Node, Reclaimer<Node>> {
41     friend SWMRListSet;
42     T elem_;
43     std::atomic<Node*> next_;
44
45     Node(T e, Node* n) : elem_(e), next_(n) {
46       DEBUG_PRINT(this << " " << e << " " << n);
47     }
48
49    public:
50     ~Node() {
51       DEBUG_PRINT(this);
52     }
53   };
54
55   std::atomic<Node*> head_ = {nullptr};
56
57   /* Used by the single writer */
58   void locate_lower_bound(const T& v, std::atomic<Node*>*& prev) const {
59     auto curr = prev->load(std::memory_order_relaxed);
60     while (curr) {
61       if (curr->elem_ >= v) break;
62       prev = &(curr->next_);
63       curr = curr->next_.load(std::memory_order_relaxed);
64     }
65     return;
66   }
67
68  public:
69   ~SWMRListSet() {
70     Node* next;
71     for (auto p = head_.load(); p; p = next) {
72       next = p->next_.load();
73       delete p;
74     }
75   }
76
77   bool add(T v) {
78     auto prev = &head_;
79     locate_lower_bound(v, prev);
80     auto curr = prev->load(std::memory_order_relaxed);
81     if (curr && curr->elem_ == v) return false;
82     prev->store(new Node(std::move(v), curr));
83     return true;
84   }
85
86   bool remove(const T& v) {
87     auto prev = &head_;
88     locate_lower_bound(v, prev);
89     auto curr = prev->load(std::memory_order_relaxed);
90     if (!curr || curr->elem_ != v) return false;
91     Node *curr_next = curr->next_.load();
92     // Patch up the actual list...
93     prev->store(curr_next, std::memory_order_release);
94     // ...and only then null out the removed node.
95     curr->next_.store(nullptr, std::memory_order_release);
96     curr->retire();
97     return true;
98   }
99
100   /* Used by readers */
101   bool contains(const T& val) const {
102     /* Two hazard pointers for hand-over-hand traversal. */
103     hazptr_local<2> hptr;
104     hazptr_holder* hptr_prev = &hptr[0];
105     hazptr_holder* hptr_curr = &hptr[1];
106     while (true) {
107       auto prev = &head_;
108       auto curr = prev->load(std::memory_order_acquire);
109       while (true) {
110         if (!curr) { return false; }
111         if (!hptr_curr->try_protect(curr, *prev))
112           break;
113         auto next = curr->next_.load(std::memory_order_acquire);
114         if (prev->load(std::memory_order_acquire) != curr)
115           break;
116         if (curr->elem_ == val) {
117             return true;
118         } else if (!(curr->elem_ < val)) {
119             return false;  // because the list is sorted
120         }
121         prev = &(curr->next_);
122         curr = next;
123         std::swap(hptr_curr, hptr_prev);
124       }
125     }
126   }
127 };
128
129 } // namespace folly
130 } // namespace hazptr