Skip to content

Commit

Permalink
librbd: helper class for quiescing in-flight async ops
Browse files Browse the repository at this point in the history
Signed-off-by: Jason Dillaman <dillaman@redhat.com>
  • Loading branch information
Jason Dillaman committed Sep 15, 2016
1 parent abe508e commit 84dec47
Showing 1 changed file with 36 additions and 0 deletions.
36 changes: 36 additions & 0 deletions src/librbd/Utils.h
Expand Up @@ -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

Expand Down

0 comments on commit 84dec47

Please sign in to comment.