Skip to content

Commit b77238a

Browse files
authored
♻️ Add guard to use pointers, add signal-based I/O, and rename listener API (#92)
This commit makes three major changes: 1. **guard class**: Converted from references to pointers with proper move semantics—moved-from objects null out both pointers. 2. **I/O state**: Renamed `blocked_by::io` and `blocked_by::external` to `blocked_by::signal` to clarify that any external events trigger unblocking. 3. **Listener API**: Renamed `unblock_listener` to `context_listener` and `on_unblock()` to `set_listener()`, plus added `on_sync_block()` callback for sync contention tracking. The changes improve semantic clarity around blocking states and make the guard type safely movable with proper cleanup semantics. Co-authored-by: Malia Labor (@MaliaLabor)
1 parent 37ac9ca commit b77238a

16 files changed

Lines changed: 643 additions & 633 deletions

CMakeLists.txt

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,9 @@ libhal_add_library(async_context
2525
MODULES
2626
modules/async_context.cppm
2727
modules/coroutine.cppm
28-
modules/schedulers.cppm)
28+
modules/schedulers.cppm
29+
modules/sync.cppm
30+
)
2931
libhal_apply_compile_options(async_context)
3032
libhal_install_library(async_context NAMESPACE libhal)
3133
libhal_add_tests(async_context
@@ -34,9 +36,9 @@ libhal_add_tests(async_context
3436
basics
3537
blocked_by
3638
cancel
37-
exclusive_access
39+
mutex
3840
proxy
39-
on_unblock
41+
context_listener
4042
simple_scheduler
4143
clock_adapter
4244
run_until_done

README.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ using namespace std::chrono_literals;
3434
async::future<int> read_sensor(async::context& ctx, std::string_view p_name)
3535
{
3636
std::println("['{}': Sensor] Starting read...", p_name);
37-
co_await ctx.block_by_io(); // Simulate I/O operation
37+
co_await ctx.block_by_signal(); // Simulate I/O operation
3838
std::println("['{}': Sensor] Read complete: 42", p_name);
3939
co_return 42;
4040
}
@@ -57,7 +57,7 @@ async::future<void> write_actuator(async::context& ctx,
5757
int value)
5858
{
5959
std::println("['{}': Actuator] Writing {}...", p_name, value);
60-
co_await ctx.block_by_io();
60+
co_await ctx.block_by_signal();
6161
std::println("['{}': Actuator] Write complete!", p_name);
6262
}
6363

@@ -84,10 +84,10 @@ async::future<void> io_unblock_driver(async::context& p_ctx,
8484
if (ctx1.done() && ctx2.done()) {
8585
co_return;
8686
}
87-
if (ctx1.state() == async::blocked_by::io) {
87+
if (ctx1.state() == async::blocked_by::signal) {
8888
ctx1.unblock();
8989
}
90-
if (ctx2.state() == async::blocked_by::io) {
90+
if (ctx2.state() == async::blocked_by::signal) {
9191
ctx2.unblock();
9292
}
9393
co_await 1us;
@@ -410,7 +410,7 @@ async::future<void> io_example(async::context& p_ctx) {
410410
// Start DMA transaction...
411411

412412
while (!dma_complete) {
413-
co_await p_ctx.block_by_io();
413+
co_await p_ctx.block_by_signal();
414414
}
415415
co_return;
416416
}
@@ -419,7 +419,7 @@ async::future<void> io_example(async::context& p_ctx) {
419419
Please note that this coroutine has a loop where it continually reports that
420420
its blocked by IO. It is important that any coroutine blocking by IO check if
421421
the IO has completed before proceeding. If not, it must
422-
`co_await ctx.block_by_io();` at some point to give control back to the resumer.
422+
`co_await ctx.block_by_signal();` at some point to give control back to the resumer.
423423
424424
### Composing Coroutines
425425

docs/api/async_context.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,29 +27,29 @@ Defined in namespace `async::v0`
2727
```{doxygenclass} v0::proxy_context
2828
```
2929

30-
## async::exclusive_access
30+
## async::blocked_by
3131

3232
Defined in namespace `async::v0`
3333

3434
*import async_context;*
3535

36-
```{doxygenclass} v0::exclusive_access
36+
```{doxygenenum} v0::blocked_by
3737
```
3838

39-
## async::blocked_by
39+
## async::future\<T\>
4040

4141
Defined in namespace `async::v0`
4242

4343
*import async_context;*
4444

45-
```{doxygenenum} v0::blocked_by
45+
```{doxygenclass} v0::future
4646
```
4747

48-
## async::future\<T\>
48+
## async::mutex
4949

5050
Defined in namespace `async::v0`
5151

5252
*import async_context;*
5353

54-
```{doxygenclass} v0::future
54+
```{doxygenclass} v0::mutex
5555
```

modules/async_context.cppm

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,4 @@ export module async_context;
1616

1717
export import :coroutine;
1818
export import :schedulers;
19+
export import :sync;

0 commit comments

Comments
 (0)