1
0
Fork 0
mirror of https://github.com/ossrs/srs.git synced 2025-02-15 04:42:04 +00:00

Kernel: Refine lazy sweep resource.

This commit is contained in:
winlin 2022-10-04 20:47:49 +08:00
parent eb04f92176
commit d4ce877407
4 changed files with 22 additions and 22 deletions

View file

@ -420,7 +420,7 @@ srs_error_t SrsSweepGc::start()
return err; return err;
} }
void SrsSweepGc::remove(ISrsLazyResource* c) void SrsSweepGc::remove(SrsLazyObject* c)
{ {
// TODO: FIXME: MUST lazy sweep. // TODO: FIXME: MUST lazy sweep.
srs_freep(c); srs_freep(c);

View file

@ -132,7 +132,7 @@ public:
virtual ~SrsSweepGc(); virtual ~SrsSweepGc();
public: public:
virtual srs_error_t start(); virtual srs_error_t start();
virtual void remove(ISrsLazyResource* c); virtual void remove(SrsLazyObject* c);
}; };
extern ISrsLazyGc* _srs_gc; extern ISrsLazyGc* _srs_gc;
@ -140,14 +140,14 @@ extern ISrsLazyGc* _srs_gc;
// A wrapper template for lazy-sweep resource. // A wrapper template for lazy-sweep resource.
// See https://github.com/ossrs/srs/issues/3176#lazy-sweep // See https://github.com/ossrs/srs/issues/3176#lazy-sweep
template<typename T> template<typename T>
class SrsLazyResourceWrapper : public ISrsResource class SrsLazyObjectWrapper : public ISrsResource
{ {
private: private:
T* resource_; T* resource_;
ISrsResource* wrapper_; ISrsResource* wrapper_;
bool is_root_; bool is_root_;
public: public:
SrsLazyResourceWrapper(T* resource = NULL, ISrsResource* wrapper = NULL) { SrsLazyObjectWrapper(T* resource = NULL, ISrsResource* wrapper = NULL) {
wrapper_ = wrapper ? wrapper : this; wrapper_ = wrapper ? wrapper : this;
resource_ = resource ? resource : new T(); resource_ = resource ? resource : new T();
resource_->gc_use(wrapper_); resource_->gc_use(wrapper_);
@ -157,7 +157,7 @@ public:
resource_->gc_set_creator_wrapper(wrapper_); resource_->gc_set_creator_wrapper(wrapper_);
} }
} }
virtual ~SrsLazyResourceWrapper() { virtual ~SrsLazyObjectWrapper() {
resource_->gc_dispose(wrapper_); resource_->gc_dispose(wrapper_);
if (is_root_) { if (is_root_) {
@ -169,8 +169,8 @@ public:
} }
} }
public: public:
SrsLazyResourceWrapper<T>* copy() { SrsLazyObjectWrapper<T>* copy() {
return new SrsLazyResourceWrapper<T>(resource_); return new SrsLazyObjectWrapper<T>(resource_);
} }
T* resource() { T* resource() {
return resource_; return resource_;
@ -189,7 +189,7 @@ public:
// See https://github.com/ossrs/srs/issues/3176#lazy-sweep // See https://github.com/ossrs/srs/issues/3176#lazy-sweep
#define SRS_LAZY_WRAPPER_GENERATOR(Resource, IWrapper, IResource) \ #define SRS_LAZY_WRAPPER_GENERATOR(Resource, IWrapper, IResource) \
private: \ private: \
SrsLazyResourceWrapper<Resource> impl_; \ SrsLazyObjectWrapper<Resource> impl_; \
public: \ public: \
Resource##Wrapper(Resource* resource = NULL) : impl_(resource, this) { \ Resource##Wrapper(Resource* resource = NULL) : impl_(resource, this) { \
} \ } \

View file

