From: Sage Weil Date: Sat, 18 Feb 2017 21:15:33 +0000 (-0500) Subject: common/RefCountedObject: allow refs to const objects X-Git-Tag: v12.0.1~279^2~23 X-Git-Url: http://git-server-git.apps.pok.os.sepia.ceph.com/?a=commitdiff_plain;h=71dc3cfedbc88020c1aee69163d9d939598b30f9;p=ceph.git common/RefCountedObject: allow refs to const objects Make nref mutable, and make a const and non-const get() variant. Signed-off-by: Sage Weil --- diff --git a/src/common/RefCountedObj.cc b/src/common/RefCountedObj.cc index 236a2b11ffd..058a18b3399 100644 --- a/src/common/RefCountedObj.cc +++ b/src/common/RefCountedObj.cc @@ -14,10 +14,10 @@ #include "common/RefCountedObj.h" -void intrusive_ptr_add_ref(RefCountedObject *p) { +void intrusive_ptr_add_ref(const RefCountedObject *p) { p->get(); } -void intrusive_ptr_release(RefCountedObject *p) { +void intrusive_ptr_release(const RefCountedObject *p) { p->put(); } diff --git a/src/common/RefCountedObj.h b/src/common/RefCountedObj.h index cd886f66f37..2da83d1a760 100644 --- a/src/common/RefCountedObj.h +++ b/src/common/RefCountedObj.h @@ -23,7 +23,7 @@ struct RefCountedObject { private: - atomic_t nref; + mutable atomic_t nref; CephContext *cct; public: RefCountedObject(CephContext *c = NULL, int n=1) : nref(n), cct(c) {} @@ -31,6 +31,14 @@ public: assert(nref.read() == 0); } + const RefCountedObject *get() const { + int v = nref.inc(); + if (cct) + lsubdout(cct, refs, 1) << "RefCountedObject::get " << this << " " + << (v - 1) << " -> " << v + << dendl; + return this; + } RefCountedObject *get() { int v = nref.inc(); if (cct) @@ -39,7 +47,7 @@ public: << dendl; return this; } - void put() { + void put() const { CephContext *local_cct = cct; int v = nref.dec(); if (v == 0) { @@ -151,7 +159,7 @@ struct RefCountedWaitObject { } }; -void intrusive_ptr_add_ref(RefCountedObject *p); -void intrusive_ptr_release(RefCountedObject *p); +void intrusive_ptr_add_ref(const RefCountedObject *p); +void intrusive_ptr_release(const RefCountedObject *p); #endif