1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2 // vim: ts=8 sw=2 smarttab
4 * Ceph - scalable distributed file system
6 * Copyright (C) 2014 Sebastien Ponce <sebastien.ponce@cern.ch>
8 * This is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License version 2.1, as published by the Free Software
11 * Foundation. See file COPYING.
15 #ifndef CEPH_LIBRADOSSTRIPERSTRIPER_MULTIAIOCOMPLETIONIMPL_H
16 #define CEPH_LIBRADOSSTRIPERSTRIPER_MULTIAIOCOMPLETIONIMPL_H
20 #include "common/ceph_mutex.h"
21 #include "include/radosstriper/libradosstriper.hpp"
23 namespace libradosstriper
{
25 struct MultiAioCompletionImpl
{
27 ceph::mutex lock
= ceph::make_mutex("MultiAioCompletionImpl lock", false);
28 ceph::condition_variable cond
;
30 int pending_complete
, pending_safe
;
31 rados_callback_t callback_complete
, callback_safe
;
32 void *callback_complete_arg
, *callback_safe_arg
;
33 bool building
; ///< true if we are still building this completion
34 bufferlist bl
; /// only used for read case in C api of rados striper
35 std::list
<bufferlist
*> bllist
; /// keep temporary buffer lists used for destriping
37 MultiAioCompletionImpl()
39 pending_complete(0), pending_safe(0),
40 callback_complete(0), callback_safe(0),
41 callback_complete_arg(0), callback_safe_arg(0),
44 ~MultiAioCompletionImpl() {
45 // deallocate temporary buffer lists
46 for (std::list
<bufferlist
*>::iterator it
= bllist
.begin();
54 int set_complete_callback(void *cb_arg
, rados_callback_t cb
) {
55 std::scoped_lock l
{lock
};
56 callback_complete
= cb
;
57 callback_complete_arg
= cb_arg
;
60 int set_safe_callback(void *cb_arg
, rados_callback_t cb
) {
61 std::scoped_lock l
{lock
};
63 callback_safe_arg
= cb_arg
;
66 int wait_for_complete() {
67 std::unique_lock l
{lock
};
68 cond
.wait(l
, [this] { return !pending_complete
; });
72 std::unique_lock l
{lock
};
73 cond
.wait(l
, [this] { return !pending_safe
; });
77 std::scoped_lock l
{lock
};
78 return pending_complete
== 0;
81 std::scoped_lock l
{lock
};
82 return pending_safe
== 0;
84 void wait_for_complete_and_cb() {
85 std::unique_lock l
{lock
};
86 cond
.wait(l
, [this] { return !pending_complete
&& !callback_complete
; });
88 void wait_for_safe_and_cb() {
89 std::unique_lock l
{lock
};
90 cond
.wait(l
, [this] { return !pending_safe
&& !callback_safe
; });
92 bool is_complete_and_cb() {
93 std::scoped_lock l
{lock
};
94 return ((0 == pending_complete
) && !callback_complete
);
96 bool is_safe_and_cb() {
97 std::scoped_lock l
{lock
};
98 return ((0 == pending_safe
) && !callback_safe
);
100 int get_return_value() {
101 std::scoped_lock l
{lock
};
105 std::scoped_lock l
{lock
};
109 ceph_assert(ceph_mutex_is_locked(lock
));
110 ceph_assert(ref
> 0);
118 ceph_assert(ref
> 0);
125 std::scoped_lock l
{lock
};
131 void add_safe_request() {
132 std::scoped_lock l
{lock
};
137 ceph_assert(ceph_mutex_is_locked(lock
));
138 if (callback_complete
) {
139 callback_complete(this, callback_complete_arg
);
140 callback_complete
= 0;
145 ceph_assert(ceph_mutex_is_locked(lock
));
147 callback_safe(this, callback_safe_arg
);
153 void complete_request(ssize_t r
);
154 void safe_request(ssize_t r
);
155 void finish_adding_requests();
158 inline void intrusive_ptr_add_ref(MultiAioCompletionImpl
* ptr
)
163 inline void intrusive_ptr_release(MultiAioCompletionImpl
* ptr
)
169 #endif // CEPH_LIBRADOSSTRIPERSTRIPER_MULTIAIOCOMPLETIONIMPL_H