URING++
Loading...
Searching...
No Matches
pipe.h
1#pragma once
2
3#include <cassert>
4#include <memory>
5#include <unistd.h>
6
7#include "uringpp/error.h"
8#include "uringpp/event_loop.h"
9namespace uringpp {
10
15class pipe {
16 std::shared_ptr<event_loop> loop_;
17 int fds_[2];
18
19public:
25 pipe(std::shared_ptr<event_loop> loop) : loop_(loop) {
26 check_errno(::pipe(fds_), "failed to create pipe");
27 assert(fds_[0] > 0);
28 assert(fds_[1] > 0);
29 }
30
36 pipe(pipe &&other) noexcept
37 : loop_(std::move(other.loop_)), fds_{std::exchange(other.fds_[0], -1),
38 std::exchange(other.fds_[1], -1)} {}
39
46 int readable_fd() const {
47 assert(fds_[0] > 0);
48 return fds_[0];
49 }
50
56 int writable_fd() const {
57 assert(fds_[1] > 0);
58 return fds_[1];
59 }
60
67 co_await loop_->close(readable_fd());
68 fds_[0] = -1;
69 }
70
77 co_await loop_->close(writable_fd());
78 fds_[1] = -1;
79 }
80
87 if (fds_[0] > 0) {
88 co_await loop_->close(fds_[0]);
89 fds_[0] = -1;
90 }
91 if (fds_[1] > 0) {
92 co_await loop_->close(fds_[1]);
93 fds_[1] = -1;
94 }
95 }
96
103 if (fds_[0] > 0) {
104 loop_->close_detach(fds_[0]);
105 }
106 if (fds_[1] > 0) {
107 loop_->close_detach(fds_[1]);
108 }
109 }
110};
111
112} // namespace uringpp
A pipe.
Definition: pipe.h:15
pipe(pipe &&other) noexcept
Move construct a new pipe object.
Definition: pipe.h:36
int writable_fd() const
Get the write end of the pipe.
Definition: pipe.h:56
~pipe()
Destroy the pipe object. If any end of the pipe is still open, it will be closed.
Definition: pipe.h:102
int readable_fd() const
Get the read end of the pipe.
Definition: pipe.h:46
task< void > close_write()
Close the write end of the pipe.
Definition: pipe.h:76
task< void > close_read()
Close the read end of the pipe.
Definition: pipe.h:66
pipe(std::shared_ptr< event_loop > loop)
Construct a new pipe object.
Definition: pipe.h:25
task< void > close()
Close the read and write ends of the pipe.
Definition: pipe.h:86
Definition: task.h:53