]> git.apps.os.sepia.ceph.com Git - ceph.git/commitdiff
librbd: helper class for quiescing in-flight async ops
authorJason Dillaman <dillaman@redhat.com>
Tue, 13 Sep 2016 20:38:51 +0000 (16:38 -0400)
committerJason Dillaman <dillaman@redhat.com>
Fri, 23 Sep 2016 13:02:41 +0000 (09:02 -0400)
Signed-off-by: Jason Dillaman <dillaman@redhat.com>
src/librbd/Utils.h

index 6bd4320073b9f240d55966037d03525a080cf1ef..b098881d6f5a988d81b69c3e2154b9b8da8da88d 100644 (file)
@@ -161,6 +161,42 @@ inline ImageCtx *get_image_ctx(ImageCtx *image_ctx) {
   return image_ctx;
 }
 
+/// helper for tracking in-flight async ops when coordinating
+/// a shut down of the invoking class instance
+class AsyncOpTracker {
+public:
+  AsyncOpTracker() : m_refs(0) {
+  }
+
+  void start_op() {
+    m_refs.inc();
+  }
+
+  void finish_op() {
+    if (m_refs.dec() == 0 && m_on_finish != nullptr) {
+      Context *on_finish = nullptr;
+      std::swap(on_finish, m_on_finish);
+      on_finish->complete(0);
+    }
+  }
+
+  template <typename I>
+  void wait(I &image_ctx, Context *on_finish) {
+    assert(m_on_finish == nullptr);
+
+    on_finish = create_async_context_callback(image_ctx, on_finish);
+    if (m_refs.read() == 0) {
+      on_finish->complete(0);
+      return;
+    }
+    m_on_finish = on_finish;
+  }
+
+private:
+  atomic_t m_refs;
+  Context *m_on_finish = nullptr;
+};
+
 } // namespace util
 } // namespace librbd