Skip to content

Commit

Permalink
improve (#537)
Browse files Browse the repository at this point in the history
  • Loading branch information
qicosmos authored Mar 20, 2024
1 parent e7c3527 commit 1d44c39
Showing 1 changed file with 16 additions and 12 deletions.
28 changes: 16 additions & 12 deletions include/cinatra/ylt/coro_io/coro_io.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -260,13 +260,11 @@ inline async_simple::coro::Lazy<std::error_code> async_handshake(
#endif
class period_timer : public asio::steady_timer {
public:
using asio::steady_timer::steady_timer;
template <typename T>
period_timer(coro_io::ExecutorWrapper<T> *executor)
: asio::steady_timer(executor->get_asio_executor()) {}
template <typename executor_t, typename Rep, typename Period>
period_timer(const executor_t &executor,
const std::chrono::duration<Rep, Period> &timeout_duration)
: asio::steady_timer(executor, timeout_duration) {}

async_simple::coro::Lazy<bool> async_await() noexcept {
callback_awaitor<bool> awaitor;

Expand Down Expand Up @@ -297,10 +295,10 @@ inline async_simple::coro::Lazy<void> sleep_for(const Duration &d) {
}
}

template <typename R, typename Func>
template <typename R, typename Func, typename Executor>
struct post_helper {
void operator()(auto handler) const {
asio::dispatch(e->get_asio_executor(), [this, handler]() {
asio::dispatch(e, [this, handler]() {
try {
if constexpr (std::is_same_v<R, async_simple::Try<void>>) {
func();
Expand All @@ -317,24 +315,30 @@ struct post_helper {
}
});
}
coro_io::ExecutorWrapper<> *e;
Executor e;
Func func;
};

template <typename Func>
template <typename Func, typename Executor>
inline async_simple::coro::Lazy<
async_simple::Try<typename util::function_traits<Func>::return_type>>
post(Func func,
coro_io::ExecutorWrapper<> *e = coro_io::get_global_block_executor()) {
post(Func func, Executor executor) {
using R =
async_simple::Try<typename util::function_traits<Func>::return_type>;

callback_awaitor<R> awaitor;

post_helper<R, Func> helper{e, std::move(func)};
post_helper<R, Func, Executor> helper{executor, std::move(func)};
co_return co_await awaitor.await_resume(helper);
}

template <typename Func>
inline async_simple::coro::Lazy<
async_simple::Try<typename util::function_traits<Func>::return_type>>
post(Func func,
coro_io::ExecutorWrapper<> *e = coro_io::get_global_block_executor()) {
co_return co_await post(std::move(func), e->get_asio_executor());
}

template <typename R>
struct coro_channel
: public asio::experimental::channel<void(std::error_code, R)> {
Expand Down

0 comments on commit 1d44c39

Please sign in to comment.