system/FileDescriptor: add method CreatePipeNonBlock()

This commit is contained in:
Max Kellermann 2017-08-11 09:15:22 +02:00
parent 2119e4fd3e
commit 9056dcaf7d
2 changed files with 30 additions and 0 deletions

View File

@ -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
{

View File

@ -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.
*/