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
16 changes: 2 additions & 14 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -35,19 +35,6 @@ FetchContent_Declare(
FetchContent_MakeAvailable(abseil)
# Abseil provides targets like absl::base, absl::strings, etc.

# --- External Dependency: co ---
# cpp_toolbelt depends on co, so we need to fetch and make it available.
FetchContent_Declare(
co
GIT_REPOSITORY https://github.com/dallison/co.git
GIT_TAG cf1252b2f5952d7cba83b67dd69288971c0a2b57
# Pass architecture settings to co's CMake build
CMAKE_ARGS
CMAKE_OSX_ARCHITECTURES="${CMAKE_OSX_ARCHITECTURES}"
)
FetchContent_MakeAvailable(co)
# co provides the 'co' target.

# --- toolbelt Library Target ---
# Corresponds to //toolbelt:toolbelt in Bazel
# Source files are omitted here for manual addition by the user.
Expand All @@ -67,6 +54,8 @@ add_library(toolbelt STATIC
toolbelt/payload_buffer.h
toolbelt/pipe.cc
toolbelt/pipe.h
toolbelt/poller.cc
toolbelt/poller.h
toolbelt/sockets.cc
toolbelt/sockets.h
toolbelt/table.cc
Expand All @@ -88,7 +77,6 @@ target_link_libraries(toolbelt PUBLIC
absl::log # Added as it's used by toolbelt/logging
absl::time # Added as it's used by toolbelt/time
absl::synchronization # Added as it's used by toolbelt/utilities/thread_pool
co # Link against the co library
)

# Note: If there are tests in cpp_toolbelt, they would need a separate target
Expand Down
5 changes: 5 additions & 0 deletions MODULE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,9 @@ bazel_dep(name = "rules_cc", version = "0.2.17")
# module_name = "coroutines",
# path = "../co",
# )
git_override(
module_name = "coroutines",
commit = "21e28c2c6b28ea1f4cf5f9948a9c6dd379b5ec7e",
remote = "https://github.com/mikael-s-persson/co.git",
)

14 changes: 6 additions & 8 deletions MODULE.bazel.lock

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

5 changes: 4 additions & 1 deletion toolbelt/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ load("@rules_cc//cc:defs.bzl", "cc_library", "cc_test")
cc_library(
name = "toolbelt",
srcs = [
"async_waiter.cc",
"color.cc",
"fd.cc",
"hexdump.cc",
Expand All @@ -17,6 +18,7 @@ cc_library(
"stacktrace.cc",
],
hdrs = [
"async_waiter.h",
"bitset.h",
"clock.h",
"color.h",
Expand All @@ -37,7 +39,6 @@ cc_library(
"@abseil-cpp//absl/strings",
"@abseil-cpp//absl/strings:str_format",
"@abseil-cpp//absl/types:span",
"@coroutines//:co",
],
)

Expand Down Expand Up @@ -91,6 +92,7 @@ cc_test(
"@abseil-cpp//absl/status:status_matchers",
"@abseil-cpp//absl/status:statusor",
"@googletest//:gtest_main",
"@coroutines//:co",
],
)

Expand All @@ -105,6 +107,7 @@ cc_test(
"@abseil-cpp//absl/status:status_matchers",
"@abseil-cpp//absl/status:statusor",
"@googletest//:gtest_main",
"@coroutines//:co",
],
)

Expand Down
60 changes: 60 additions & 0 deletions toolbelt/async_waiter.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
// Copyright 2026 Mikael Persson
// All Rights Reserved
// See LICENSE file for licensing information.
#include "toolbelt/async_waiter.h"

#include <chrono>
#include <poll.h>
#include <thread>

namespace toolbelt {

PosixPoller::PosixPoller(uint64_t yield_sleep_ns) : AsyncWaiter(), yield_sleep_ns_(yield_sleep_ns) {}

void PosixPoller::Yield() const {
if (yield_sleep_ns_ == 0) {
std::this_thread::yield();
} else {
std::this_thread::sleep_for(std::chrono::nanoseconds(yield_sleep_ns_));
}
}

void PosixPoller::Nanosleep(uint64_t ns) const {
std::this_thread::sleep_for(std::chrono::nanoseconds(ns));
}

void PosixPoller::AddToUserWaitFds(int fd, uint32_t event_mask) const {
user_fds_.push_back({.fd = fd, .events = short(event_mask)});
}

int PosixPoller::PollImpl(std::vector<struct pollfd> &fds, int timeout_ms) const {
int ret = ::poll(user_fds_.data(), user_fds_.size(), timeout_ms);
if (ret < 0) {
return -2;
}
if (ret == 0) {
return -1;
}
for (auto &pfd : fds) {
if (pfd.revents & (pfd.events | POLLOUT)) {
return pfd.fd;
}
}
return -1;
}

int PosixPoller::WaitOnUserWaitFds(uint64_t timeout_ns) const {
int timeout_ms = -1;
if (timeout_ns != 0) {
timeout_ms = std::max(1, static_cast<int>(timeout_ns / 1000000ULL));
}
int result = PollImpl(user_fds_, timeout_ms);
user_fds_.clear();
return result;
}

int PosixPoller::PollWithMutableFds(std::vector<struct pollfd> &fds) const {
return PollImpl(fds, 0);
}

} // namespace toolbelt
Loading
Loading