screen-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[screen-devel] [PATCH 4/4] Count timeout in milliseconds instead of usin


From: Amadeusz Sławiński
Subject: [screen-devel] [PATCH 4/4] Count timeout in milliseconds instead of using struct timeval
Date: Sun, 3 Nov 2019 00:32:00 +0100

poll() accepts timeout in milliseconds, so there is no need to keep
timeout in struct timeval.

Signed-off-by: Amadeusz Sławiński <address@hidden>
---
 src/display.c |  4 ++--
 src/sched.c   | 40 +++++++++++++++-------------------------
 src/sched.h   |  2 +-
 src/winmsg.c  |  5 ++---
 4 files changed, 20 insertions(+), 31 deletions(-)

diff --git a/src/display.c b/src/display.c
index c4d0f9eb..3a6342d5 100644
--- a/src/display.c
+++ b/src/display.c
@@ -1705,7 +1705,7 @@ void RefreshHStatus(void)
                         (D_HS && D_has_hstatus == HSTATUS_HS && D_WS > 0) ? 
D_WS : D_width - !D_CLP + extrabytes, &D_hstatusev, 0);
        if (buf && *buf) {
                ShowHStatus(buf);
-               if (D_has_hstatus != HSTATUS_IGNORE && 
D_hstatusev.timeout.tv_sec)
+               if (D_has_hstatus != HSTATUS_IGNORE && D_hstatusev.timeout)
                        evenq(&D_hstatusev);
        } else
                ShowHStatus(NULL);
@@ -1774,7 +1774,7 @@ void RefreshLine(int y, int from, int to, int isblank)
                                    MakeWinMsgEv(NULL, captionstring, p, '%',
                                                 cv->c_xe - cv->c_xs + 
(cv->c_xe + 1 < D_width
                                                                        || 
D_CLP) + extrabytes, &cv->c_captev, 0);
-                               if (cv->c_captev.timeout.tv_sec)
+                               if (cv->c_captev.timeout)
                                        evenq(&cv->c_captev);
                                xx = to > cv->c_xe ? cv->c_xe : to;
                                l = strlen(buf);
diff --git a/src/sched.c b/src/sched.c
index 6d2f2ac0..24b5fdb5 100644
--- a/src/sched.c
+++ b/src/sched.c
@@ -110,13 +110,11 @@ static Event *calctimo(void)
 
        if ((min = tevs) == NULL)
                return NULL;
-       mins = min->timeout.tv_sec;
+       mins = min->timeout;
        for (ev = tevs->next; ev; ev = ev->next) {
-               if (mins < ev->timeout.tv_sec)
-                       continue;
-               if (mins > ev->timeout.tv_sec || min->timeout.tv_usec > 
ev->timeout.tv_usec) {
+               if (mins > ev->timeout) {
                        min = ev;
-                       mins = ev->timeout.tv_sec;
+                       mins = ev->timeout;
                }
        }
        return min;
@@ -126,25 +124,19 @@ void sched(void)
 {
        Event *ev;
        Event *timeoutev = NULL;
-       struct timeval timeout;
+       int timeout;
        int i, n;
 
        for (;;) {
                if (calctimeout)
                        timeoutev = calctimo();
                if (timeoutev) {
-                       gettimeofday(&timeout, NULL);
+                       struct timeval now;
+                       gettimeofday(&now, NULL);
                        /* tp - timeout */
-                       timeout.tv_sec = timeoutev->timeout.tv_sec - 
timeout.tv_sec;
-                       timeout.tv_usec = timeoutev->timeout.tv_usec - 
timeout.tv_usec;
-                       if (timeout.tv_usec < 0) {
-                               timeout.tv_usec += 1000000;
-                               timeout.tv_sec--;
-                       }
-                       if (timeout.tv_sec < 0) {
-                               timeout.tv_usec = 0;
-                               timeout.tv_sec = 0;
-                       }
+                       timeout = timeoutev->timeout - (now.tv_sec * 1000 + 
now.tv_usec / 1000);
+                       if (timeout < 0)
+                               timeout = 0;
                }
 
                memset(pfd, 0, sizeof(struct pollfd) * pfd_cnt);
@@ -164,7 +156,7 @@ skip:
                                i++;
                }
 
-               n = poll(pfd, i, timeoutev ? (timeout.tv_sec * 1000 + 
timeout.tv_usec / 1000) : 0);
+               n = poll(pfd, i, timeoutev ? timeout : 0);
                if (n < 0) {
                        if (errno != EINTR) {
                                Panic(errno, "poll");
@@ -213,13 +205,11 @@ skip:
 
 void SetTimeout(Event *ev, int timo)
 {
-       gettimeofday(&ev->timeout, NULL);
-       ev->timeout.tv_sec += timo / 1000;
-       ev->timeout.tv_usec += (timo % 1000) * 1000;
-       if (ev->timeout.tv_usec > 1000000) {
-               ev->timeout.tv_usec -= 1000000;
-               ev->timeout.tv_sec++;
-       }
+       struct timeval now;
+       gettimeofday(&now, NULL);
+
+       ev->timeout = (now.tv_sec * 1000 + now.tv_usec / 1000) + timo;
+
        if (ev->queued)
                calctimeout = 1;
 }
diff --git a/src/sched.h b/src/sched.h
index a6187815..e7f6f197 100644
--- a/src/sched.h
+++ b/src/sched.h
@@ -48,7 +48,7 @@ struct Event {
        int fd;
        EventType type;
        int priority;
-       struct timeval timeout;
+       int timeout;            /* timeout in milliseconds */
        bool queued;            /* in evs queue */
        int *condpos;           /* only active if condpos - condneg > 0 */
        int *condneg;
diff --git a/src/winmsg.c b/src/winmsg.c
index 8287358f..b9b44119 100644
--- a/src/winmsg.c
+++ b/src/winmsg.c
@@ -674,8 +674,7 @@ char *MakeWinMsgEv(WinMsgBuf *winmsg, char *str, Window 
*win,
        }
        if (ev) {
                evdeq(ev);      /* just in case */
-               ev->timeout.tv_sec = 0;
-               ev->timeout.tv_usec = 0;
+               ev->timeout = 0;
        }
        if (ev && tick) {
                now.tv_usec = 100000;
@@ -683,7 +682,7 @@ char *MakeWinMsgEv(WinMsgBuf *winmsg, char *str, Window 
*win,
                        now.tv_sec++;
                else
                        now.tv_sec += tick - (now.tv_sec % tick);
-               ev->timeout = now;
+               ev->timeout = (now.tv_sec * 1000 + now.tv_usec / 1000);
        }
 
        free(cond);
-- 
2.23.0




reply via email to

[Prev in Thread] Current Thread [Next in Thread]