]>
git.proxmox.com Git - ceph.git/blob - ceph/src/include/Context.h
bf992d0a2bf9bcfcfbaaedc2b14eae2411a1c93c
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) 2004-2006 Sage Weil <sage@newdream.net>
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.
16 #ifndef CEPH_CONTEXT_H
17 #define CEPH_CONTEXT_H
19 #include "common/dout.h"
21 #include <boost/function.hpp>
26 #include "include/assert.h"
27 #include "include/memory.h"
29 #define mydout(cct, v) lgeneric_subdout(cct, context, v)
32 * GenContext - abstract callback class
36 GenContext(const GenContext
& other
);
37 const GenContext
& operator=(const GenContext
& other
);
40 virtual void finish(T t
) = 0;
44 virtual ~GenContext() {} // we want a virtual destructor!!!
47 void complete(C
&&t
) {
48 finish(std::forward
<C
>(t
));
54 using GenContextURef
= std::unique_ptr
<GenContext
<T
> >;
57 * Context - abstract callback class
60 Context(const Context
& other
);
61 const Context
& operator=(const Context
& other
);
64 virtual void finish(int r
) = 0;
68 virtual ~Context() {} // we want a virtual destructor!!!
69 virtual void complete(int r
) {
76 * Simple context holding a single object
79 class ContainerContext
: public Context
{
82 ContainerContext(T
&obj
) : obj(obj
) {}
83 void finish(int r
) override
{}
86 ContainerContext
<T
> *make_container_context(T
&&t
) {
87 return new ContainerContext
<T
>(std::forward
<T
>(t
));
91 struct Wrapper
: public Context
{
94 Wrapper(Context
*to_run
, T val
) : to_run(to_run
), val(val
) {}
95 void finish(int r
) override
{
102 RunOnDelete(Context
*to_run
) : to_run(to_run
) {}
108 typedef ceph::shared_ptr
<RunOnDelete
> RunOnDeleteRef
;
110 template <typename T
>
111 struct LambdaContext
: public Context
{
113 LambdaContext(T
&&t
) : t(std::forward
<T
>(t
)) {}
114 void finish(int) override
{
118 template <typename T
>
119 LambdaContext
<T
> *make_lambda_context(T
&&t
) {
120 return new LambdaContext
<T
>(std::move(t
));
123 template <typename F
, typename T
>
124 struct LambdaGenContext
: GenContext
<T
> {
126 LambdaGenContext(F
&&f
) : f(std::forward
<F
>(f
)) {}
127 void finish(T t
) override
{
128 f(std::forward
<T
>(t
));
131 template <typename T
, typename F
>
132 GenContextURef
<T
> make_gen_lambda_context(F
&&f
) {
133 return GenContextURef
<T
>(new LambdaGenContext
<F
, T
>(std::move(f
)));
137 * finish and destroy a list of Contexts
140 inline void finish_contexts(CephContext
*cct
, std::list
<A
*>& finished
,
143 if (finished
.empty())
147 ls
.swap(finished
); // swap out of place to avoid weird loops
150 mydout(cct
, 10) << ls
.size() << " contexts to finish with " << result
<< dendl
;
151 typename
std::list
<A
*>::iterator it
;
152 for (it
= ls
.begin(); it
!= ls
.end(); it
++) {
155 mydout(cct
,10) << "---- " << c
<< dendl
;
160 inline void finish_contexts(CephContext
*cct
, std::vector
<Context
*>& finished
,
163 if (finished
.empty())
167 ls
.swap(finished
); // swap out of place to avoid weird loops
170 mydout(cct
,10) << ls
.size() << " contexts to finish with " << result
<< dendl
;
171 for (std::vector
<Context
*>::iterator it
= ls
.begin();
176 mydout(cct
,10) << "---- " << c
<< dendl
;
181 class C_NoopContext
: public Context
{
183 void finish(int r
) override
{ }
187 struct C_Lock
: public Context
{
190 C_Lock(Mutex
*l
, Context
*c
) : lock(l
), fin(c
) {}
194 void finish(int r
) override
{
205 * C_Contexts - set of Contexts
207 * ContextType must be an ancestor class of ContextInstanceType, or the same class.
208 * ContextInstanceType must be default-constructable.
210 template <class ContextType
, class ContextInstanceType
>
211 class C_ContextsBase
: public ContextInstanceType
{
214 std::list
<ContextType
*> contexts
;
216 C_ContextsBase(CephContext
*cct_
)
221 void add(ContextType
* c
) {
222 contexts
.push_back(c
);
224 void take(std::list
<ContextType
*>& ls
) {
225 contexts
.splice(contexts
.end(), ls
);
227 void complete(int r
) override
{
228 // Neuter any ContextInstanceType custom complete(), because although
229 // I want to look like it, I don't actually want to run its code.
230 Context::complete(r
);
232 void finish(int r
) override
{
233 finish_contexts(cct
, contexts
, r
);
235 bool empty() { return contexts
.empty(); }
237 static ContextType
*list_to_context(list
<ContextType
*> &cs
) {
238 if (cs
.size() == 0) {
240 } else if (cs
.size() == 1) {
241 ContextType
*c
= cs
.front();
245 C_ContextsBase
<ContextType
, ContextInstanceType
> *c(new C_ContextsBase
<ContextType
, ContextInstanceType
>(0));
252 typedef C_ContextsBase
<Context
, Context
> C_Contexts
;
257 * ContextType must be an ancestor class of ContextInstanceType, or the same class.
258 * ContextInstanceType must be default-constructable.
260 * BUG:? only reports error from last sub to have an error return
262 template <class ContextType
, class ContextInstanceType
>
263 class C_GatherBase
: public ContextType
{
267 ContextType
*onfinish
;
269 std::set
<ContextType
*> waitfor
;
271 int sub_created_count
;
272 int sub_existing_count
;
276 void sub_finish(ContextType
* sub
, int r
) {
279 assert(waitfor
.count(sub
));
282 --sub_existing_count
;
283 mydout(cct
,10) << "C_GatherBase " << this << ".sub_finish(r=" << r
<< ") " << sub
285 << " (remaining " << waitfor
<< ")"
288 if (r
< 0 && result
== 0)
290 if ((activated
== false) || (sub_existing_count
!= 0)) {
300 onfinish
->complete(result
);
306 class C_GatherSub
: public ContextInstanceType
{
307 C_GatherBase
*gather
;
309 C_GatherSub(C_GatherBase
*g
) : gather(g
) {}
310 void complete(int r
) override
{
311 // Cancel any customized complete() functionality
312 // from the Context subclass we're templated for,
313 // we only want to hit that in onfinish, not at each
314 // sub finish. e.g. MDSInternalContext.
315 Context::complete(r
);
317 void finish(int r
) override
{
318 gather
->sub_finish(this, r
);
321 ~C_GatherSub() override
{
323 gather
->sub_finish(this, 0);
328 C_GatherBase(CephContext
*cct_
, ContextType
*onfinish_
)
329 : cct(cct_
), result(0), onfinish(onfinish_
),
330 sub_created_count(0), sub_existing_count(0),
331 lock("C_GatherBase::lock", true, false), //disable lockdep
334 mydout(cct
,10) << "C_GatherBase " << this << ".new" << dendl
;
336 ~C_GatherBase() override
{
337 mydout(cct
,10) << "C_GatherBase " << this << ".delete" << dendl
;
339 void set_finisher(ContextType
*onfinish_
) {
340 Mutex::Locker
l(lock
);
342 onfinish
= onfinish_
;
346 assert(activated
== false);
348 if (sub_existing_count
!= 0) {
355 ContextType
*new_sub() {
356 Mutex::Locker
l(lock
);
357 assert(activated
== false);
359 sub_existing_count
++;
360 ContextType
*s
= new C_GatherSub(this);
364 mydout(cct
,10) << "C_GatherBase " << this << ".new_sub is " << sub_created_count
<< " " << s
<< dendl
;
367 void finish(int r
) override
{
368 ceph_abort(); // nobody should ever call me.
371 inline int get_sub_existing_count() const {
372 Mutex::Locker
l(lock
);
373 return sub_existing_count
;
376 inline int get_sub_created_count() const {
377 Mutex::Locker
l(lock
);
378 return sub_created_count
;
383 * The C_GatherBuilder remembers each C_Context created by
384 * C_GatherBuilder.new_sub() in a C_Gather. When a C_Context created
385 * by new_sub() is complete(), C_Gather forgets about it. When
386 * C_GatherBuilder notices that there are no C_Context left in
387 * C_Gather, it calls complete() on the C_Context provided as the
388 * second argument of the constructor (finisher).
390 * How to use C_GatherBuilder:
392 * 1. Create a C_GatherBuilder on the stack
393 * 2. Call gather_bld.new_sub() as many times as you want to create new subs
394 * It is safe to call this 0 times, or 100, or anything in between.
395 * 3. If you didn't supply a finisher in the C_GatherBuilder constructor,
396 * set one with gather_bld.set_finisher(my_finisher)
397 * 4. Call gather_bld.activate()
401 * C_SaferCond all_done;
402 * C_GatherBuilder gb(g_ceph_context, all_done);
403 * j.submit_entry(1, first, 0, gb.new_sub()); // add a C_Context to C_Gather
404 * j.submit_entry(2, first, 0, gb.new_sub()); // add a C_Context to C_Gather
405 * gb.activate(); // consume C_Context as soon as they complete()
406 * all_done.wait(); // all_done is complete() after all new_sub() are complete()
408 * The finisher may be called at any point after step 4, including immediately
409 * from the activate() function.
410 * The finisher will never be called before activate().
412 * Note: Currently, subs must be manually freed by the caller (for some reason.)
414 template <class ContextType
, class GatherType
>
415 class C_GatherBuilderBase
418 C_GatherBuilderBase(CephContext
*cct_
)
419 : cct(cct_
), c_gather(NULL
), finisher(NULL
), activated(false)
422 C_GatherBuilderBase(CephContext
*cct_
, ContextType
*finisher_
)
423 : cct(cct_
), c_gather(NULL
), finisher(finisher_
), activated(false)
426 ~C_GatherBuilderBase() {
428 assert(activated
); // Don't forget to activate your C_Gather!
434 ContextType
*new_sub() {
436 c_gather
= new GatherType(cct
, finisher
);
438 return c_gather
->new_sub();
443 assert(finisher
!= NULL
);
445 c_gather
->activate();
447 void set_finisher(ContextType
*finisher_
) {
448 finisher
= finisher_
;
450 c_gather
->set_finisher(finisher
);
452 GatherType
*get() const {
455 bool has_subs() const {
456 return (c_gather
!= NULL
);
458 int num_subs_created() {
460 if (c_gather
== NULL
)
462 return c_gather
->get_sub_created_count();
464 int num_subs_remaining() {
466 if (c_gather
== NULL
)
468 return c_gather
->get_sub_existing_count();
473 GatherType
*c_gather
;
474 ContextType
*finisher
;
478 typedef C_GatherBase
<Context
, Context
> C_Gather
;
479 typedef C_GatherBuilderBase
<Context
, C_Gather
> C_GatherBuilder
;
481 class FunctionContext
: public Context
{
483 FunctionContext(boost::function
<void(int)> &&callback
)
484 : m_callback(std::move(callback
))
488 void finish(int r
) override
{
492 boost::function
<void(int)> m_callback
;