Skip to content

Commit ffb29bb

Browse files
authored
replace "get_scheduler" with "this_task" example using more syntax sugar (#63)
* replace get_scheduler with this_task example * fix format
1 parent 7656cc3 commit ffb29bb

4 files changed

Lines changed: 22 additions & 9 deletions

File tree

examples/CMakeLists.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,8 @@ add_executable(alien_counting_scope alien_counting_scope.cpp)
3636
target_link_libraries(alien_counting_scope PRIVATE CoroutineTests)
3737
add_executable(alien_schedule_on alien_schedule_on.cpp)
3838
target_link_libraries(alien_schedule_on PRIVATE CoroutineTests)
39-
add_executable(alien_get_scheduler alien_get_scheduler.cpp)
40-
target_link_libraries(alien_get_scheduler PRIVATE CoroutineTests)
39+
add_executable(alien_this_task alien_this_task.cpp)
40+
target_link_libraries(alien_this_task PRIVATE CoroutineTests)
4141

4242
if(BUILD_STDEXEC)
4343
add_executable(exec_task_stdexec exec_task.cpp)

examples/README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -150,11 +150,11 @@ Link: [alien_schedule_on.cpp](alien_schedule_on.cpp)
150150

151151
This example demonstrates changing scheduler used by part of a chain of coroutines following the semantics from ["Alien" example](#alien). The `schedule_on` algorithm can be used to adapt a coroutine to use a different scheduler than its parent. Starting the coroutine suspends its parent and reschedule work on the new scheduler, finishing the coroutine reschedules continuation of its parent using parent's scheduler.
152152

153-
## Alien get_scheduler
153+
## Alien this_task
154154

155-
Link: [alien_get_scheduler.cpp](alien_get_scheduler.cpp)
155+
Link: [alien_this_task.cpp](alien_this_task.cpp)
156156

157-
This example shows how the contents of promise type can be extracted from within a coroutine. The `GetScheduler` awaitable when co_awaited accessed the promise type and returns the currently used coroutine. The example uses then `schedule_on` algorithm from ["alien schedule_on" example](#alien-schedule_on) to alternate between used schedulers in each nested coroutine.
157+
This example shows how the contents of promise type can be extracted from within a coroutine. The `this_task::scheduler` uses custom `operator co_await` that returns a helper awaitable which returns the scheduler used by the current coroutine. The example uses then `schedule_on` algorithm from ["alien schedule_on" example](#alien-schedule_on) to alternate between used schedulers in each nested coroutine.
158158

159159
## Capy task
160160

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
#include <format>
22
#include <string_view>
33

4-
#include "CoroutineTests/alien/get_scheduler.hpp"
54
#include "CoroutineTests/alien/schedule_on.hpp"
65
#include "CoroutineTests/alien/sync_wait.hpp"
6+
#include "CoroutineTests/alien/this_task.hpp"
77
#include "CoroutineTests/alien/tool.hpp"
88
#include "CoroutineTests/threadpool.hpp"
99
#include "logging_utils.hpp"
1010

1111
using namespace CoroutineTests::alien;
12+
1213
using scheduler_t = std::function<void(std::coroutine_handle<>)>;
1314
tool::Task<void> innermost_task(std::string_view parent) {
1415
const auto self = format_name(parent, "innermost_task");
@@ -31,7 +32,7 @@ tool::Task<void> outer_task(scheduler_t inner_scheduler,
3132
std::string_view parent) {
3233
const auto self = format_name(parent, "outer_task");
3334
log(self) << "Running outer task" << std::endl;
34-
auto current_scheduler = co_await GetScheduler{};
35+
auto current_scheduler = co_await this_task::scheduler;
3536
log(self) << "Got current scheduler in outer task, scheduling inner task "
3637
"on inner scheduler"
3738
<< std::endl;

include/CoroutineTests/alien/get_scheduler.hpp renamed to include/CoroutineTests/alien/this_task.hpp

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
#include <coroutine>
22
#include <functional>
33

4-
namespace CoroutineTests::alien {
4+
namespace CoroutineTests::alien::this_task {
5+
6+
namespace detail {
57

68
class GetScheduler {
79
public:
@@ -24,4 +26,14 @@ class GetScheduler {
2426
std::function<void(std::coroutine_handle<>)> m_scheduler;
2527
};
2628

27-
} // namespace CoroutineTests::alien
29+
} // namespace detail
30+
31+
struct scheduler_tag {};
32+
33+
constexpr scheduler_tag scheduler;
34+
35+
// Custom operator to produce a helper awaitable which will be actually awaited.
36+
inline auto operator co_await(scheduler_tag) noexcept {
37+
return detail::GetScheduler{};
38+
}
39+
} // namespace CoroutineTests::alien::this_task

0 commit comments

Comments
 (0)