Skip to content

Commit bbbbd71

Browse files
committed
Add ExecutionContext concept and consoldiate test contexts
1 parent 0a371bf commit bbbbd71

12 files changed

Lines changed: 208 additions & 86 deletions

File tree

bench/bench.cpp

Lines changed: 30 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77
// Official repository: https://github.com/cppalliance/capy
88
//
99

10+
#include <boost/capy/concept/execution_context.hpp>
1011
#include <boost/capy/ex/run_async.hpp>
11-
#include <boost/capy/ex/execution_context.hpp>
1212
#include <boost/capy/ex/run.hpp>
1313
#include <boost/capy/ex/strand.hpp>
1414
#include <boost/capy/task.hpp>
@@ -21,48 +21,53 @@ using namespace boost::capy;
2121

2222
//-----------------------------------------------
2323
//
24-
// Test Execution Context
24+
// Bench Execution Context
2525
//
2626
// A minimal execution context for benchmarking.
2727
//
2828
//-----------------------------------------------
2929

30-
class test_context : public execution_context
30+
class bench_io_context : public execution_context
3131
{
3232
public:
33-
~test_context()
33+
class executor_type;
34+
35+
~bench_io_context()
3436
{
3537
shutdown();
3638
destroy();
3739
}
40+
41+
executor_type
42+
get_executor() noexcept;
3843
};
3944

4045
//-----------------------------------------------
4146
//
42-
// Test Executor
47+
// Bench Executor
4348
//
4449
// A minimal executor that satisfies the capy
4550
// Executor concept. Dispatches inline for
4651
// benchmarking pure coroutine overhead.
4752
//
4853
//-----------------------------------------------
4954

50-
class test_executor
55+
class bench_io_context::executor_type
5156
{
52-
test_context* ctx_;
57+
bench_io_context* ctx_;
5358

5459
public:
55-
explicit test_executor(test_context& ctx) noexcept
60+
explicit executor_type(bench_io_context& ctx) noexcept
5661
: ctx_(&ctx)
5762
{
5863
}
5964

60-
test_context& context() const noexcept
65+
bench_io_context& context() const noexcept
6166
{
6267
return *ctx_;
6368
}
6469

65-
bool operator==(test_executor const& other) const noexcept
70+
bool operator==(executor_type const& other) const noexcept
6671
{
6772
return ctx_ == other.ctx_;
6873
}
@@ -93,6 +98,15 @@ class test_executor
9398
}
9499
};
95100

