proxygen
DestructorCheck.h
Go to the documentation of this file.
1 /*
2  * Copyright 2014-present 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 namespace folly {
19 
58  public:
59  virtual ~DestructorCheck() {
61  }
62 
63  class Safety;
64 
65  class ForwardLink {
66  // These methods are mostly private because an outside caller could violate
67  // the integrity of the linked list.
68  private:
69  void setAllDestroyed() {
70  for (auto guard = next_; guard; guard = guard->next_) {
71  guard->setDestroyed();
72  }
73  }
74 
75  // This is used to maintain the double-linked list. An intrusive list does
76  // not require any heap allocations, like a standard container would. This
77  // isolation of next_ in its own class means that the DestructorCheck can
78  // easily hold a next_ pointer without needing to hold a prev_ pointer.
79  // DestructorCheck never needs a prev_ pointer because it is the head node
80  // and this is a special list where the head never moves and never has a
81  // previous node.
82  Safety* next_{nullptr};
83 
84  friend class DestructorCheck;
85  friend class Safety;
86  };
87 
88  // See above example for usage
89  class Safety : public ForwardLink {
90  public:
91  explicit Safety(DestructorCheck& destructorCheck) {
92  // Insert this node at the head of the list.
93  prev_ = &destructorCheck.rootGuard_;
94  next_ = prev_->next_;
95  if (next_ != nullptr) {
96  next_->prev_ = this;
97  }
98  prev_->next_ = this;
99  }
100 
102  if (!destroyed()) {
103  // Remove this node from the list.
104  prev_->next_ = next_;
105  if (next_ != nullptr) {
106  next_->prev_ = prev_;
107  }
108  }
109  }
110 
111  Safety(const Safety&) = delete;
112  Safety(Safety&& goner) = delete;
113  Safety& operator=(const Safety&) = delete;
114  Safety& operator=(Safety&&) = delete;
115 
116  bool destroyed() const {
117  return prev_ == nullptr;
118  }
119 
120  private:
121  void setDestroyed() {
122  prev_ = nullptr;
123  }
124 
125  // This field is used to maintain the double-linked list. If the root has
126  // been destroyed then the field is set to the nullptr sentinel value.
128 
129  friend class ForwardLink;
130  };
131 
132  private:
134 };
135 
136 } // namespace folly
—— Concurrent Priority Queue Implementation ——
Definition: AtomicBitSet.h:29
GuardImpl guard(ErrorHandler &&handler)
Definition: Base.h:840
Safety(DestructorCheck &destructorCheck)