uvco 0.1
Loading...
Searching...
No Matches
close.h
Go to the documentation of this file.
1// uvco (c) 2023 Lewin Bormann. See LICENSE for specific terms.
2
3#pragma once
4
5#include <cstdio>
6#include <uv.h>
7
9
10#include <boost/assert.hpp>
11#include <coroutine>
12#include <optional>
13
14namespace uvco {
15
17 [[nodiscard]] bool await_ready() const;
18 bool await_suspend(std::coroutine_handle<> handle);
19 void await_resume();
20
21 std::optional<std::coroutine_handle<>> handle_;
22 bool closed_ = false;
23};
24
25void onCloseCallback(uv_handle_t *stream);
26
27template <typename T, typename C>
28Promise<void> closeHandle(T *handle, C closer) {
29 BOOST_ASSERT(handle != nullptr);
30 CloseAwaiter awaiter{};
31 setData(handle, &awaiter);
32 closer(handle, onCloseCallback);
33 co_await awaiter;
34 setData(handle, (void *)nullptr);
35 BOOST_ASSERT(awaiter.closed_);
36}
37
38template <typename T> Promise<void> closeHandle(T *handle) {
39 return closeHandle((uv_handle_t *)handle, uv_close);
40}
41
42} // namespace uvco
Definition promise.h:76
void setData(Handle *handle, Data *data)
Definition internal_utils.h:55
Definition async_work.cc:17
void onCloseCallback(uv_handle_t *stream)
Definition close.cc:23
Promise< void > closeHandle(T *handle, C closer)
Definition close.h:28
Definition close.h:16
bool await_ready() const
Definition close.cc:19
bool closed_
Definition close.h:22
bool await_suspend(std::coroutine_handle<> handle)
Definition close.cc:14
std::optional< std::coroutine_handle<> > handle_
Definition close.h:21
void await_resume()
Definition close.cc:21