Skip to content

Commit

Permalink
support float type time duration
Browse files Browse the repository at this point in the history
  • Loading branch information
netcan committed Nov 22, 2021
1 parent 97a347a commit 7caccb1
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 10 deletions.
16 changes: 8 additions & 8 deletions include/asyncio/event_loop.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,28 +20,28 @@ class EventLoop: private NonCopyable {

public:
EventLoop() {
using namespace std::chrono;
auto now = system_clock::now();
auto now = std::chrono::system_clock::now();
start_time_ = duration_cast<MSDuration>(now.time_since_epoch());
}

MSDuration time() {
using namespace std::chrono;
auto now = system_clock::now();
auto now = std::chrono::system_clock::now();
return duration_cast<MSDuration>(now.time_since_epoch()) - start_time_;
}

bool is_stop() {
return schedule_.empty() && ready_.empty();
}

void call_later(MSDuration delay, Handle& callback) {
call_at(time() + delay, callback);
template<typename Rep, typename Period>
void call_later(std::chrono::duration<Rep, Period> delay, Handle& callback) {
call_at(time() + duration_cast<MSDuration>(delay), callback);
}

void call_at(MSDuration when, Handle& callback) {
template<typename Rep, typename Period>
void call_at(std::chrono::duration<Rep, Period> when, Handle& callback) {
callback.set_state(PromiseState::PENDING);
schedule_.emplace_back(std::make_pair(when, &callback));
schedule_.emplace_back(std::make_pair(duration_cast<MSDuration>(when), &callback));
std::ranges::push_heap(schedule_, std::ranges::greater{}, &TimerHandle::first);
}

Expand Down
6 changes: 5 additions & 1 deletion include/asyncio/sleep.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,22 @@
#ifndef ASYNCIO_SLEEP_H
#define ASYNCIO_SLEEP_H
#include <asyncio/asyncio_ns.h>
#include <asyncio/noncopyable.h>
#include <chrono>
ASYNCIO_NS_BEGIN
namespace detail {
template<typename Duration>
struct SleepAwaiter {
struct SleepAwaiter: private NonCopyable {
SleepAwaiter(Duration delay): delay_(delay) {}
constexpr bool await_ready() noexcept { return false; }
constexpr void await_resume() const noexcept {}
template<typename Promise>
void await_suspend(std::coroutine_handle<Promise> caller) const noexcept {
auto& loop = get_event_loop();
loop.call_later(delay_, caller.promise());
}

private:
Duration delay_;
};
}
Expand Down
2 changes: 2 additions & 0 deletions include/asyncio/wait_for.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@
#define ASYNCIO_WAIT_FOR_H
#include <asyncio/asyncio_ns.h>
#include <asyncio/concept/future.h>
#include <asyncio/awaitable_traits.h>
#include <asyncio/schedule_task.h>
#include <asyncio/event_loop.h>
#include <asyncio/exception.h>
#include <asyncio/void_value.h>
#include <chrono>
ASYNCIO_NS_BEGIN
Expand Down
2 changes: 1 addition & 1 deletion test/ut/task_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ SCENARIO("test gather") {
int r = 1;
for (int i = 2; i <= number; ++i) {
fmt::print("Task {}: Compute factorial({}), currently i={}...\n", name, number, i);
co_await asyncio::sleep(500ms);
co_await asyncio::sleep(0.3s);
r *= i;
}
fmt::print("Task {}: factorial({}) = {}\n", name, number, r);
Expand Down

0 comments on commit 7caccb1

Please sign in to comment.