summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAmadeusz Sławiński <amade@asmblr.net>2019-11-03 00:32:00 +0100
committerAmadeusz Sławiński <amade@asmblr.net>2019-12-28 13:41:59 +0100
commitbde659b6bf53348f0550d79e170d3dd396e34d09 (patch)
tree711bd828c823a75528bbeb4db30de9b31727defc
parent8becc4d29a12688b39312639f56e1aeeb76f96e2 (diff)
downloadscreen-bde659b6bf53348f0550d79e170d3dd396e34d09.tar.gz
Count timeout in milliseconds instead of using struct timeval
poll() accepts timeout in milliseconds, so there is no need to keep timeout in struct timeval. Signed-off-by: Amadeusz Sławiński <amade@asmblr.net>
-rw-r--r--src/display.c4
-rw-r--r--src/sched.c40
-rw-r--r--src/sched.h2
-rw-r--r--src/winmsg.c5
4 files changed, 20 insertions, 31 deletions
diff --git a/src/display.c b/src/display.c
index 52cb760..0ec6c33 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 6ff92ae..39d9d09 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 b7b0ca1..5bd1b3a 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 bccadd5..79936e7 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);