Skip to content

Commit

Permalink
Add sleep() and schedule() for easier usage
Browse files Browse the repository at this point in the history
  • Loading branch information
longhao-li committed Sep 21, 2024
1 parent 33b0d88 commit c01c0b6
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 4 deletions.
4 changes: 2 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,10 @@ endif()
# Alias target to be consistent with the package name.
add_library(nyaio::nyaio ALIAS nyaio)

# This project uses C++20 coroutine.
# This project uses C++20 coroutine and concept.
target_compile_features(nyaio PUBLIC cxx_std_20)

# Set options for shared library.
# Set compiler options.
if(CMAKE_CXX_COMPILER_ID MATCHES "GNU|Clang")
target_compile_options(
nyaio
Expand Down
30 changes: 30 additions & 0 deletions include/nyaio/task.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -1167,6 +1167,23 @@ class TimeoutAwaitable {
__kernel_timespec m_timeout;
};

/// @brief
/// Suspend current coroutine for a while.
/// @tparam Rep
/// Type representation of duration type. See @c std::chrono::duration for details.
/// @tparam Period
/// Ratio type that is used to measure how to do conversion between different duration types. See
/// @c std::chrono::duration for details.
/// @param period
/// Duration to be suspended. Ratios greater than nanoseconds are not allowed.
/// @return
/// An awaitable object that can be used to suspend current coroutine for the specified time.
template <class Rep, class Period>
requires(std::ratio_less_equal_v<std::nano, Period>)
auto sleep(std::chrono::duration<Rep, Period> period) noexcept -> TimeoutAwaitable {
return TimeoutAwaitable(period);
}

/// @class ScheduleAwaitable
/// @brief
/// Awaitable object for scheduling a new task in current worker.
Expand Down Expand Up @@ -1220,6 +1237,19 @@ class ScheduleAwaitable {
template <class T>
ScheduleAwaitable(Task<T>) -> ScheduleAwaitable<T>;

/// @brief
/// Schedule a new task in current worker.
/// @tparam T
/// Return type of the task to be scheduled.
/// @param task
/// The task to be scheduled.
/// @return
/// An awaitable object that can be used to schedule the given task.
template <class T>
auto schedule(Task<T> task) noexcept -> ScheduleAwaitable<T> {
return {std::move(task)};
}

/// @class WaitAllAwaitable
/// @tparam Ts
/// Return types of the awaiting tasks. @c void is not allowed.
Expand Down
4 changes: 2 additions & 2 deletions test/task.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ auto scheduleAwaitableTask(IoContext &ctx) noexcept -> Task<> {
}(tid, childTid);

// The sub-coroutine runs in the same thread as the parent coroutine.
co_await ScheduleAwaitable(t);
co_await schedule(t);
while (!t.isCompleted())
co_await YieldAwaitable();

Expand All @@ -166,7 +166,7 @@ TEST_CASE("[task] ScheduleAwaitable") {
namespace {

auto waitAllTask0() noexcept -> Task<int> {
co_await TimeoutAwaitable(100ms);
co_await sleep(100ms);
co_return 1;
}

Expand Down

0 comments on commit c01c0b6

Please sign in to comment.