Krolyxon
Commit 6922d5d

Branches

Refs →

nsxiv6922d5d

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.
Author
N-R-K
Date
2022-04-28 03:12 +0000
Committer
GitHub · 2022-04-28 03:12 +0000
Parent
5bb1df4
Commit
6922d5d01b03b0f584a411803d2da45b07f5af01

+14 −18

2 files changed

  1. M main.c +13 −18
  2. M nsxiv.h +1 −0
M main.c View
@@ -29,7 +29,7 @@
2929 #include <errno.h>
3030 #include <locale.h>
3131 #include <signal.h>
32−#include <sys/select.h>
32+#include <poll.h>
3333 #include <sys/stat.h>
3434 #include <sys/wait.h>
3535 #include <time.h>
@@ -701,9 +701,9 @@ static void on_buttonpress(const XButtonEvent *bev)
701701
702702 static void run(void)
703703 {
704− int xfd;
705− fd_set fds;
706− struct timeval timeout;
704+ enum { FD_X, FD_INFO, FD_ARL, FD_CNT };
705+ struct pollfd pfd[FD_CNT];
706+ struct timeval timeout = {0};
707707 const struct timespec ten_ms = {0, 10000000};
708708 bool discard, init_thumb, load_thumb, to_set;
709709 XEvent ev, nextev;
@@ -730,21 +730,16 @@ static void run(void)
730730 if (!tns_load(&tns, tns.initnext, false, true))
731731 remove_file(tns.initnext, false);
732732 } else {
733− xfd = ConnectionNumber(win.env.dpy);
734− FD_ZERO(&fds);
735− FD_SET(xfd, &fds);
736− if (info.fd != -1) {
737− FD_SET(info.fd, &fds);
738− xfd = MAX(xfd, info.fd);
739− }
740− if (arl.fd != -1) {
741− FD_SET(arl.fd, &fds);
742− xfd = MAX(xfd, arl.fd);
743− }
744− select(xfd + 1, &fds, 0, 0, to_set ? &timeout : NULL);
745− if (info.fd != -1 && FD_ISSET(info.fd, &fds))
733+ pfd[FD_X].fd = ConnectionNumber(win.env.dpy);
734+ pfd[FD_INFO].fd = info.fd;
735+ pfd[FD_ARL].fd = arl.fd;
736+ pfd[FD_X].events = pfd[FD_INFO].events = pfd[FD_ARL].events = POLLIN;
737+
738+ if (poll(pfd, ARRLEN(pfd), to_set ? TV_TO_MS(&timeout) : -1) < 0)
739+ continue;
740+ if (pfd[FD_INFO].revents & POLLIN)
746741 read_info();
747− if (arl.fd != -1 && FD_ISSET(arl.fd, &fds)) {
742+ if (pfd[FD_ARL].revents & POLLIN) {
748743 if (arl_handle(&arl)) {
749744 /* when too fast, imlib2 can't load the image */
750745 nanosleep(&ten_ms, NULL);
M nsxiv.h View
@@ -44,6 +44,7 @@
4444
4545 #define TV_DIFF(t1,t2) (((t1)->tv_sec - (t2)->tv_sec ) * 1000 + \
4646 ((t1)->tv_usec - (t2)->tv_usec) / 1000)
47+#define TV_TO_MS(tv) (((tv)->tv_sec * 1000) + ((tv)->tv_usec / 1000))
4748
4849 #define TV_SET_MSEC(tv,t) { \
4950 (tv)->tv_sec = (t) / 1000; \