libcoro  1.0
Coroutine support library for C++20
cooperative.h
1 #pragma once
2 #include "prepared_coro.h"
3 
4 #include <optional>
5 #include <queue>
6 
7 namespace coro {
8 
15 
44 class suspend : public trace::suspend_always{
45 public:
46 
48 
52  static void await_suspend(std::coroutine_handle<> h) {
53  if (local_queue) {
54  local_queue->push(h);
55  } else {
56  std::queue<prepared_coro> q;
57  local_queue = &q;
58  trace::resume(h);
59  while (!q.empty()) {
60  auto n = std::move(q.front());
61  q.pop();
62  n();
63  }
64  local_queue = nullptr;
65  }
66  }
67 
69 
74  friend bool in_cooperative_mode() {
75  return local_queue != nullptr;
76  }
77 
79 
87  friend void enqueue(prepared_coro c) {
89  }
90 
91 
92 
93 protected:
94  static thread_local std::queue<prepared_coro> *local_queue;
95 };
96 
97 
98 inline thread_local std::queue<prepared_coro> *suspend::local_queue = nullptr;
99 
100 
101 
102 }
std::coroutine_handle symmetric_transfer()
release handle to be used in function await_suspend()
Definition: prepared_coro.h:53
contains prepared coroutine (prepared to run)
Definition: prepared_coro.h:15
static void await_suspend(std::coroutine_handle<> h)
this function is static.
Definition: cooperative.h:52
Suspend current coroutine and switch to another coroutine ready to run.
Definition: cooperative.h:44
friend bool in_cooperative_mode()
Determines, whether current thread is in cooperative mode.
Definition: cooperative.h:74
friend void enqueue(prepared_coro c)
Enqueue coroutine to the queue in the cooperative mode.
Definition: cooperative.h:87
void resume(std::coroutine_handle<> h) noexcept
Record resumption of an coroutine.
Definition: trace.h:382
main namespace
Definition: aggregator.h:8