101+
inline bench_io_context::executor_type
102+
bench_io_context::get_executor() noexcept
103+
{
104+
return executor_type(*this);
105+
}
106+
107+
static_assert(Executor<bench_io_context::executor_type>);
108+
static_assert(ExecutionContext<bench_io_context>);
109+
96110
//-----------------------------------------------
97111
//
98112
// Foreign Awaitable
@@ -251,14 +265,14 @@ int main()
251265
{
252266
constexpr std::size_t iterations = 1000000;
253267

254-
test_context ctx1;
255-
test_context ctx2;
268+
bench_io_context ctx1;
269+
bench_io_context ctx2;
256270

257-
test_executor ex1(ctx1);
258-
test_executor ex2(ctx2);
271+
auto ex1 = ctx1.get_executor();
272+
auto ex2 = ctx2.get_executor();
259273

260-
strand<test_executor> strand1(ex1);
261-
strand<test_executor> strand2(ex2);
274+
strand<bench_io_context::executor_type> strand1(ex1);
275+
strand<bench_io_context::executor_type> strand2(ex2);
262276

263277
foreign_awaitable foreign;
264278

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
//
2+
// Copyright (c) 2025 Vinnie Falco (vinnie dot falco at gmail dot com)
3+
//
4+
// Distributed under the Boost Software License, Version 1.0. (See accompanying
5+
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
6+
//
7+
// Official repository: https://github.com/cppalliance/capy
8+
//
9+
10+
#ifndef BOOST_CAPY_CONCEPT_EXECUTION_CONTEXT_HPP
11+
#define BOOST_CAPY_CONCEPT_EXECUTION_CONTEXT_HPP
12+
13+
#include <boost/capy/detail/config.hpp>
14+
#include <boost/capy/concept/executor.hpp>
15+
#include <boost/capy/ex/execution_context.hpp>
16+
17+
#include <concepts>
18+
19+
namespace boost {
20+
namespace capy {
21+
22+
/** Concept for execution context types.
23+
24+
An execution context represents a place where function objects
25+
are executed. A type meeting the ExecutionContext requirements
26+
must be publicly derived from execution_context and provide
27+
an associated executor type.
28+
29+
@par Required Operations
30+
31+
@li `X::executor_type` - A type meeting the Executor requirements.
32+
33+
@li `x.get_executor()` - Returns an executor object associated
34+
with the execution context.
35+
36+
@par Destructor Semantics
37+
38+
The destructor destroys all unexecuted function objects that
39+
were submitted via an executor associated with this context.
40+
41+
@tparam X The type to check for execution context conformance.
42+
*/
43+
template<class X>
44+
concept ExecutionContext =
45+
std::derived_from<X, execution_context> &&
46+
requires(X& x) {
47+
typename X::executor_type;
48+
requires Executor<typename X::executor_type>;
49+
{ x.get_executor() } noexcept -> std::same_as<typename X::executor_type>;
50+
};
51+
52+
} // capy
53+
} // boost
54+
55+
#endif

include/boost/capy/test/run_blocking.hpp

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@
1111
#define BOOST_CAPY_TEST_RUN_BLOCKING_HPP
1212

1313
#include <boost/capy/coro.hpp>
14+
#include <boost/capy/concept/execution_context.hpp>
1415
#include <boost/capy/concept/executor.hpp>
15-
#include <boost/capy/ex/execution_context.hpp>
1616
#include <boost/capy/ex/run_async.hpp>
1717
#include <boost/capy/ex/system_context.hpp>
1818

@@ -28,6 +28,28 @@ namespace boost {
2828
namespace capy {
2929
namespace test {
3030

31+
struct inline_executor;
32+
33+
/** Execution context for inline blocking execution.
34+
35+
This execution context is used with inline_executor for
36+
blocking synchronous execution. It satisfies the
37+
ExecutionContext concept requirements.
38+
39+
@see inline_executor
40+
@see run_blocking
41+
*/
42+
class inline_context : public execution_context
43+
{
44+
public:
45+
using executor_type = inline_executor;
46+
47+
inline_context() = default;
48+
49+
executor_type
50+
get_executor() noexcept;
51+
};
52+
3153
/** Synchronous executor that executes inline and disallows posting.
3254
3355
This executor executes work synchronously on the calling thread
@@ -53,10 +75,9 @@ struct inline_executor
5375
5476
@return A reference to a function-local static `inline_context`.
5577
*/
56-
execution_context&
78+
inline_context&
5779
context() const noexcept
5880
{
59-
struct inline_context : public execution_context {};
6081
static inline_context ctx;
6182
return ctx;
6283
}
@@ -97,7 +118,14 @@ struct inline_executor
97118
}
98119
};
99120

121+
inline inline_context::executor_type
122+
inline_context::get_executor() noexcept
123+
{
124+
return inline_executor{};
125+
}
126+
100127
static_assert(Executor<inline_executor>);
128+
static_assert(ExecutionContext<inline_context>);
101129

102130
//----------------------------------------------------------
103131
//
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
//
2+
// Copyright (c) 2025 Vinnie Falco (vinnie dot falco at gmail dot com)
3+
//
4+
// Distributed under the Boost Software License, Version 1.0. (See accompanying
5+
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
6+
//
7+
// Official repository: https://github.com/cppalliance/capy
8+
//
9+
10+
// Test that header file is self-contained.
11+
#include <boost/capy/concept/execution_context.hpp>

test/unit/ex/async_event.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ namespace {
3131
struct queuing_executor
3232
{
3333
std::queue<coro>* queue_;
34-
test_context* ctx_ = nullptr;
34+
test_io_context* ctx_ = nullptr;
3535

3636
explicit queuing_executor(std::queue<coro>& q)
3737
: queue_(&q)
@@ -45,7 +45,7 @@ struct queuing_executor
4545

4646
execution_context& context() const noexcept
4747
{
48-
return ctx_ ? *ctx_ : default_test_context();
48+
return ctx_ ? *ctx_ : default_test_io_context();
4949
}
5050

5151
void on_work_started() const noexcept {}

0 commit comments

Comments
 (0)