From: Dan Mick Date: Mon, 9 Jul 2012 21:11:23 +0000 (-0700) Subject: librados: add new constructor to form a Rados object from IoCtx X-Git-Tag: v0.50~96^2~8 X-Git-Url: http://git-server-git.apps.pok.os.sepia.ceph.com/?a=commitdiff_plain;h=95f85df7ca658cf3ee585da70e244ef6fe5aa5f7;p=ceph.git librados: add new constructor to form a Rados object from IoCtx This creates a separate reference to an existing connection, for use when a client holding IoCtx needs to consult another (say, for rbd cloning) Signed-off-by: Dan Mick Reviewed-by: Josh Durgin --- diff --git a/src/include/rados/librados.hpp b/src/include/rados/librados.hpp index 64101481b63c..7b6e68aa70ea 100644 --- a/src/include/rados/librados.hpp +++ b/src/include/rados/librados.hpp @@ -475,6 +475,7 @@ namespace librados static void version(int *major, int *minor, int *extra); Rados(); + explicit Rados(IoCtx& ioctx); ~Rados(); int init(const char * const id); diff --git a/src/librados/RadosClient.cc b/src/librados/RadosClient.cc index 0a08a3b82628..0f9fc8c47fb1 100644 --- a/src/librados/RadosClient.cc +++ b/src/librados/RadosClient.cc @@ -65,6 +65,7 @@ librados::RadosClient::RadosClient(CephContext *cct_) objecter(NULL), lock("radosclient"), timer(cct, lock), + refcnt(1), finisher(cct), max_watch_cookie(0) { @@ -367,6 +368,19 @@ int librados::RadosClient::get_fs_stats(ceph_statfs& stats) return 0; } +void librados::RadosClient::get() { + Mutex::Locker l(lock); + assert(refcnt > 0); + refcnt++; +} + +bool librados::RadosClient::put() { + Mutex::Locker l(lock); + assert(refcnt > 0); + refcnt--; + return (refcnt == 0); +} + int librados::RadosClient::pool_create(string& name, unsigned long long auid, __u8 crush_rule) { diff --git a/src/librados/RadosClient.h b/src/librados/RadosClient.h index 3439815dc750..3ca6153df5b8 100644 --- a/src/librados/RadosClient.h +++ b/src/librados/RadosClient.h @@ -62,6 +62,7 @@ private: Mutex lock; Cond cond; SafeTimer timer; + int refcnt; public: Finisher finisher; @@ -99,6 +100,8 @@ public: librados::WatchCtx *ctx, uint64_t *cookie); void unregister_watcher(uint64_t cookie); void watch_notify(MWatchNotify *m); + void get(); + bool put(); }; #endif diff --git a/src/librados/librados.cc b/src/librados/librados.cc index 9fc2f4f2e5c1..452120f9c380 100644 --- a/src/librados/librados.cc +++ b/src/librados/librados.cc @@ -1024,6 +1024,13 @@ librados::Rados::Rados() : client(NULL) { } +librados::Rados::Rados(IoCtx &ioctx) +{ + client = ioctx.io_ctx_impl->client; + assert(client != NULL); + client->get(); +} + librados::Rados::~Rados() { shutdown(); @@ -1053,9 +1060,11 @@ void librados::Rados::shutdown() { if (!client) return; - client->shutdown(); - delete client; - client = NULL; + if (client->put()) { + client->shutdown(); + delete client; + client = NULL; + } } uint64_t librados::Rados::get_instance_id()