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
67 changes: 41 additions & 26 deletions include/condy/awaiters.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include "condy/finish_handles.hpp"
#include "condy/ring.hpp"
#include "condy/runtime.hpp"
#include "condy/type_traits.hpp"
#include "condy/work_type.hpp"
#include <coroutine>
#include <cstddef>
Expand All @@ -28,9 +29,9 @@ template <OpFinishHandleLike Handle> class HandleBox {
public:
HandleBox(Handle h) : handle_(std::move(h)) {}

Handle &get() { return handle_; }
Handle &get() noexcept { return handle_; }

void maybe_release() { /* No-op */ }
void maybe_release() noexcept { /* No-op */ }

private:
Handle handle_;
Expand All @@ -44,9 +45,9 @@ class HandleBox<ZeroCopyMixin<Func, HandleBase>> {
HandleBox(const HandleBox &other) // Deep copy
: handle_ptr_(std::make_unique<Handle>(*other.handle_ptr_)) {}

Handle &get() { return *handle_ptr_; }
Handle &get() noexcept { return *handle_ptr_; }

void maybe_release() { handle_ptr_.release(); }
void maybe_release() noexcept { handle_ptr_.release(); }

private:
std::unique_ptr<Handle> handle_ptr_;
Expand All @@ -60,9 +61,9 @@ template <OpFinishHandleLike Handle, PrepFuncLike Func> class OpAwaiterBase {
: prep_func_(func), finish_handle_(std::move(handle)) {}

public:
HandleType *get_handle() { return &finish_handle_.get(); }
HandleType *get_handle() noexcept { return &finish_handle_.get(); }

void init_finish_handle() { /* Leaf node, no-op */ }
void init_finish_handle() noexcept { /* Leaf node, no-op */ }

void register_operation(unsigned int flags) noexcept {
auto &context = detail::Context::current();
Expand All @@ -87,7 +88,7 @@ template <OpFinishHandleLike Handle, PrepFuncLike Func> class OpAwaiterBase {
register_operation(0);
}

auto await_resume() {
auto await_resume() noexcept {
auto result = finish_handle_.get().extract_result();
finish_handle_.maybe_release();
return result;
Expand Down Expand Up @@ -147,12 +148,14 @@ class [[nodiscard]] FlaggedOpAwaiter : public Awaiter {
using Base = Awaiter;
FlaggedOpAwaiter(Awaiter awaiter) : Base(std::move(awaiter)) {}

void register_operation(unsigned int flags) {
void register_operation(unsigned int flags) noexcept(
is_nothrow_suspendible_v<Awaiter>) {
Base::register_operation(flags | Flags);
}

template <typename PromiseType>
void await_suspend(std::coroutine_handle<PromiseType> h) {
void await_suspend(std::coroutine_handle<PromiseType> h) noexcept(
is_nothrow_suspendible_v<Awaiter>) {
Base::init_finish_handle();
Base::get_handle()->set_invoker(&h.promise());
register_operation(0);
Expand All @@ -168,9 +171,9 @@ class [[nodiscard]] RangedParallelAwaiterBase {
: awaiters_(std::move(awaiters)) {}

public:
HandleType *get_handle() { return &finish_handle_; }
HandleType *get_handle() noexcept { return &finish_handle_; }

void init_finish_handle() {
void init_finish_handle() noexcept {
using ChildHandle = typename Awaiter::HandleType;
std::vector<ChildHandle *> handles;
handles.reserve(awaiters_.size());
Expand All @@ -181,7 +184,8 @@ class [[nodiscard]] RangedParallelAwaiterBase {
finish_handle_.init(std::move(handles));
}

void register_operation(unsigned int flags) {
void register_operation(unsigned int flags) noexcept(
is_nothrow_suspendible_v<Awaiter>) {
for (auto &awaiter : awaiters_) {
awaiter.register_operation(flags);
}
Expand All @@ -191,13 +195,15 @@ class [[nodiscard]] RangedParallelAwaiterBase {
bool await_ready() const noexcept { return awaiters_.empty(); }

template <typename PromiseType>
void await_suspend(std::coroutine_handle<PromiseType> h) {
void await_suspend(std::coroutine_handle<PromiseType> h) noexcept(
is_nothrow_suspendible_v<Awaiter>) {
init_finish_handle();
finish_handle_.set_invoker(&h.promise());
register_operation(0);
}

typename Handle::ReturnType await_resume() {
typename Handle::ReturnType
await_resume() noexcept(is_nothrow_extract_result_v<Handle>) {
return finish_handle_.extract_result();
}

Expand Down Expand Up @@ -264,7 +270,8 @@ class [[nodiscard]] RangedLinkAwaiterBase
using Base = RangedWhenAllAwaiter<Awaiter>;
using Base::Base;

void register_operation(unsigned int flags) {
void register_operation(unsigned int flags) noexcept(
is_nothrow_suspendible_v<Awaiter>) {
auto *ring = detail::Context::current().ring();
ring->reserve_space(Base::awaiters_.size());
for (int i = 0; i < Base::awaiters_.size() - 1; ++i) {
Expand All @@ -274,7 +281,8 @@ class [[nodiscard]] RangedLinkAwaiterBase
}

template <typename PromiseType>
void await_suspend(std::coroutine_handle<PromiseType> h) {
void await_suspend(std::coroutine_handle<PromiseType> h) noexcept(
is_nothrow_suspendible_v<Awaiter>) {
Base::init_finish_handle();
Base::finish_handle_.set_invoker(&h.promise());
register_operation(0);
Expand Down Expand Up @@ -313,9 +321,9 @@ class [[nodiscard]] ParallelAwaiterBase {
std::make_tuple(std::move(new_awaiter)))) {}

public:
HandleType *get_handle() { return &finish_handle_; }
HandleType *get_handle() noexcept { return &finish_handle_; }

void init_finish_handle() {
void init_finish_handle() noexcept {
auto handles = foreach_init_finish_handle_();
static_assert(std::tuple_size<decltype(handles)>::value ==
sizeof...(Awaiters),
Expand All @@ -327,26 +335,29 @@ class [[nodiscard]] ParallelAwaiterBase {
handles);
}

void register_operation(unsigned int flags) {
void register_operation(unsigned int flags) noexcept(
(is_nothrow_suspendible_v<Awaiters> && ...)) {
foreach_register_operation_(flags);
}

public:
bool await_ready() const noexcept { return false; }

template <typename PromiseType>
void await_suspend(std::coroutine_handle<PromiseType> h) {
void await_suspend(std::coroutine_handle<PromiseType> h) noexcept(
(is_nothrow_suspendible_v<Awaiters> && ...)) {
init_finish_handle();
finish_handle_.set_invoker(&h.promise());
register_operation(0);
}

typename Handle::ReturnType await_resume() {
typename Handle::ReturnType
await_resume() noexcept(is_nothrow_extract_result_v<Handle>) {
return finish_handle_.extract_result();
}

private:
template <size_t Idx = 0> auto foreach_init_finish_handle_() {
template <size_t Idx = 0> auto foreach_init_finish_handle_() noexcept {
if constexpr (Idx < sizeof...(Awaiters)) {
std::get<Idx>(awaiters_).init_finish_handle();
return std::tuple_cat(
Expand All @@ -358,7 +369,8 @@ class [[nodiscard]] ParallelAwaiterBase {
}

template <size_t Idx = 0>
void foreach_register_operation_(unsigned int flags) {
void foreach_register_operation_(unsigned int flags) noexcept(
(is_nothrow_suspendible_v<Awaiters> && ...)) {
if constexpr (Idx < sizeof...(Awaiters)) {
std::get<Idx>(awaiters_).register_operation(flags);
foreach_register_operation_<Idx + 1>(flags);
Expand Down Expand Up @@ -428,22 +440,25 @@ class [[nodiscard]] LinkAwaiterBase : public WhenAllAwaiter<Awaiter...> {
using Base = WhenAllAwaiter<Awaiter...>;
using Base::Base;

void register_operation(unsigned int flags) {
void register_operation(unsigned int flags) noexcept(
(is_nothrow_suspendible_v<Awaiter> && ...)) {
auto *ring = detail::Context::current().ring();
ring->reserve_space(sizeof...(Awaiter));
foreach_register_operation_(flags);
}

template <typename PromiseType>
void await_suspend(std::coroutine_handle<PromiseType> h) {
void await_suspend(std::coroutine_handle<PromiseType> h) noexcept(
(is_nothrow_suspendible_v<Awaiter> && ...)) {
Base::init_finish_handle();
Base::finish_handle_.set_invoker(&h.promise());
register_operation(0);
}

private:
template <size_t Idx = 0>
void foreach_register_operation_(unsigned int flags) {
void foreach_register_operation_(unsigned int flags) noexcept(
(is_nothrow_suspendible_v<Awaiter> && ...)) {
if constexpr (Idx < sizeof...(Awaiter)) {
std::get<Idx>(Base::awaiters_)
.register_operation(Idx < sizeof...(Awaiter) - 1 ? flags | Flags
Expand Down
36 changes: 18 additions & 18 deletions include/condy/buffers.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,12 @@ class MutableBuffer : public BufferBase {
/**
* @brief Get the data of the buffer
*/
void *data() const { return data_; }
void *data() const noexcept { return data_; }

/**
* @brief Get the byte size of the buffer
*/
size_t size() const { return size_; }
size_t size() const noexcept { return size_; }

private:
void *data_ = nullptr;
Expand All @@ -62,12 +62,12 @@ class ConstBuffer : public BufferBase {
/**
* @brief Get the data of the buffer
*/
const void *data() const { return data_; }
const void *data() const noexcept { return data_; }

/**
* @brief Get the byte size of the buffer
*/
size_t size() const { return size_; }
size_t size() const noexcept { return size_; }

private:
const void *data_ = nullptr;
Expand All @@ -82,46 +82,46 @@ class ConstBuffer : public BufferBase {
* convert different types of memory regions into buffer objects for use in
* asynchronous operations.
*/
inline MutableBuffer buffer(void *data, size_t size) {
inline MutableBuffer buffer(void *data, size_t size) noexcept {
return MutableBuffer(data, size);
}

/**
* @copydoc buffer(void*, size_t)
*/
inline ConstBuffer buffer(const void *data, size_t size) {
inline ConstBuffer buffer(const void *data, size_t size) noexcept {
return ConstBuffer(data, size);
}

/**
* @copydoc buffer(void*, size_t)
*/
template <typename PodType, size_t N>
inline MutableBuffer buffer(PodType (&arr)[N]) {
inline MutableBuffer buffer(PodType (&arr)[N]) noexcept {
return MutableBuffer(static_cast<void *>(arr), sizeof(PodType) * N);
}

/**
* @copydoc buffer(void*, size_t)
*/
template <typename PodType, size_t N>
inline ConstBuffer buffer(const PodType (&arr)[N]) {
inline ConstBuffer buffer(const PodType (&arr)[N]) noexcept {
return ConstBuffer(static_cast<const void *>(arr), sizeof(PodType) * N);
}

/**
* @copydoc buffer(void*, size_t)
*/
template <typename PodType, size_t N>
inline MutableBuffer buffer(std::array<PodType, N> &arr) {
inline MutableBuffer buffer(std::array<PodType, N> &arr) noexcept {
return MutableBuffer(static_cast<void *>(arr.data()), sizeof(PodType) * N);
}

/**
* @copydoc buffer(void*, size_t)
*/
template <typename PodType, size_t N>
inline ConstBuffer buffer(const std::array<PodType, N> &arr) {
inline ConstBuffer buffer(const std::array<PodType, N> &arr) noexcept {
return ConstBuffer(static_cast<const void *>(arr.data()),
sizeof(PodType) * N);
}
Expand All @@ -130,7 +130,7 @@ inline ConstBuffer buffer(const std::array<PodType, N> &arr) {
* @copydoc buffer(void*, size_t)
*/
template <typename PodType>
inline MutableBuffer buffer(std::vector<PodType> &vec) {
inline MutableBuffer buffer(std::vector<PodType> &vec) noexcept {
return MutableBuffer(static_cast<void *>(vec.data()),
sizeof(PodType) * vec.size());
}
Expand All @@ -139,44 +139,44 @@ inline MutableBuffer buffer(std::vector<PodType> &vec) {
* @copydoc buffer(void*, size_t)
*/
template <typename PodType>
inline ConstBuffer buffer(const std::vector<PodType> &vec) {
inline ConstBuffer buffer(const std::vector<PodType> &vec) noexcept {
return ConstBuffer(static_cast<const void *>(vec.data()),
sizeof(PodType) * vec.size());
}

/**
* @copydoc buffer(void*, size_t)
*/
inline MutableBuffer buffer(std::string &str) {
inline MutableBuffer buffer(std::string &str) noexcept {
return MutableBuffer(static_cast<void *>(str.data()), str.size());
}

/**
* @copydoc buffer(void*, size_t)
*/
inline ConstBuffer buffer(const std::string &str) {
inline ConstBuffer buffer(const std::string &str) noexcept {
return ConstBuffer(static_cast<const void *>(str.data()), str.size());
}

/**
* @copydoc buffer(void*, size_t)
*/
inline ConstBuffer buffer(std::string_view strv) {
inline ConstBuffer buffer(std::string_view strv) noexcept {
return ConstBuffer(static_cast<const void *>(strv.data()), strv.size());
}

/**
* @copydoc buffer(void*, size_t)
*/
inline MutableBuffer buffer(iovec &iov) {
inline MutableBuffer buffer(iovec &iov) noexcept {
return MutableBuffer(iov.iov_base, iov.iov_len);
}

/**
* @copydoc buffer(void*, size_t)
*/
template <typename PodType, size_t N>
inline ConstBuffer buffer(std::span<const PodType, N> sp) {
inline ConstBuffer buffer(std::span<const PodType, N> sp) noexcept {
return ConstBuffer(static_cast<const void *>(sp.data()),
sp.size() * sizeof(PodType));
}
Expand All @@ -185,7 +185,7 @@ inline ConstBuffer buffer(std::span<const PodType, N> sp) {
* @copydoc buffer(void*, size_t)
*/
template <typename PodType, size_t N>
inline MutableBuffer buffer(std::span<PodType, N> sp) {
inline MutableBuffer buffer(std::span<PodType, N> sp) noexcept {
return MutableBuffer(static_cast<void *>(sp.data()),
sp.size() * sizeof(PodType));
}
Expand Down
Loading
Loading