forked from nanocurrency/nano-node
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ef2524b
commit 828c29f
Showing
3 changed files
with
100 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
#pragma once | ||
|
||
#include <nano/lib/utility.hpp> | ||
|
||
#include <boost/asio.hpp> | ||
|
||
namespace asio = boost::asio; | ||
|
||
namespace nano::this_coro | ||
{ | ||
asio::awaitable<void> sleep_for (auto duration) | ||
{ | ||
asio::steady_timer timer{ co_await asio::this_coro::executor }; | ||
timer.expires_after (duration); | ||
co_await timer.async_wait (asio::use_awaitable); | ||
} | ||
} | ||
|
||
namespace nano | ||
{ | ||
template <typename Executor> | ||
class async_condition | ||
{ | ||
public: | ||
explicit async_condition (Executor & executor) : | ||
executor{ executor }, | ||
timer{ executor } | ||
{ | ||
} | ||
|
||
void notify () | ||
{ | ||
debug_assert (executor.running_in_this_thread ()); | ||
|
||
timer.cancel (); | ||
} | ||
|
||
asio::awaitable<void> wait_for_async (auto duration) | ||
{ | ||
debug_assert (executor.running_in_this_thread ()); | ||
|
||
timer.expires_after (duration); | ||
co_await timer.async_wait (asio::use_awaitable); | ||
} | ||
|
||
private: | ||
Executor & executor; | ||
asio::steady_timer timer; | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters