Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 3 additions & 9 deletions MODULE.bazel
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
module(
name = "cpp_toolbelt",
version = "2.1.7",
version = "2.2.0",
)

bazel_dep(name = "platforms", version = "1.0.0")
bazel_dep(name = "bazel_skylib", version = "1.9.0")
bazel_dep(name = "abseil-cpp", version = "20250814.1")
bazel_dep(name = "googletest", version = "1.17.0.bcr.2")
bazel_dep(name = "coroutines", version = "3.4.1")
bazel_dep(name = "coroutines", version = "3.5.0")
bazel_dep(name = "rules_cc", version = "0.2.17")

# For local debugging of co coroutine library.
Expand All @@ -17,12 +17,6 @@ bazel_dep(name = "rules_cc", version = "0.2.17")
# )
git_override(
module_name = "coroutines",
commit = "f4bcdffa67315d9aefaed6a802731263c0e0c19b",
commit = "c9734b087b231a932ff21def81ad78540a43eff8", # 3.5.0
remote = "https://github.com/dallison/co.git",
# GCC 15 still emits TSan function entry/exit hooks around a stack switch,
# which corrupts the shadow stack (Ubuntu 26.04 CI). This is the fix from
# dallison/co c2cbd0f, applied onto the pinned 3.4.1 commit.
patches = ["//third_party:coroutines_tsan.patch"],
patch_strip = 1,
)

20 changes: 20 additions & 0 deletions MODULE.bazel.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

29 changes: 28 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,8 @@ if (result.ok()) {

The socket classes provide a high-level interface to various socket types. They are coroutine-aware and work with the [co coroutine library](https://github.com/dallison/co).

Pass a `co::Coroutine*` (as `toolbelt::AsyncWaiter*`) to `Receive` / `Send` / `Accept` / etc. for stackful C++17 coroutines. For C++20 stackless coroutines (`co20::`), use the awaitable helpers in `toolbelt/co20.h` instead — see [C++20 coroutines](#c20-coroutines) below.

**Header:** `toolbelt/sockets.h`

#### InetAddress
Expand Down Expand Up @@ -1038,10 +1040,35 @@ void some_function() {
}
```

### C++20 coroutines

A C++20 stackless `co20::Coroutine` can only suspend at `co_await`. Calling the
`AsyncWaiter`-based APIs through a `co20::Coroutine*` therefore blocks the
scheduler thread instead of suspending. Use the header-only helpers in
`toolbelt/co20.h` (Bazel target `//toolbelt:toolbelt_co20`):

```cpp
#include "toolbelt/co20.h"
#include "co/coroutine_cpp20.h"

co20::Scheduler sched;
sched.Spawn([&]() -> co20::Task {
auto n = co_await toolbelt::await::Receive(socket, buffer, sizeof(buffer));
// ...
co_return;
});
sched.Run();
```

Available as `toolbelt::await::*`: `Read` / `Write` (file descriptors and pipes),
`Receive` / `Send` / `ReceiveMessage` / `SendMessage` / `Accept` (sockets),
`SendTo` / `ReceiveFrom` (UDP), `SendFds` / `ReceiveFds` (Unix), and
`SharedPtrPipe` `Read` / `Write`.

## Dependencies

- [Abseil](https://github.com/abseil/abseil-cpp) - Status types, strings, formatting
- [co](https://github.com/dallison/co) - Coroutine library (for socket classes)
- [co](https://github.com/dallison/co) - Coroutine library (C++17 `co::` and C++20 `co20::`)

## License

Expand Down
41 changes: 41 additions & 0 deletions toolbelt/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,29 @@ cc_library(
],
)

# Header-only C++20 awaitable wrappers around FileDescriptor / Pipe / Socket
# I/O. Kept separate from :toolbelt so the core library stays C++17 and so
# @coroutines//co:co_cpp20 (which depends on :toolbelt) does not form a cycle.
cc_library(
name = "toolbelt_co20",
hdrs = [
"co20.h",
"fd_co20.h",
"pipe_co20.h",
"sockets_co20.h",
],
copts = [
"-std=c++20",
],
deps = [
":toolbelt",
"@abseil-cpp//absl/status",
"@abseil-cpp//absl/status:statusor",
"@abseil-cpp//absl/strings:str_format",
"@coroutines//co:co_cpp20",
],
)

cc_test(
name = "fd_test",
size = "small",
Expand Down Expand Up @@ -171,3 +194,21 @@ cc_test(
"@googletest//:gtest",
],
)

cc_test(
name = "co20_test",
size = "small",
srcs = ["co20_test.cc"],
copts = [
"-std=c++20",
],
tags = ["cpp20"],
deps = [
":toolbelt_co20",
"@abseil-cpp//absl/status",
"@abseil-cpp//absl/status:status_matchers",
"@abseil-cpp//absl/status:statusor",
"@coroutines//co:co_cpp20",
"@googletest//:gtest_main",
],
)
7 changes: 6 additions & 1 deletion toolbelt/async_waiter.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,13 @@ struct WaitFd {
uint32_t events;
};

// This is a AsyncWaiter. It's an abstract interface to a poll/wait/yield
// This is a AsyncWaiter. It's an abstract interface to a poll/wait/yield
// mechanism, such as a vanilla POSIX implementation or coroutines.
//
// Stackful co::Coroutine implements this and can suspend inside Wait/Yield.
// Stackless C++20 coroutines can only suspend at co_await, so do not pass a
// co20::Coroutine through these virtuals expecting a yield — use
// toolbelt/co20.h (toolbelt::await::*) with co_await instead.
class AsyncWaiter {
public:
AsyncWaiter() = default;
Expand Down
30 changes: 30 additions & 0 deletions toolbelt/co20.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// Copyright 2023-2026 David Allison
// All Rights Reserved
// See LICENSE file for licensing information.

#pragma once

// Umbrella header for C++20 awaitable toolbelt I/O.
//
// Stackful co::Coroutine can suspend inside toolbelt::AsyncWaiter::Wait /
// Yield, so the existing APIs that take an AsyncWaiter* work with it. C++20
// stackless co20::Coroutine can only suspend at co_await; calling those same
// APIs through an AsyncWaiter* blocks the scheduler thread instead.
//
// Include this header (or the individual *_co20.h headers) and use the
// toolbelt::await::* helpers with co_await:
//
// #include "toolbelt/co20.h"
//
// sched.Spawn([&]() -> ::co20::Task {
// auto n = co_await toolbelt::await::Receive(socket, buf, sizeof(buf));
// ...
// co_return;
// });
//
// Requires -std=c++20 and a dependency on @coroutines//:co_cpp20
// (Bazel target //toolbelt:toolbelt_co20, depends on @coroutines//co:co_cpp20).

#include "toolbelt/fd_co20.h"
#include "toolbelt/pipe_co20.h"
#include "toolbelt/sockets_co20.h"
Loading
Loading