ublk-cpp v0.1
A ublk library for std::execution.
Loading...
Searching...
No Matches
retry.hpp
1#pragma once
2
5#include <condy.hpp>
6#include <cstddef>
7#include <cstdint>
8#include <system_error>
9
10namespace ublk {
11namespace detail {
12
13template <typename Sched, typename Alloc, typename Fn>
14inline ex::task<bool, TaskEnv<Sched, Alloc>> retry(Fn fn, size_t max_retry,
15 int64_t sleep_ms) {
16 if (co_await fn()) {
17 co_return true;
18 }
19 __kernel_timespec ts = {};
20 ts.tv_sec = sleep_ms / 1000;
21 ts.tv_nsec = (sleep_ms % 1000) * 1'000'000;
22 bool ok = false;
23 for (size_t i = 0; i < max_retry; i++) {
24 co_await (condy::async_timeout(&ts, 0, 0) |
25 ex::upon_error(
26 [](std::error_code ec) noexcept { return -ec.value(); }));
27 ok = co_await fn();
28 if (ok) {
29 break;
30 }
31 }
32 co_return ok;
33}
34
35} // namespace detail
36} // namespace ublk
The std::execution backend namespace used by ublk-cpp.
The main namespace of the ublk-cpp library.
Definition ublk.hpp:21
Task environment utils.