Use monotonic clock to prevent timing issues

This patch replaces the gettimeofday()/timeval-system with
uses of clock_gettime() with a monolithic clock and timespec-structs.
gettimeofday() is not accurate and prone to jumps and POSIX.1-2008
marks it as obsolete. Read more here [0].

The patch should speak for itself and decreases the binary
size for me by almost 200K(!).

[0]: http://blog.habets.pp.se/2010/09/gettimeofday-should-never-be-used-to-measure-time

Signed-off-by: Roberto E. Vargas Caballero <k0ga@shike2.com>
This commit is contained in:
FRIGN 2014-06-23 16:51:51 +02:00 committed by Roberto E. Vargas Caballero
parent 738f555f66
commit 5edeec1b20

27
st.c
View file

@ -76,7 +76,7 @@ char *argv0;
#define LIMIT(x, a, b) (x) = (x) < (a) ? (a) : (x) > (b) ? (b) : (x) #define LIMIT(x, a, b) (x) = (x) < (a) ? (a) : (x) > (b) ? (b) : (x)
#define ATTRCMP(a, b) ((a).mode != (b).mode || (a).fg != (b).fg || (a).bg != (b).bg) #define ATTRCMP(a, b) ((a).mode != (b).mode || (a).fg != (b).fg || (a).bg != (b).bg)
#define IS_SET(flag) ((term.mode & (flag)) != 0) #define IS_SET(flag) ((term.mode & (flag)) != 0)
#define TIMEDIFF(t1, t2) ((t1.tv_sec-t2.tv_sec)*1000 + (t1.tv_usec-t2.tv_usec)/1000) #define TIMEDIFF(t1, t2) ((t1.tv_sec-t2.tv_sec)*1000 + (t1.tv_nsec-t2.tv_nsec)/10E6)
#define CEIL(x) (((x) != (int) (x)) ? (x) + 1 : (x)) #define CEIL(x) (((x) != (int) (x)) ? (x) + 1 : (x))
#define MODBIT(x, set, bit) ((set) ? ((x) |= (bit)) : ((x) &= ~(bit))) #define MODBIT(x, set, bit) ((set) ? ((x) |= (bit)) : ((x) &= ~(bit)))
@ -294,8 +294,8 @@ typedef struct {
char *clip; char *clip;
Atom xtarget; Atom xtarget;
bool alt; bool alt;
struct timeval tclick1; struct timespec tclick1;
struct timeval tclick2; struct timespec tclick2;
} Selection; } Selection;
typedef union { typedef union {
@ -860,7 +860,7 @@ mousereport(XEvent *e) {
void void
bpress(XEvent *e) { bpress(XEvent *e) {
struct timeval now; struct timespec now;
Mousekey *mk; Mousekey *mk;
if(IS_SET(MODE_MOUSE) && !(e->xbutton.state & forceselmod)) { if(IS_SET(MODE_MOUSE) && !(e->xbutton.state & forceselmod)) {
@ -877,7 +877,7 @@ bpress(XEvent *e) {
} }
if(e->xbutton.button == Button1) { if(e->xbutton.button == Button1) {
gettimeofday(&now, NULL); clock_gettime(CLOCK_MONOTONIC, &now);
/* Clear previous selection, logically and visually. */ /* Clear previous selection, logically and visually. */
selclear(NULL); selclear(NULL);
@ -3709,7 +3709,8 @@ run(void) {
int w = xw.w, h = xw.h; int w = xw.w, h = xw.h;
fd_set rfd; fd_set rfd;
int xfd = XConnectionNumber(xw.dpy), xev, blinkset = 0, dodraw = 0; int xfd = XConnectionNumber(xw.dpy), xev, blinkset = 0, dodraw = 0;
struct timeval drawtimeout, *tv = NULL, now, last, lastblink; struct timespec drawtimeout, *tv = NULL, now, last, lastblink;
long deltatime;
/* Waiting for window mapping */ /* Waiting for window mapping */
while(1) { while(1) {
@ -3725,17 +3726,15 @@ run(void) {
ttynew(); ttynew();
cresize(w, h); cresize(w, h);
gettimeofday(&last, NULL); clock_gettime(CLOCK_MONOTONIC, &last);
lastblink = last; lastblink = last;
for(xev = actionfps;;) { for(xev = actionfps;;) {
long deltatime;
FD_ZERO(&rfd); FD_ZERO(&rfd);
FD_SET(cmdfd, &rfd); FD_SET(cmdfd, &rfd);
FD_SET(xfd, &rfd); FD_SET(xfd, &rfd);
if(select(MAX(xfd, cmdfd)+1, &rfd, NULL, NULL, tv) < 0) { if(pselect(MAX(xfd, cmdfd)+1, &rfd, NULL, NULL, tv, NULL) < 0) {
if(errno == EINTR) if(errno == EINTR)
continue; continue;
die("select failed: %s\n", strerror(errno)); die("select failed: %s\n", strerror(errno));
@ -3752,9 +3751,9 @@ run(void) {
if(FD_ISSET(xfd, &rfd)) if(FD_ISSET(xfd, &rfd))
xev = actionfps; xev = actionfps;
gettimeofday(&now, NULL); clock_gettime(CLOCK_MONOTONIC, &now);
drawtimeout.tv_sec = 0; drawtimeout.tv_sec = 0;
drawtimeout.tv_usec = (1000/xfps) * 1000; drawtimeout.tv_nsec = (1000/xfps) * 10E6;
tv = &drawtimeout; tv = &drawtimeout;
dodraw = 0; dodraw = 0;
@ -3789,9 +3788,9 @@ run(void) {
if(blinkset) { if(blinkset) {
if(TIMEDIFF(now, lastblink) \ if(TIMEDIFF(now, lastblink) \
> blinktimeout) { > blinktimeout) {
drawtimeout.tv_usec = 1; drawtimeout.tv_nsec = 1000;
} else { } else {
drawtimeout.tv_usec = (1000 * \ drawtimeout.tv_nsec = (10E6 * \
(blinktimeout - \ (blinktimeout - \
TIMEDIFF(now, TIMEDIFF(now,
lastblink))); lastblink)));