]> git-server-git.apps.pok.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>
Tue, 11 Oct 2016 16:51:12 +0000 (12:51 -0400)
Signed-off-by: Jason Dillaman <dillaman@redhat.com>
(cherry picked from commit 39d9e5cc9b38de2ee9ad2faf8e04253314160811)

src/librbd/Utils.h

index 59040819e959559534aca9ca987e433f041229a6..43f097a9617840d20554445a63bf423d8e191004 100644 (file)
@@ -157,6 +157,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