proxygen
folly::DelayedDestruction Class Reference

#include <DelayedDestruction.h>

Inheritance diagram for folly::DelayedDestruction:
folly::DelayedDestructionBase DeleteGuarder fizz::client::test::TestFizzClient fizz::server::test::TestFizzServer fizz::test::TestFizzBase folly::AsyncPipeReader folly::AsyncPipeWriter folly::AsyncServerSocket folly::AsyncTransport folly::HHWheelTimer folly::NotificationQueue< MessageT >::Consumer proxygen::AsyncTimeoutSet wangle::AcceptorHandshakeHelper wangle::BroadcastPool< T, R, P >::BroadcastManager wangle::ConnectionManager wangle::ManagedConnection wangle::SocketPeeker

Classes

class  Destructor
 

Public Member Functions

virtual void destroy ()
 
bool getDestroyPending () const
 
- Public Member Functions inherited from folly::DelayedDestructionBase
virtual ~DelayedDestructionBase ()=default
 

Protected Member Functions

 ~DelayedDestruction () override=default
 
 DelayedDestruction ()
 
- Protected Member Functions inherited from folly::DelayedDestructionBase
 DelayedDestructionBase ()
 
uint32_t getDestructorGuardCount () const
 

Private Member Functions

void onDelayedDestroy (bool delayed) override
 

Private Attributes

bool destroyPending_
 

Detailed Description

DelayedDestruction is a helper class to ensure objects are not deleted while they still have functions executing in a higher stack frame.

This is useful for objects that invoke callback functions, to ensure that a callback does not destroy the calling object.

Classes needing this functionality should:

  • derive from DelayedDestruction
  • make their destructor private or protected, so it cannot be called directly
  • create a DestructorGuard object on the stack in each public method that may invoke a callback

DelayedDestruction does not perform any locking. It is intended to be used only from a single thread.

Definition at line 42 of file DelayedDestruction.h.

Constructor & Destructor Documentation

folly::DelayedDestruction::~DelayedDestruction ( )
overrideprotecteddefault

Protected destructor.

Making this protected ensures that users cannot delete DelayedDestruction objects directly, and that everyone must use destroy() instead. Subclasses of DelayedDestruction must also define their destructors as protected or private in order for this to work.

This also means that DelayedDestruction objects cannot be created directly on the stack; they must always be dynamically allocated on the heap.

In order to use a DelayedDestruction object with a shared_ptr, create the shared_ptr using a DelayedDestruction::Destructor as the second argument to the shared_ptr constructor.

Referenced by getDestroyPending().

folly::DelayedDestruction::DelayedDestruction ( )
inlineprotected

Definition at line 99 of file DelayedDestruction.h.

Member Function Documentation

virtual void folly::DelayedDestruction::destroy ( )
inlinevirtual

destroy() requests destruction of the object.

This method will destroy the object after it has no more functions running higher up on the stack. (i.e., No more DestructorGuard objects exist for this object.) This method must be used instead of the destructor.

Reimplemented in folly::AsyncSocket, folly::AsyncServerSocket, folly::NotificationQueue< MessageT >::Consumer, proxygen::AsyncTimeoutSet, fizz::AsyncFizzBase, and folly::UndelayedDestruction< HHWheelTimer >.

Definition at line 51 of file DelayedDestruction.h.

References destroyPending_, folly::DelayedDestructionBase::getDestructorGuardCount(), and onDelayedDestroy().

Referenced by proxygen::HTTPSession::checkForShutdown(), folly::NotificationQueue< MessageT >::Consumer::destroy(), folly::AsyncServerSocket::destroy(), folly::AsyncSocket::destroy(), and folly::DelayedDestruction::Destructor::operator()().

51  {
52  // If guardCount_ is not 0, just set destroyPending_ to delay
53  // actual destruction.
54  if (getDestructorGuardCount() != 0) {
55  destroyPending_ = true;
56  } else {
57  onDelayedDestroy(false);
58  }
59  }
void onDelayedDestroy(bool delayed) override
bool folly::DelayedDestruction::getDestroyPending ( ) const
inline

Definition at line 76 of file DelayedDestruction.h.

References destroyPending_, and ~DelayedDestruction().

76  {
77  return destroyPending_;
78  }
void folly::DelayedDestruction::onDelayedDestroy ( bool  delayed)
inlineoverrideprivatevirtual

Implement onDelayedDestroy in subclasses. onDelayedDestroy() is invoked when the object is potentially being destroyed.

Parameters
delayedThis parameter is true if destruction was delayed because of a DestructorGuard object, or false if onDelayedDestroy() is being called directly from the destructor.

Implements folly::DelayedDestructionBase.

Reimplemented in folly::UndelayedDestruction< HHWheelTimer >.

Definition at line 111 of file DelayedDestruction.h.

Referenced by destroy().

111  {
112  // check if it is ok to destroy now
113  if (delayed && !destroyPending_) {
114  return;
115  }
116  destroyPending_ = false;
117  delete this;
118  }

Member Data Documentation

bool folly::DelayedDestruction::destroyPending_
private

destroyPending_ is set to true if destoy() is called while guardCount_ is non-zero. It is set to false before the object is deleted.

If destroyPending_ is true, the object will be destroyed the next time guardCount_ drops to 0.

Definition at line 109 of file DelayedDestruction.h.

Referenced by destroy(), and getDestroyPending().


The documentation for this class was generated from the following file: