summaryrefslogtreecommitdiffstats
path: root/src/librados/PoolAsyncCompletionImpl.h
blob: 9a3b1be4bdc072532af323270f31b7c1a35be5eb (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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
// vim: ts=8 sw=2 smarttab
/*
 * Ceph - scalable distributed file system
 *
 * Copyright (C) 2004-2012 Sage Weil <sage@newdream.net>
 *
 * This is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License version 2.1, as published by the Free Software
 * Foundation.  See file COPYING.
 *
 */

#ifndef CEPH_LIBRADOS_POOLASYNCCOMPLETIONIMPL_H
#define CEPH_LIBRADOS_POOLASYNCCOMPLETIONIMPL_H

#include "common/Cond.h"
#include "common/Mutex.h"
#include "include/Context.h"
#include "include/rados/librados.h"
#include "include/rados/librados.hpp"

namespace librados {
  struct PoolAsyncCompletionImpl {
    Mutex lock;
    Cond cond;
    int ref, rval;
    bool released;
    bool done;

    rados_callback_t callback;
    void *callback_arg;

    PoolAsyncCompletionImpl() : lock("PoolAsyncCompletionImpl lock"),
				ref(1), rval(0), released(false), done(false),
				callback(0), callback_arg(0) {}

    int set_callback(void *cb_arg, rados_callback_t cb) {
      lock.Lock();
      callback = cb;
      callback_arg = cb_arg;
      lock.Unlock();
      return 0;
    }
    int wait() {
      lock.Lock();
      while (!done)
	cond.Wait(lock);
      lock.Unlock();
      return 0;
    }
    int is_complete() {
      lock.Lock();
      int r = done;
      lock.Unlock();
      return r;
    }
    int get_return_value() {
      lock.Lock();
      int r = rval;
      lock.Unlock();
      return r;
    }
    void get() {
      lock.Lock();
      ceph_assert(ref > 0);
      ref++;
      lock.Unlock();
    }
    void release() {
      lock.Lock();
      ceph_assert(!released);
      released = true;
      put_unlock();
    }
    void put() {
      lock.Lock();
      put_unlock();
    }
    void put_unlock() {
      ceph_assert(ref > 0);
      int n = --ref;
      lock.Unlock();
      if (!n)
	delete this;
    }
  };

  class C_PoolAsync_Safe : public Context {
    PoolAsyncCompletionImpl *c;

  public:
    explicit C_PoolAsync_Safe(PoolAsyncCompletionImpl *_c) : c(_c) {
      c->get();
    }
    ~C_PoolAsync_Safe() override {
      c->put();
    }
  
    void finish(int r) override {
      c->lock.Lock();
      c->rval = r;
      c->done = true;
      c->cond.Signal();

      if (c->callback) {
	rados_callback_t cb = c->callback;
	void *cb_arg = c->callback_arg;
	c->lock.Unlock();
	cb(c, cb_arg);
	c->lock.Lock();
      }

      c->lock.Unlock();
    }
  };
}
#endif