Fix some bugs when testing opensds ansible
[stor4nfv.git] / src / ceph / src / common / ContextCompletion.cc
1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2 // vim: ts=8 sw=2 smarttab
3 #include "common/ContextCompletion.h"
4
5 namespace ceph
6 {
7
8 ContextCompletion::ContextCompletion(Context *ctx, bool ignore_enoent)
9   : m_lock("ceph::ContextCompletion::m_lock"), m_ctx(ctx),
10     m_ignore_enoent(ignore_enoent), m_ret(0), m_building(true), m_current_ops(0)
11 {
12 }
13
14 void ContextCompletion::finish_adding_requests() {
15   bool complete;
16   {
17     Mutex::Locker l(m_lock);
18     m_building = false;
19     complete = (m_current_ops == 0);
20   }
21   if (complete) {
22     m_ctx->complete(m_ret);
23     delete this;
24   }
25 }
26
27 void ContextCompletion::start_op() {
28   Mutex::Locker l(m_lock);
29   ++m_current_ops;
30 }
31
32 void ContextCompletion::finish_op(int r) {
33   bool complete;
34   {
35     Mutex::Locker l(m_lock);
36     if (r < 0 && m_ret == 0 && (!m_ignore_enoent || r != -ENOENT)) {
37       m_ret = r;
38     }
39
40     --m_current_ops;
41     complete = (m_current_ops == 0 && !m_building);
42   }
43   if (complete) {
44     m_ctx->complete(m_ret);
45     delete this;
46   }
47 }
48
49 } // namespace ceph