Skip to content
Open
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
7 changes: 7 additions & 0 deletions .bazelrc
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,13 @@ build:macos_x86_64 --extra_execution_platforms=//platform/host:darwin_x86_64

# For all builds, use C++17
build --cxxopt="-std=c++17"

# C++20 build: the server and the async layer use C++20 stackless coroutines
# (co20) instead of the stackful co library. The backend has to match the
# language mode, so this config sets both.
build:cpp20 --cxxopt="-std=c++20"
build:cpp20 --//:coro_backend=co20

build --java_runtime_version=remotejdk_17

# Statically link C++ deps into Rust binaries (avoids RPATH issues)
Expand Down
34 changes: 34 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,40 @@ jobs:
--test_output=errors \
${{ matrix.bazel_flags }}

# Builds and tests the whole tree with the C++20 coroutine backend (co20,
# --config=cpp20). Targets that depend on the stackful co API or on
# zero-allocation hot paths are gated off under co20 and are skipped. See
# docs/cpp20-coroutines.md.
cpp20:
name: cpp20 (${{ matrix.os }})
runs-on: ${{ matrix.os }}
timeout-minutes: 90
strategy:
fail-fast: false
matrix:
include:
- os: ubuntu-latest
bazel_flags:

steps:
- name: Checkout code
uses: actions/checkout@v6

- name: Install Bazel
uses: bazel-contrib/setup-bazel@0.18.0
with:
bazelisk-cache: true
disk-cache: ${{ github.workflow }}-${{ matrix.os }}-cpp20
repository-cache: true

- name: Build and test all targets with co20
run: |
bazelisk test \
--config=cpp20 \
--test_output=errors \
${{ matrix.bazel_flags }} \
//...

# Exercises the anonymous memfd_create shared-memory backend on Linux
# (--config=linux_memfd, i.e. SUBSPACE_SHMEM_MODE_MEMFD). This is the same
# mechanism Android always uses; running it on native Linux guards against
Expand Down
11 changes: 10 additions & 1 deletion BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,19 @@ load("@bazel_skylib//rules:common_settings.bzl", "bool_flag", "string_flag")

# Build-time selection of the coroutine/networking backend for the server (and
# the client paths it relies on). See common/async/context.h. `co` is the
# default; `asio` compiles the server against Boost.Asio.
# default; `asio` compiles the server against Boost.Asio; `co20` uses C++20
# stackless coroutines and needs -std=c++20, so select it with --config=cpp20
# (see .bazelrc), which sets both.
#
# bazelisk build --//:coro_backend=asio //server
# bazelisk build --config=cpp20 //server
string_flag(
name = "coro_backend",
build_setting_default = "co",
values = [
"co",
"asio",
"co20",
],
)

Expand All @@ -26,6 +30,11 @@ config_setting(
flag_values = {":coro_backend": "asio"},
)

config_setting(
name = "coro_backend_co20",
flag_values = {":coro_backend": "co20"},
)

# Mutual TLS for native bridge connections and the tunnel. Off by default,
# so a normal build does not depend on BoringSSL. See docs/mtls.md.
#
Expand Down
19 changes: 19 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,25 @@

## Unreleased

### C++20 Coroutine Backend (co20)
- The server and client internals can run on C++20 stackless coroutines
instead of the stackful `co` library. Build with `--config=cpp20` (Bazel) or
`-DCMAKE_CXX_STANDARD=20` (CMake, where `SUBSPACE_CORO_BACKEND=auto` selects
it). The blocking client API and the wire protocol do not change. See
`docs/cpp20-coroutines.md`.
- Every client method that can wait on the server has an `...Async` form that
returns `async::Task<T>` (for example `CreatePublisherAsync`,
`PublishMessageAsync`, `ReadMessageAsync`, `CloseAsync`). Code that runs on
the server's scheduler thread must use it, because a blocking client call
from a server coroutine deadlocks.
- The reload callback type changed to `std::function<async::Task<bool>(Channel *)>`
and `Channel::CheckReload` is awaitable. Plugins and code that install reload
callbacks must match this under co20. The default build is unchanged.
- `//client:alloc_test`, `//c_client:client_test`, `//rpc/client:client_test`,
`//rpc/server:server_test`, `//rpc/test:rpc_test` and `//shadow:shadow_test`
are skipped under `--config=cpp20`. They depend on the stackful `co` API or on
zero-allocation hot paths.

