Skip to content

Commit d751dd1

Browse files
Eric Wonggitster
authored andcommitted
hoist out handle_nonblock function for xread and xwrite
At least for me, this improves the readability of xread and xwrite; hopefully allowing missing "continue" statements to be spotted more easily. Signed-off-by: Eric Wong <[email protected]> Signed-off-by: Jeff King <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent ef1cf01 commit d751dd1

File tree

1 file changed

+20
-28
lines changed

1 file changed

+20
-28
lines changed

wrapper.c

Lines changed: 20 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,24 @@ int xopen(const char *path, int oflag, ...)
224224
}
225225
}
226226

227+
static int handle_nonblock(int fd, short poll_events, int err)
228+
{
229+
struct pollfd pfd;
230+
231+
if (err != EAGAIN && err != EWOULDBLOCK)
232+
return 0;
233+
234+
pfd.fd = fd;
235+
pfd.events = poll_events;
236+
237+
/*
238+
* no need to check for errors, here;
239+
* a subsequent read/write will detect unrecoverable errors
240+
*/
241+
poll(&pfd, 1, -1);
242+
return 1;
243+
}
244+
227245
/*
228246
* xread() is the same a read(), but it automatically restarts read()
229247
* operations with a recoverable error (EAGAIN and EINTR). xread()
@@ -239,21 +257,8 @@ ssize_t xread(int fd, void *buf, size_t len)
239257
if (nr < 0) {
240258
if (errno == EINTR)
241259
continue;
242-
if (errno == EAGAIN || errno == EWOULDBLOCK) {
243-
struct pollfd pfd;
244-
pfd.events = POLLIN;
245-
pfd.fd = fd;
246-
/*
247-
* it is OK if this poll() failed; we
248-
* want to leave this infinite loop
249-
* only when read() returns with
250-
* success, or an expected failure,
251-
* which would be checked by the next
252-
* call to read(2).
253-
*/
254-
poll(&pfd, 1, -1);
260+
if (handle_nonblock(fd, POLLIN, errno))
255261
continue;
256-
}
257262
}
258263
return nr;
259264
}
@@ -274,21 +279,8 @@ ssize_t xwrite(int fd, const void *buf, size_t len)
274279
if (nr < 0) {
275280
if (errno == EINTR)
276281
continue;
277-
if (errno == EAGAIN || errno == EWOULDBLOCK) {
278-
struct pollfd pfd;
279-
pfd.events = POLLOUT;
280-
pfd.fd = fd;
281-
/*
282-
* it is OK if this poll() failed; we
283-
* want to leave this infinite loop
284-
* only when write() returns with
285-
* success, or an expected failure,
286-
* which would be checked by the next
287-
* call to write(2).
288-
*/
289-
poll(&pfd, 1, -1);
282+
if (handle_nonblock(fd, POLLOUT, errno))
290283
continue;
291-
}
292284
}
293285

294286
return nr;

0 commit comments

Comments
 (0)