Skip to content

Commit 10f7433

Browse files
peffgitster
authored andcommitted
compat: add function to enable nonblocking pipes
We'd like to be able to make some of our pipes nonblocking so that poll() can be used effectively, but O_NONBLOCK isn't portable. Let's introduce a compat wrapper so this can be abstracted for each platform. The interface is as narrow as possible to let platforms do what's natural there (rather than having to implement fcntl() and a fake O_NONBLOCK for example, or having to handle other types of descriptors). The next commit will add Windows support, at which point we should be covering all platforms in practice. But if we do find some other platform without O_NONBLOCK, we'll return ENOSYS. Arguably we could just trigger a build-time #error in this case, which would catch the problem earlier. But since we're not planning to use this compat wrapper in many code paths, a seldom-seen runtime error may be friendlier for such a platform than blocking compilation completely. Our test suite would still notice it. Signed-off-by: Jeff King <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent ad60ddd commit 10f7433

File tree

3 files changed

+33
-0
lines changed

3 files changed

+33
-0
lines changed

Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -910,6 +910,7 @@ LIB_OBJS += combine-diff.o
910910
LIB_OBJS += commit-graph.o
911911
LIB_OBJS += commit-reach.o
912912
LIB_OBJS += commit.o
913+
LIB_OBJS += compat/nonblock.o
913914
LIB_OBJS += compat/obstack.o
914915
LIB_OBJS += compat/terminal.o
915916
LIB_OBJS += compat/zlib-uncompress2.o

compat/nonblock.c

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#include "git-compat-util.h"
2+
#include "nonblock.h"
3+
4+
#ifdef O_NONBLOCK
5+
6+
int enable_pipe_nonblock(int fd)
7+
{
8+
int flags = fcntl(fd, F_GETFL);
9+
if (flags < 0)
10+
return -1;
11+
flags |= O_NONBLOCK;
12+
return fcntl(fd, F_SETFL, flags);
13+
}
14+
15+
#else
16+
17+
int enable_pipe_nonblock(int fd)
18+
{
19+
errno = ENOSYS;
20+
return -1;
21+
}
22+
23+
#endif

compat/nonblock.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#ifndef COMPAT_NONBLOCK_H
2+
#define COMPAT_NONBLOCK_H
3+
4+
/*
5+
* Enable non-blocking I/O for the pipe specified by the passed-in descriptor.
6+
*/
7+
int enable_pipe_nonblock(int fd);
8+
9+
#endif

0 commit comments

Comments
 (0)