summaryrefslogtreecommitdiffstats
path: root/src/common/ContextCompletion.cc
blob: a4f8168343c81a17f9866af45e93db91707ff757 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
// vim: ts=8 sw=2 smarttab
#include "common/ContextCompletion.h"

namespace ceph
{

ContextCompletion::ContextCompletion(Context *ctx, bool ignore_enoent)
  : m_ctx(ctx),
    m_ignore_enoent(ignore_enoent), m_ret(0), m_building(true), m_current_ops(0)
{
}

void ContextCompletion::finish_adding_requests() {
  bool complete;
  {
    std::lock_guard l(m_lock);
    m_building = false;
    complete = (m_current_ops == 0);
  }
  if (complete) {
    m_ctx->complete(m_ret);
    delete this;
  }
}

void ContextCompletion::start_op() {
  std::lock_guard l(m_lock);
  ++m_current_ops;
}

void ContextCompletion::finish_op(int r) {
  bool complete;
  {
    std::lock_guard l(m_lock);
    if (r < 0 && m_ret == 0 && (!m_ignore_enoent || r != -ENOENT)) {
      m_ret = r;
    }

    --m_current_ops;
    complete = (m_current_ops == 0 && !m_building);
  }
  if (complete) {
    m_ctx->complete(m_ret);
    delete this;
  }
}

} // namespace ceph