b54240f2a793ad995ca79afdf586637bf25b7b8a
[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   hazptr_domain& domain_;
57
58   /* Used by the single writer */
59   void locate_lower_bound(const T v, std::atomic<Node*>*& prev) const {
60     auto curr = prev->load();
61     while (curr) {
62       if (curr->elem_ >= v) break;
63       prev = &(curr->next_);
64       curr = curr->next_.load();
65     }
66     return;
67   }
68
69  public:
70   explicit SWMRListSet(hazptr_domain& domain = default_hazptr_domain())
71       : domain_(domain) {}
72
73   ~SWMRListSet() {
74     Node* next;
75     for (auto p = head_.load(); p; p = next) {
76       next = p->next_.load();
77       delete p;
78     }
79   }
80
81   bool add(const T v) {
82     auto prev = &head_;
83     locate_lower_bound(v, prev);
84     auto curr = prev->load();
85     if (curr && curr->elem_ == v) return false;
86     prev->store(new Node(v, curr));
87     return true;
88   }
89
90   bool remove(const T v) {
91     auto prev = &head_;
92     locate_lower_bound(v, prev);
93     auto curr = prev->load();
94     if (!curr || curr->elem_ != v) return false;
95     prev->store(curr->next_.load());
96     curr->retire(domain_);
97     return true;
98   }
99   /* Used by readers */
100   bool contains(const T val) const {
101     /* Acquire two hazard pointers for hand-over-hand traversal. */
102     hazptr_owner<Node> hptr_prev(domain_);
103     hazptr_owner<Node> hptr_curr(domain_);
104     T elem;
105     bool done = false;
106     while (!done) {
107       auto prev = &head_;
108       auto curr = prev->load();
109       while (true) {
110         if (!curr) { done = true; break; }
111         if (!hptr_curr.try_protect(curr, *prev))
112           break;
113         auto next = curr->next_.load();
114         elem = curr->elem_;
115         if (prev->load() != curr) break;
116         if (elem >= val) { done = true; break; }
117         prev = &(curr->next_);
118         curr = next;
119         /* Swap does not change the values of the owned hazard
120          * pointers themselves. After the swap, The hazard pointer
121          * owned by hptr_prev continues to protect the node that
122          * contains the pointer *prev. The hazard pointer owned by
123          * hptr_curr will continue to protect the node that contains
124          * the old *prev (unless the old prev was &head), which no
125          * longer needs protection, so hptr_curr's hazard pointer is
126          * now free to protect *curr in the next iteration (if curr !=
127          * null).
128          */
129         swap(hptr_curr, hptr_prev);
130       }
131     }
132     return elem == val;
133     /* The hazard pointers are released automatically. */
134   }
135 };
136
137 } // namespace folly {
138 } // namespace hazptr {