### Subscriber Queues Keep Per-Publisher Order
- A subscriber with an explicit queue size could read a message that had
overflowed its queue after newer messages from the same publisher. The
Expand Down
48 changes: 35 additions & 13 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,12 @@
cmake_minimum_required(VERSION 3.15)
project(Subspace LANGUAGES CXX ASM)

# Set C++ standard and compiler flags
set(CMAKE_CXX_STANDARD 17)
# Set C++ standard and compiler flags. Configure with
# -DCMAKE_CXX_STANDARD=20 for the C++20 coroutine backend (see
# SUBSPACE_CORO_BACKEND below).
if(NOT DEFINED CMAKE_CXX_STANDARD)
set(CMAKE_CXX_STANDARD 17)
endif()
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF) # Prefer standard C++
set(CMAKE_POSITION_INDEPENDENT_CODE ON)
Expand Down Expand Up @@ -199,11 +203,11 @@ endif()
message(STATUS "Trigger mechanism: ${SUBSPACE_TRIGGER_MECHANISM}")

# --- External Dependency: co (using FetchContent for native CMake) ---
# Same commit as the Bazel git_override in MODULE.bazel (co 3.5.2, #82).
FetchContent_Declare(
co
GIT_REPOSITORY https://github.com/dallison/co.git
# dallison/co#77 (poll event mask) plus TSan stack-switch fix (c2cbd0f).
GIT_TAG c2cbd0f75b1e834090762d584b812a485b8b8ca8
GIT_TAG db9ecf4c6a7396bcd409addfffac3be6f00c2b59
CMAKE_ARGS
-DCMAKE_OSX_ARCHITECTURES="${CMAKE_OSX_ARCHITECTURES}"
)
Expand Down Expand Up @@ -234,22 +238,40 @@ FetchContent_MakeAvailable(protobuf)
# These targets can be used directly without find_package

# --- Option: coroutine/networking backend for the server ---
# Mirrors the Bazel //:coro_backend flag. "co" (default) builds against the co
# coroutine library; "asio" builds the server against Boost.Asio. The value is
# Mirrors the Bazel //:coro_backend flag. "co" builds against the co
# coroutine library; "asio" builds the server against Boost.Asio; "co20" uses
# C++20 stackless coroutines and needs CMAKE_CXX_STANDARD 20 or later. "auto"
# (default) picks co20 for C++20 and later, otherwise co. The value is
# translated into the SUBSPACE_CORO_BACKEND macro (see common/async/context.h)
# and applied globally so every translation unit that includes the async
# headers sees a consistent backend.
set(SUBSPACE_CORO_BACKEND "co" CACHE STRING "Server coroutine backend: co or asio")
set_property(CACHE SUBSPACE_CORO_BACKEND PROPERTY STRINGS "co" "asio")
set(SUBSPACE_CORO_BACKEND "auto" CACHE STRING "Server coroutine backend: auto, co, co20 or asio")
set_property(CACHE SUBSPACE_CORO_BACKEND PROPERTY STRINGS "auto" "co" "co20" "asio")

if(SUBSPACE_CORO_BACKEND STREQUAL "auto")
if(CMAKE_CXX_STANDARD GREATER_EQUAL 20)
set(SUBSPACE_CORO_BACKEND_RESOLVED "co20")
else()
set(SUBSPACE_CORO_BACKEND_RESOLVED "co")
endif()
else()
set(SUBSPACE_CORO_BACKEND_RESOLVED "${SUBSPACE_CORO_BACKEND}")
endif()

