24template <CQEHandlerLike CQEHandler,
typename Receiver>
25class OpFinishHandle :
public OpFinishHandleBase {
27 OpFinishHandle(CQEHandler cqe_handler, Receiver receiver)
28 : cqe_handler_(std::move(cqe_handler)), receiver_(std::move(receiver)) {
29 this->handle_func_ = handle_static_;
32 CONDY_DELETE_COPY_MOVE(OpFinishHandle);
35 bool stop_requested() noexcept {
36 auto stop_token = receiver_.get_stop_token();
37 return stop_token.stop_requested();
40 void maybe_set_cancel(Runtime *runtime)
noexcept {
41 auto stop_token = receiver_.get_stop_token();
42 if (stop_token.stop_possible()) {
43 stop_callback_.emplace(std::move(stop_token),
44 Cancellation{
this, runtime});
49 static bool handle_static_(
void *data, io_uring_cqe *cqe)
noexcept {
50 auto *self =
static_cast<OpFinishHandle *
>(data);
51 return self->handle_impl_(cqe);
54 bool handle_impl_(io_uring_cqe *cqe)
noexcept {
62 void operator()() noexcept {
63 runtime->cancel_internal(encode_work(self, WorkType::Common));
67 using StopCallbackType =
68 stop_callback_t<stop_token_t<Receiver>, Cancellation>;
71 void finish_(io_uring_cqe *cqe)
noexcept {
72 stop_callback_.reset();
73 std::move(receiver_)(cqe_handler_(cqe));
76 CQEHandler cqe_handler_;
78 std::optional<StopCallbackType> stop_callback_;
81template <CQEHandlerLike CQEHandler,
typename Func,
typename Receiver>
82class MultiShotOpFinishHandle :
public OpFinishHandle<CQEHandler, Receiver> {
84 MultiShotOpFinishHandle(CQEHandler cqe_handler, Receiver receiver,
86 : OpFinishHandle<CQEHandler, Receiver>(std::move(cqe_handler),
88 func_(std::move(func)) {
89 this->handle_func_ = handle_static_;
93 static bool handle_static_(
void *data, io_uring_cqe *cqe)
noexcept {
94 auto *self =
static_cast<MultiShotOpFinishHandle *
>(data);
95 return self->handle_impl_(cqe);
98 bool handle_impl_(io_uring_cqe *cqe)
noexcept
100 if (cqe->flags & IORING_CQE_F_MORE) {
101 func_(this->cqe_handler_(cqe));
113template <CQEHandlerLike CQEHandler,
typename Func,
typename Receiver>
114class ZeroCopyOpFinishHandle :
public OpFinishHandle<CQEHandler, Receiver> {
116 ZeroCopyOpFinishHandle(CQEHandler cqe_handler, Receiver receiver, Func func)
117 : OpFinishHandle<CQEHandler, Receiver>(std::move(cqe_handler),
118 std::move(receiver)),
119 free_func_(std::move(func)) {
120 this->handle_func_ = handle_static_;
124 static bool handle_static_(
void *data, io_uring_cqe *cqe)
noexcept {
125 auto *self =
static_cast<ZeroCopyOpFinishHandle *
>(data);
126 return self->handle_impl_(cqe);
129 bool handle_impl_(io_uring_cqe *cqe)
noexcept
131 if (cqe->flags & IORING_CQE_F_MORE) {
135 if (cqe->flags & IORING_CQE_F_NOTIF) {
149 void notify_(int32_t res)
noexcept {
158template <
typename Handle>
class HandleBox {
160 template <
typename... Args>
161 HandleBox(Args &&...args) : handle_(std::forward<Args>(args)...) {}
163 CONDY_DELETE_COPY_MOVE(HandleBox);
166 Handle &get() noexcept {
return handle_; }
172template <CQEHandlerLike CQEHandler,
typename Func,
typename Receiver>
173class HandleBox<ZeroCopyOpFinishHandle<CQEHandler, Func, Receiver>> {
175 using Handle = ZeroCopyOpFinishHandle<CQEHandler, Func, Receiver>;
177 template <
typename... Args>
178 HandleBox(Args &&...args)
179 : handle_ptr_(new Handle(std::forward<Args>(args)...)) {}
181 CONDY_DELETE_COPY_MOVE(HandleBox);
184 Handle &get() noexcept {
return *handle_ptr_; }
The main namespace for the Condy library.
Runtime type for running the io_uring event loop.
Internal utility classes and functions used by Condy.