// // Copyright 2020 gRPC authors. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. // #ifndef GRPC_SRC_CORE_LIB_GPRPP_DUAL_REF_COUNTED_H #define GRPC_SRC_CORE_LIB_GPRPP_DUAL_REF_COUNTED_H #include <grpc/support/port_platform.h> #include <atomic> #include <cstdint> #include <grpc/support/log.h> #include "src/core/lib/gprpp/debug_location.h" #include "src/core/lib/gprpp/orphanable.h" #include "src/core/lib/gprpp/ref_counted_ptr.h" namespace grpc_core { // DualRefCounted is an interface for reference-counted objects with two // classes of refs: strong refs (usually just called "refs") and weak refs. // This supports cases where an object needs to start shutting down when // all external callers are done with it (represented by strong refs) but // cannot be destroyed until all internal callbacks are complete // (represented by weak refs). // // Each class of refs can be incremented and decremented independently. // Objects start with 1 strong ref and 0 weak refs at instantiation. // When the strong refcount reaches 0, the object's Orphan() method is called. // When the weak refcount reaches 0, the object is destroyed. // // This will be used by CRTP (curiously-recurring template pattern), e.g.: // class MyClass : public RefCounted<MyClass> { ... }; template <typename Child> class DualRefCounted : public Orphanable { … }; } // namespace grpc_core #endif // GRPC_SRC_CORE_LIB_GPRPP_DUAL_REF_COUNTED_H