if(SUBSPACE_CORO_BACKEND STREQUAL "asio")
if(SUBSPACE_CORO_BACKEND_RESOLVED STREQUAL "asio")
add_compile_definitions(SUBSPACE_CORO_BACKEND=2)
message(STATUS "Subspace coroutine backend: asio (Boost.Asio)")
elseif(SUBSPACE_CORO_BACKEND STREQUAL "co")
# co backend leaves SUBSPACE_CORO_BACKEND unset; context.h defaults to _CO.
elseif(SUBSPACE_CORO_BACKEND_RESOLVED STREQUAL "co20")
if(CMAKE_CXX_STANDARD LESS 20)
message(FATAL_ERROR "SUBSPACE_CORO_BACKEND=co20 needs CMAKE_CXX_STANDARD 20 or later")
endif()
add_compile_definitions(SUBSPACE_CORO_BACKEND=3)
message(STATUS "Subspace coroutine backend: co20 (C++20 coroutines)")
elseif(SUBSPACE_CORO_BACKEND_RESOLVED STREQUAL "co")
add_compile_definitions(SUBSPACE_CORO_BACKEND=1)
message(STATUS "Subspace coroutine backend: co")
else()
message(FATAL_ERROR "SUBSPACE_CORO_BACKEND must be 'co' or 'asio'")
message(FATAL_ERROR "SUBSPACE_CORO_BACKEND must be 'auto', 'co', 'co20' or 'asio'")
endif()

# --- Option: asio_rpc backend (Boost.Asio RPC) ---
Expand All @@ -269,7 +291,7 @@ if(SUBSPACE_ASIO_RPC AND CMAKE_CROSSCOMPILING)
endif()

set(SUBSPACE_NEEDS_BOOST OFF)
if(SUBSPACE_CORO_BACKEND STREQUAL "asio" OR SUBSPACE_ASIO_RPC)
if(SUBSPACE_CORO_BACKEND_RESOLVED STREQUAL "asio" OR SUBSPACE_ASIO_RPC)
set(SUBSPACE_NEEDS_BOOST ON)
endif()

Expand Down
13 changes: 6 additions & 7 deletions MODULE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,15 @@ bazel_dep(name = "rules_java", version = "9.1.0")
bazel_dep(name = "rules_pkg", version = "1.0.1")
bazel_dep(name = "rules_android_ndk", version = "0.1.5")
bazel_dep(name = "zlib", version = "1.3.1.bcr.5")
bazel_dep(name = "coroutines", version = "3.4.1")
bazel_dep(name = "coroutines", version = "3.5.2")

# The cpp20 poll fallback must resume a waiter only for events it asked for.
# A socketpair is always writable, so waking every waiter on POLLOUT hangs
# co20_rpc on macOS (dallison/co#77). c2cbd0f also keeps TSan from corrupting
# the shadow stack on a coroutine stack switch with GCC 15 (dallison/co c2cbd0f).
# Not released yet.
# The co20 backend needs the C++20 scheduler changes in co 3.5.2 (dallison/co
# #82): multi-fd Wait, the poll fallback event mask, TSan across stack
# switches, non-aggregate ValueTask promises for GCC and an atomic Stop.
# Not in the BCR yet.
git_override(
module_name = "coroutines",
commit = "c2cbd0f75b1e834090762d584b812a485b8b8ca8",
commit = "db9ecf4c6a7396bcd409addfffac3be6f00c2b59",
remote = "https://github.com/dallison/co.git",
)

Expand Down
4 changes: 2 additions & 2 deletions MODULE.bazel.lock

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

6 changes: 6 additions & 0 deletions c_client/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@ cc_library(

cc_test(
name = "client_test",
# Co-only: these use the stackful co API directly (or, for alloc_test, assert
# zero allocations on the hot path, which co20 coroutine frames violate).
target_compatible_with = select({
"//:coro_backend_co20": ["@platforms//:incompatible"],
"//conditions:default": [],
}),
size = "small",
srcs = ["client_test.cc"],
data = [
Expand Down
4 changes: 4 additions & 0 deletions c_client/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,11 @@ target_link_libraries(subspace_c_client PUBLIC
subspace_client
)

# These tests construct subspace::Server with a co::CoroutineScheduler, which the
# co20 backend doesn't provide (mirrors target_compatible_with in Bazel).
if(NOT SUBSPACE_CORO_BACKEND_RESOLVED STREQUAL "co20")
add_executable(c_client_test client_test.cc)
add_test(NAME c_client_test COMMAND c_client_test)
add_test(NAME c_client_split_buffer_test COMMAND c_client_test --use_split_buffers)
target_link_libraries(c_client_test PUBLIC subspace_c_client libserver gtest absl::flags_parse)
endif()
Loading
Loading