diff options
| author | Joel Klinghed <the_jk@spawned.biz> | 2021-11-17 22:34:57 +0100 |
|---|---|---|
| committer | Joel Klinghed <the_jk@spawned.biz> | 2021-11-17 22:34:57 +0100 |
| commit | 6232d13f5321b87ddf12a1aa36b4545da45f173d (patch) | |
| tree | 23f3316470a14136debd9d02f9e920ca2b06f4cc /src/unique_pipe.cc | |
Travel3: Simple image and video display site
Reads the images and videos from filesystem and builds a site in
memroy.
Diffstat (limited to 'src/unique_pipe.cc')
| -rw-r--r-- | src/unique_pipe.cc | 47 |
1 files changed, 47 insertions, 0 deletions
diff --git a/src/unique_pipe.cc b/src/unique_pipe.cc new file mode 100644 index 0000000..3ab1871 --- /dev/null +++ b/src/unique_pipe.cc @@ -0,0 +1,47 @@ +#include "common.hh" + +#include "io.hh" +#include "unique_pipe.hh" + +#include <fcntl.h> +#include <unistd.h> + +unique_pipe::unique_pipe(bool non_blocking_reader, bool non_blocking_writer) { + int fd[2]; +#if HAVE_PIPE2 + if (non_blocking_reader == non_blocking_writer) { + if (pipe2(fd, non_blocking_reader ? O_NONBLOCK : 0) == 0) { + fd_[0] = unique_fd(fd[0]); + fd_[1] = unique_fd(fd[1]); + } + return; + } +#endif + if (pipe(fd)) + return; + if (non_blocking_reader) + io::make_nonblocking(fd[0]); + if (non_blocking_writer) + io::make_nonblocking(fd[1]); + fd_[0] = unique_fd(fd[0]); + fd_[1] = unique_fd(fd[1]); +} + +unique_pipe::unique_pipe(unique_pipe&& fd) noexcept { + fd_[0] = unique_fd(fd.fd_[0].release()); + fd_[1] = unique_fd(fd.fd_[1].release()); +} + +void unique_pipe::reset() { + fd_[0].reset(); + fd_[1].reset(); +} + +unique_fd unique_pipe::release_reader() { + return std::move(fd_[0]); +} + +unique_fd unique_pipe::release_writer() { + return std::move(fd_[1]); +} + |
