]>
git.proxmox.com Git - ceph.git/blob - ceph/src/common/async/yield_context.h
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) 2018 Red Hat, Inc
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.
17 #include <boost/range/begin.hpp>
18 #include <boost/range/end.hpp>
19 #include <boost/asio/io_context.hpp>
23 #include <spawn/spawn.hpp>
25 // use explicit executor types instead of the type-erased boost::asio::executor.
26 // coroutines wrap the default io_context executor with a strand executor
27 using yield_context
= spawn::basic_yield_context
<
28 boost::asio::executor_binder
<void(*)(),
29 boost::asio::strand
<boost::asio::io_context::executor_type
>>>;
31 /// optional-like wrapper for a spawn::yield_context and its associated
32 /// boost::asio::io_context. operations that take an optional_yield argument
33 /// will, when passed a non-empty yield context, suspend this coroutine instead
34 /// of the blocking the thread of execution
35 class optional_yield
{
36 boost::asio::io_context
*c
= nullptr;
37 yield_context
*y
= nullptr;
39 /// construct with a valid io and yield_context
40 explicit optional_yield(boost::asio::io_context
& c
,
41 yield_context
& y
) noexcept
44 /// type tag to construct an empty object
46 optional_yield(empty_t
) noexcept
{}
48 /// implicit conversion to bool, returns true if non-empty
49 operator bool() const noexcept
{ return y
; }
51 /// return a reference to the associated io_context. only valid if non-empty
52 boost::asio::io_context
& get_io_context() const noexcept
{ return *c
; }
54 /// return a reference to the yield_context. only valid if non-empty
55 yield_context
& get_yield_context() const noexcept
{ return *y
; }
58 // type tag object to construct an empty optional_yield
59 static constexpr optional_yield::empty_t null_yield
{};