@ -38,17 +38,17 @@ ISrsConnection::~ISrsConnection()
{ {
} }
ISrsLazyResource::ISrsLazyResource() SrsLazyObject::SrsLazyObject()
{ {
gc_ref_ = 0; gc_ref_ = 0;
gc_creator_wrapper_ = NULL; gc_creator_wrapper_ = NULL;
} }
ISrsLazyResource::~ISrsLazyResource() SrsLazyObject::~SrsLazyObject()
{ {
} }
ISrsLazyResource* ISrsLazyResource::gc_use(ISrsResource* wrapper) SrsLazyObject* SrsLazyObject::gc_use(ISrsResource* wrapper)
{ {
srs_assert(wrapper); srs_assert(wrapper);
if (std::find(gc_wrappers_.begin(), gc_wrappers_.end(), wrapper) == gc_wrappers_.end()) { if (std::find(gc_wrappers_.begin(), gc_wrappers_.end(), wrapper) == gc_wrappers_.end()) {
@ -59,7 +59,7 @@ ISrsLazyResource* ISrsLazyResource::gc_use(ISrsResource* wrapper)
return this; return this;
} }
ISrsLazyResource* ISrsLazyResource::gc_dispose(ISrsResource* wrapper) SrsLazyObject* SrsLazyObject::gc_dispose(ISrsResource* wrapper)
{ {
srs_assert(wrapper); srs_assert(wrapper);
vector<ISrsResource*>::iterator it = std::find(gc_wrappers_.begin(), gc_wrappers_.end(), wrapper); vector<ISrsResource*>::iterator it = std::find(gc_wrappers_.begin(), gc_wrappers_.end(), wrapper);
@ -71,22 +71,22 @@ ISrsLazyResource* ISrsLazyResource::gc_dispose(ISrsResource* wrapper)
return this; return this;
} }
int32_t ISrsLazyResource::gc_ref() int32_t SrsLazyObject::gc_ref()
{ {
return gc_ref_; return gc_ref_;
} }
void ISrsLazyResource::gc_set_creator_wrapper(ISrsResource* wrapper) void SrsLazyObject::gc_set_creator_wrapper(ISrsResource* wrapper)
{ {
gc_creator_wrapper_ = wrapper; gc_creator_wrapper_ = wrapper;
} }
ISrsResource* ISrsLazyResource::gc_creator_wrapper() ISrsResource* SrsLazyObject::gc_creator_wrapper()
{ {
return gc_creator_wrapper_; return gc_creator_wrapper_;
} }
ISrsResource* ISrsLazyResource::gc_available_wrapper() ISrsResource* SrsLazyObject::gc_available_wrapper()
{ {
return gc_wrappers_.empty() ? NULL : gc_wrappers_.front(); return gc_wrappers_.empty() ? NULL : gc_wrappers_.front();
} }

View file

@ -50,7 +50,7 @@ public:
// Lazy-sweep resource, never sweep util all wrappers are freed. // Lazy-sweep resource, never sweep util all wrappers are freed.
// See https://github.com/ossrs/srs/issues/3176#lazy-sweep // See https://github.com/ossrs/srs/issues/3176#lazy-sweep
class ISrsLazyResource : public ISrsResource class SrsLazyObject
{ {
private: private:
// The reference count of resource, 0 is no wrapper and safe to sweep. // The reference count of resource, 0 is no wrapper and safe to sweep.
@ -61,13 +61,13 @@ private:
// All available wrappers. // All available wrappers.
std::vector<ISrsResource*> gc_wrappers_; std::vector<ISrsResource*> gc_wrappers_;
public: public:
ISrsLazyResource(); SrsLazyObject();
virtual ~ISrsLazyResource(); virtual ~SrsLazyObject();
public: public:
// For wrapper to use this resource. // For wrapper to use this resource.
virtual ISrsLazyResource* gc_use(ISrsResource* wrapper); virtual SrsLazyObject* gc_use(ISrsResource* wrapper);
// For wrapper to dispose this resource. // For wrapper to dispose this resource.
virtual ISrsLazyResource* gc_dispose(ISrsResource* wrapper); virtual SrsLazyObject* gc_dispose(ISrsResource* wrapper);
// The current reference count of resource. // The current reference count of resource.
virtual int32_t gc_ref(); virtual int32_t gc_ref();
public: public:
@ -89,7 +89,7 @@ public:
virtual ~ISrsLazyGc(); virtual ~ISrsLazyGc();
public: public:
// Remove then free the specified resource. // Remove then free the specified resource.
virtual void remove(ISrsLazyResource* c) = 0; virtual void remove(SrsLazyObject* c) = 0;
}; };
#endif #endif