Skip to content

Commit

Permalink
refactor handle
Browse files Browse the repository at this point in the history
  • Loading branch information
netcan committed Jan 8, 2022
1 parent 1770128 commit 37cca4c
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 11 deletions.
17 changes: 9 additions & 8 deletions include/asyncio/handle.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,22 +21,23 @@ using HandleId = uint64_t;
struct Handle { // type erase for EventLoop
Handle() noexcept: handle_id_(handle_id_generation_++) {}
virtual void run() = 0;
virtual void set_state(PromiseState state) {}
HandleId get_handle_id() { return handle_id_; }
virtual ~Handle() = default;
private:
HandleId handle_id_;
static HandleId handle_id_generation_;
};

struct CoroHandle: Handle {
std::string frame_name() const {
const auto& frame_info = get_frame_info();
return fmt::format("{} at {}:{}", frame_info.function_name(),
frame_info.file_name(), frame_info.line());
}
virtual void dump_backtrace(size_t depth = 0) const {};
virtual void set_state(PromiseState state) {}
HandleId get_handle_id() { return handle_id_; }
virtual ~Handle() = default;

private:
virtual const std::source_location& get_frame_info() const;

private:
HandleId handle_id_;
static HandleId handle_id_generation_;
};

ASYNCIO_NS_END
Expand Down
4 changes: 2 additions & 2 deletions include/asyncio/task.h
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ struct Task: private NonCopyable {
return Awaiter {handle_};
}

struct promise_type: Handle, Result<R> {
struct promise_type: CoroHandle, Result<R> {
promise_type() = default;

template<typename... Args> // from free function
Expand Down Expand Up @@ -137,7 +137,7 @@ struct Task: private NonCopyable {
// to auto delete by final awaiter
PromiseState state_ {PromiseState::UNSCHEDULED};
const bool wait_at_initial_suspend_ {true};
Handle* continuation_ {};
CoroHandle* continuation_ {};
std::source_location frame_info_{};
};

Expand Down
2 changes: 1 addition & 1 deletion src/event_loop.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ void EventLoop::run_once() {

HandleId Handle::handle_id_generation_ = 0;

const std::source_location& Handle::get_frame_info() const {
const std::source_location& CoroHandle::get_frame_info() const {
static const std::source_location frame_info = std::source_location::current();
return frame_info;
}
Expand Down

0 comments on commit 37cca4c

Please sign in to comment.