23template <CQEHandlerLike CQEHandler,
typename Receiver>
24class OpFinishHandle :
public OpFinishHandleBase {
26 OpFinishHandle(CQEHandler cqe_handler, Receiver receiver)
27 : cqe_handler_(std::move(cqe_handler)), receiver_(std::move(receiver)) {
28 this->handle_func_ = handle_static_;
31 CONDY_DELETE_COPY_MOVE(OpFinishHandle);
34 void maybe_set_cancel(Runtime *runtime)
noexcept {
35 auto stop_token = receiver_.get_stop_token();
36 if (stop_token.stop_possible()) {
37 stop_callback_.emplace(std::move(stop_token),
38 Cancellation{
this, runtime});
43 static bool handle_static_(
void *data, io_uring_cqe *cqe)
noexcept {
44 auto *self =
static_cast<OpFinishHandle *
>(data);
45 return self->handle_impl_(cqe);
48 bool handle_impl_(io_uring_cqe *cqe)
noexcept {
56 void operator()() noexcept {
57 runtime->cancel(encode_work(self, WorkType::Common));
61 using StopCallbackType =
62 stop_callback_t<stop_token_t<Receiver>, Cancellation>;
65 void finish_(io_uring_cqe *cqe)
noexcept {
66 stop_callback_.reset();
67 std::move(receiver_)(cqe_handler_(cqe));
70 CQEHandler cqe_handler_;
72 std::optional<StopCallbackType> stop_callback_;
75template <CQEHandlerLike CQEHandler,
typename Func,
typename Receiver>
76class MultiShotOpFinishHandle :
public OpFinishHandle<CQEHandler, Receiver> {
78 MultiShotOpFinishHandle(CQEHandler cqe_handler, Receiver receiver,
80 : OpFinishHandle<CQEHandler, Receiver>(std::move(cqe_handler),
82 func_(std::move(func)) {
83 this->handle_func_ = handle_static_;
87 static bool handle_static_(
void *data, io_uring_cqe *cqe)
noexcept {
88 auto *self =
static_cast<MultiShotOpFinishHandle *
>(data);
89 return self->handle_impl_(cqe);
92 bool handle_impl_(io_uring_cqe *cqe)
noexcept
94 if (cqe->flags & IORING_CQE_F_MORE) {
95 func_(this->cqe_handler_(cqe));
107template <CQEHandlerLike CQEHandler,
typename Func,
typename Receiver>
108class ZeroCopyOpFinishHandle :
public OpFinishHandle<CQEHandler, Receiver> {
110 ZeroCopyOpFinishHandle(CQEHandler cqe_handler, Receiver receiver, Func func)
111 : OpFinishHandle<CQEHandler, Receiver>(std::move(cqe_handler),
112 std::move(receiver)),
113 free_func_(std::move(func)) {
114 this->handle_func_ = handle_static_;
118 static bool handle_static_(
void *data, io_uring_cqe *cqe)
noexcept {
119 auto *self =
static_cast<ZeroCopyOpFinishHandle *
>(data);
120 return self->handle_impl_(cqe);
123 bool handle_impl_(io_uring_cqe *cqe)
noexcept
125 if (cqe->flags & IORING_CQE_F_MORE) {
129 if (cqe->flags & IORING_CQE_F_NOTIF) {
143 void notify_(int32_t res)
noexcept {
152template <
typename Handle>
class HandleBox {
154 template <
typename... Args>
155 HandleBox(Args &&...args) : handle_(std::forward<Args>(args)...) {}
157 CONDY_DELETE_COPY_MOVE(HandleBox);
160 Handle &get() noexcept {
return handle_; }
166template <CQEHandlerLike CQEHandler,
typename Func,
typename Receiver>
167class HandleBox<ZeroCopyOpFinishHandle<CQEHandler, Func, Receiver>> {
169 using Handle = ZeroCopyOpFinishHandle<CQEHandler, Func, Receiver>;
171 template <
typename... Args>
172 HandleBox(Args &&...args)
173 : handle_ptr_(new Handle(std::forward<Args>(args)...)) {}
175 CONDY_DELETE_COPY_MOVE(HandleBox);
178 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.