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
30 changes: 28 additions & 2 deletions include/condy/task.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,22 @@ template <typename T = void, typename Allocator = void> class TaskBase {
public:
using PromiseType = typename Coro<T, Allocator>::promise_type;

TaskBase() : TaskBase(nullptr) {}
TaskBase(std::coroutine_handle<PromiseType> h) : handle_(h) {}
TaskBase(TaskBase &&other) noexcept
: handle_(std::exchange(other.handle_, nullptr)) {}
TaskBase &operator=(TaskBase &&other) noexcept {
if (this != &other) {
if (handle_) {
panic_on("Task destroyed without being awaited");
}
handle_ = std::exchange(other.handle_, nullptr);
}
return *this;
}

TaskBase(const TaskBase &) = delete;
TaskBase &operator=(const TaskBase &) = delete;
TaskBase &operator=(TaskBase &&other) = delete;

~TaskBase() {
if (handle_) {
Expand All @@ -49,9 +58,16 @@ template <typename T = void, typename Allocator = void> class TaskBase {
handle_ = nullptr;
}

/**
* @brief Check if the task is still joinable. Similar to
* `std::thread::joinable()`.
*/
bool joinable() const noexcept { return handle_ != nullptr; }

/**
* @brief Await the task asynchronously.
* @return T The result of the coroutine.
* @throw std::invalid_argument If the task is not joinable.
* @throws Any exception thrown inside the coroutine.
* @details This function allows the caller to await the completion of the
* coroutine associated with the task. It suspends the caller coroutine
Expand All @@ -73,6 +89,9 @@ void TaskBase<T, Allocator>::wait_inner_(
if (detail::Context::current().runtime() != nullptr) [[unlikely]] {
throw std::logic_error("Sync wait inside runtime");
}
if (handle == nullptr) [[unlikely]] {
throw std::invalid_argument("Task not joinable");
}
std::promise<void> prom;
auto fut = prom.get_future();
struct TaskWaiter : public InvokerAdapter<TaskWaiter> {
Expand Down Expand Up @@ -112,6 +131,7 @@ class [[nodiscard]] Task : public TaskBase<T, Allocator> {
/**
* @brief Wait synchronously for the task to complete and get the result.
* @return T The result of the coroutine.
* @throws std::invalid_argument If the task is not joinable.
* @throws Any exception thrown inside the coroutine.
* @details This function blocks the current thread until the coroutine
* associated with the task completes. It then retrieves the result of the
Expand Down Expand Up @@ -140,6 +160,7 @@ class [[nodiscard]] Task<void, Allocator> : public TaskBase<void, Allocator> {

/**
* @brief Wait synchronously for the task to complete.
* @throws std::invalid_argument If the task is not joinable.
* @throws Any exception thrown inside the coroutine.
* @details This function blocks the current thread until the coroutine
* associated with the task completes. If the coroutine throws an exception,
Expand All @@ -164,7 +185,12 @@ struct TaskAwaiterBase : public InvokerAdapter<TaskAwaiterBase<T, Allocator>> {
Runtime *runtime)
: task_handle_(task_handle), runtime_(runtime) {}

bool await_ready() const noexcept { return false; }
bool await_ready() const {
if (task_handle_ == nullptr) {
throw std::invalid_argument("Task not joinable");
}
return false;
}

template <typename PromiseType>
bool
Expand Down
42 changes: 42 additions & 0 deletions tests/test_task.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include "condy/runtime_options.hpp"
#include "condy/task.hpp"
#include <doctest/doctest.h>
#include <stdexcept>
#include <thread>

namespace {
Expand All @@ -11,6 +12,47 @@ condy::RuntimeOptions options = condy::RuntimeOptions().sq_size(8).cq_size(16);

} // namespace

TEST_CASE("test task - construct") {
condy::Runtime runtime(options);
auto func = []() -> condy::Coro<void> { co_return; };

condy::Task<void> task;
REQUIRE(!task.joinable());

auto task2 = condy::co_spawn(runtime, func());
REQUIRE(task2.joinable());

task = std::move(task2);
REQUIRE(task.joinable());
// NOLINTNEXTLINE(bugprone-use-after-move)
REQUIRE(!task2.joinable());

task.detach();
REQUIRE(!task.joinable());
}

TEST_CASE("test task - joinable check") {
condy::Runtime runtime(options);
std::thread rt_thread([&]() { runtime.run(); });

condy::Task<void> task;
REQUIRE(!task.joinable());
REQUIRE_THROWS_AS(task.wait(), std::invalid_argument);

auto func = [&]() -> condy::Coro<void> {
REQUIRE(!task.joinable());
REQUIRE_THROWS_AS((co_await task), std::invalid_argument);
co_return;
};

auto task2 = condy::co_spawn(runtime, func());
REQUIRE(task2.joinable());
REQUIRE_NOTHROW(task2.wait());

runtime.allow_exit();
rt_thread.join();
}

TEST_CASE("test task - local spawn and await") {
condy::Runtime runtime(options);
bool finished = false;
Expand Down
Loading