2 * Copyright 2015 Facebook, Inc.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
20 #include <boost/noncopyable.hpp>
22 #include <glog/logging.h>
28 * DelayedDestructionBase is a helper class to ensure objects are not deleted
29 * while they still have functions executing in a higher stack frame.
31 * This is useful for objects that invoke callback functions, to ensure that a
32 * callback does not destroy the calling object.
34 * Classes needing this functionality should:
35 * - derive from DelayedDestructionBase directly
36 * - pass a callback to onDestroy_ which'll be called before the object is
37 * going to be destructed
38 * - create a DestructorGuard object on the stack in each public method that
39 * may invoke a callback
41 * DelayedDestructionBase does not perform any locking. It is intended to be
42 * used only from a single thread.
44 class DelayedDestructionBase : private boost::noncopyable {
46 virtual ~DelayedDestructionBase() = default;
49 * Classes should create a DestructorGuard object on the stack in any
50 * function that may invoke callback functions.
52 * The DestructorGuard prevents the guarded class from being destroyed while
53 * it exists. Without this, the callback function could delete the guarded
54 * object, causing problems when the callback function returns and the
55 * guarded object's method resumes execution.
57 class DestructorGuard {
60 explicit DestructorGuard(DelayedDestructionBase* dd) : dd_(dd) {
62 assert(dd_->guardCount_ > 0); // check for wrapping
65 DestructorGuard(const DestructorGuard& dg) : dd_(dg.dd_) {
67 assert(dd_->guardCount_ > 0); // check for wrapping
71 assert(dd_->guardCount_ > 0);
73 if (dd_->guardCount_ == 0) {
74 dd_->onDestroy_(true);
79 DelayedDestructionBase* dd_;
83 DelayedDestructionBase()
87 * Get the number of DestructorGuards currently protecting this object.
89 * This is primarily intended for debugging purposes, such as asserting
90 * that an object has at least 1 guard.
92 uint32_t getDestructorGuardCount() const {
97 * Implement onDestroy_ in subclasses.
98 * onDestroy_() is invoked when the object is potentially being destroyed.
100 * @param delayed This parameter is true if destruction was delayed because
101 * of a DestructorGuard object, or false if onDestroy_() is
102 * being called directly from the destructor.
104 std::function<void(bool)> onDestroy_;
108 * guardCount_ is incremented by DestructorGuard, to indicate that one of
109 * the DelayedDestructionBase object's methods is currently running.
111 * If the destructor is called while guardCount_ is non-zero, destruction
112 * will be delayed until guardCount_ drops to 0. This allows
113 * DelayedDestructionBase objects to invoke callbacks without having to worry
114 * about being deleted before the callback returns.
116 uint32_t guardCount_;