diff options
author | Max Kellermann <max@musicpd.org> | 2017-08-11 09:15:22 +0200 |
---|---|---|
committer | Max Kellermann <max@musicpd.org> | 2017-08-11 09:15:22 +0200 |
commit | 9056dcaf7d9234977362f3219e27fb997f174f52 (patch) | |
tree | 0ff956360461a1bdd32b3e0540985105f370d091 /src/system | |
parent | 2119e4fd3ef47d82d88b69946a354840b0b909e5 (diff) |
system/FileDescriptor: add method CreatePipeNonBlock()
Diffstat (limited to 'src/system')
-rw-r--r-- | src/system/FileDescriptor.cxx | 27 | ||||
-rw-r--r-- | src/system/FileDescriptor.hxx | 3 |
2 files changed, 30 insertions, 0 deletions
diff --git a/src/system/FileDescriptor.cxx b/src/system/FileDescriptor.cxx index d6d2a3b85..0e2d3cda5 100644 --- a/src/system/FileDescriptor.cxx +++ b/src/system/FileDescriptor.cxx @@ -125,6 +125,33 @@ FileDescriptor::CreatePipe(FileDescriptor &r, FileDescriptor &w) noexcept #ifndef _WIN32 +bool +FileDescriptor::CreatePipeNonBlock(FileDescriptor &r, + FileDescriptor &w) noexcept +{ + int fds[2]; + +#ifdef HAVE_PIPE2 + const int flags = O_CLOEXEC|O_NONBLOCK; + const int result = pipe2(fds, flags); +#else + const int result = pipe(fds); +#endif + + if (result < 0) + return false; + + r = FileDescriptor(fds[0]); + w = FileDescriptor(fds[1]); + +#ifndef HAVE_PIPE2 + r.SetNonBlocking(); + w.SetNonBlocking(); +#endif + + return true; +} + void FileDescriptor::SetNonBlocking() noexcept { diff --git a/src/system/FileDescriptor.hxx b/src/system/FileDescriptor.hxx index 4c0e1c3d0..7a5af768c 100644 --- a/src/system/FileDescriptor.hxx +++ b/src/system/FileDescriptor.hxx @@ -115,6 +115,9 @@ public: static bool CreatePipe(FileDescriptor &r, FileDescriptor &w) noexcept; #ifndef _WIN32 + static bool CreatePipeNonBlock(FileDescriptor &r, + FileDescriptor &w) noexcept; + /** * Enable non-blocking mode on this file descriptor. */ |