diff --git a/trunk/src/app/srs_app_conn.hpp b/trunk/src/app/srs_app_conn.hpp index 1cfd1a9fe33..f4b600608bd 100644 --- a/trunk/src/app/srs_app_conn.hpp +++ b/trunk/src/app/srs_app_conn.hpp @@ -139,15 +139,54 @@ extern ISrsLazyGc* _srs_gc; // A wrapper template for lazy-sweep resource. // See https://github.com/ossrs/srs/issues/3176#lazy-sweep +// +// Usage for resource which manages itself in coroutine cycle, see SrsLazyGbSession: +// class Resource { +// private: +// SrsLazyObjectWrapper* wrapper_; +// private: +// friend class SrsLazyObjectWrapper; +// Resource(SrsLazyObjectWrapper* wrapper) { wrapper_ = wrapper; } +// public: +// srs_error_t Resource::cycle() { +// srs_error_t err = do_cycle(); +// _srs_gb_manager->remove(wrapper_); +// return err; +// } +// }; +// SrsLazyObjectWrapper* obj = new SrsLazyObjectWrapper*(); +// _srs_gb_manager->add(obj); // Add wrapper to resource manager. +// Start a coroutine to do obj->resource()->cycle(). +// +// Usage for resource managed by other object: +// class Resource { +// private: +// friend class SrsLazyObjectWrapper; +// Resource(SrsLazyObjectWrapper* /*wrapper*/) { +// } +// }; +// class Manager { +// private: +// SrsLazyObjectWrapper* wrapper_; +// public: +// Manager() { wrapper_ = new SrsLazyObjectWrapper(); } +// ~Manager() { srs_freep(wrapper_); } +// }; +// Manager* manager = new Manager(); +// srs_freep(manager); +// +// Note that under-layer resource are destroyed by _srs_gc, which is literally equal to srs_freep. However, the root +// wrapper might be managed by other resource manager, such as _srs_gb_manager for SrsLazyGbSession. Furthermore, other +// copied out wrappers might be freed by srs_freep. All are ok, because all wrapper and resources are simply normal +// object, so if you added to manager then you should use manager to remove it, and you can also directly delete it. template class SrsLazyObjectWrapper : public ISrsResource { private: T* resource_; public: - SrsLazyObjectWrapper(T* resource = NULL) { - resource_ = resource ? resource : new T(this); - resource_->gc_use(); + SrsLazyObjectWrapper() { + init(new T(this)); } virtual ~SrsLazyObjectWrapper() { resource_->gc_dispose(); @@ -155,6 +194,14 @@ class SrsLazyObjectWrapper : public ISrsResource _srs_gc->remove(resource_); } } +private: + SrsLazyObjectWrapper(T* resource) { + init(resource); + } + void init(T* resource) { + resource_ = resource; + resource_->gc_use(); + } public: SrsLazyObjectWrapper* copy() { return new SrsLazyObjectWrapper(resource_);