replace select() with poll() (#270)

usage of select (3) in modern programs is typically discouraged.
this simply replaces the select call with poll (3) instead.

and since poll conveniently ignores negative fds, this also reduces
needs for some special casing.

this also handles error if they occur, while old implementation didn't.
other than the error handling, no change in functionality should occur.
This commit is contained in:
N-R-K 2022-04-28 03:12:15 +00:00 committed by GitHub
parent 5bb1df4af3
commit 6922d5d01b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 14 additions and 18 deletions

31
main.c
View file

@ -29,7 +29,7 @@
#include <errno.h> #include <errno.h>
#include <locale.h> #include <locale.h>
#include <signal.h> #include <signal.h>
#include <sys/select.h> #include <poll.h>
#include <sys/stat.h> #include <sys/stat.h>
#include <sys/wait.h> #include <sys/wait.h>
#include <time.h> #include <time.h>
@ -701,9 +701,9 @@ static void on_buttonpress(const XButtonEvent *bev)
static void run(void) static void run(void)
{ {
int xfd; enum { FD_X, FD_INFO, FD_ARL, FD_CNT };
fd_set fds; struct pollfd pfd[FD_CNT];
struct timeval timeout; struct timeval timeout = {0};
const struct timespec ten_ms = {0, 10000000}; const struct timespec ten_ms = {0, 10000000};
bool discard, init_thumb, load_thumb, to_set; bool discard, init_thumb, load_thumb, to_set;
XEvent ev, nextev; XEvent ev, nextev;
@ -730,21 +730,16 @@ static void run(void)
if (!tns_load(&tns, tns.initnext, false, true)) if (!tns_load(&tns, tns.initnext, false, true))
remove_file(tns.initnext, false); remove_file(tns.initnext, false);
} else { } else {
xfd = ConnectionNumber(win.env.dpy); pfd[FD_X].fd = ConnectionNumber(win.env.dpy);
FD_ZERO(&fds); pfd[FD_INFO].fd = info.fd;
FD_SET(xfd, &fds); pfd[FD_ARL].fd = arl.fd;
if (info.fd != -1) { pfd[FD_X].events = pfd[FD_INFO].events = pfd[FD_ARL].events = POLLIN;
FD_SET(info.fd, &fds);
xfd = MAX(xfd, info.fd); if (poll(pfd, ARRLEN(pfd), to_set ? TV_TO_MS(&timeout) : -1) < 0)
} continue;
if (arl.fd != -1) { if (pfd[FD_INFO].revents & POLLIN)
FD_SET(arl.fd, &fds);
xfd = MAX(xfd, arl.fd);
}
select(xfd + 1, &fds, 0, 0, to_set ? &timeout : NULL);
if (info.fd != -1 && FD_ISSET(info.fd, &fds))
read_info(); read_info();
if (arl.fd != -1 && FD_ISSET(arl.fd, &fds)) { if (pfd[FD_ARL].revents & POLLIN) {
if (arl_handle(&arl)) { if (arl_handle(&arl)) {
/* when too fast, imlib2 can't load the image */ /* when too fast, imlib2 can't load the image */
nanosleep(&ten_ms, NULL); nanosleep(&ten_ms, NULL);

View file

@ -44,6 +44,7 @@
#define TV_DIFF(t1,t2) (((t1)->tv_sec - (t2)->tv_sec ) * 1000 + \ #define TV_DIFF(t1,t2) (((t1)->tv_sec - (t2)->tv_sec ) * 1000 + \
((t1)->tv_usec - (t2)->tv_usec) / 1000) ((t1)->tv_usec - (t2)->tv_usec) / 1000)
#define TV_TO_MS(tv) (((tv)->tv_sec * 1000) + ((tv)->tv_usec / 1000))
#define TV_SET_MSEC(tv,t) { \ #define TV_SET_MSEC(tv,t) { \
(tv)->tv_sec = (t) / 1000; \ (tv)->tv_sec = (t) / 1000; \