[hackers] [st] Use monotonic clock to prevent timing issues || FRIGN

From: <git_AT_suckless.org>
Date: Wed, 25 Jun 2014 19:42:50 +0200

commit 5edeec1b20fcb5900d4f1408594d1e76b6c544f0
Author: FRIGN <dev_AT_frign.de>
Date: Mon Jun 23 16:51:51 2014 +0200

    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_AT_shike2.com>

diff --git a/st.c b/st.c
index 26053b0..17142a4 100644
--- a/st.c
+++ b/st.c
_AT_@ -76,7 +76,7 @@ char *argv0;
 #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 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 MODBIT(x, set, bit) ((set) ? ((x) |= (bit)) : ((x) &= ~(bit)))
 
_AT_@ -294,8 +294,8 @@ typedef struct {
         char *clip;
         Atom xtarget;
         bool alt;
- struct timeval tclick1;
- struct timeval tclick2;
+ struct timespec tclick1;
+ struct timespec tclick2;
 } Selection;
 
 typedef union {
_AT_@ -860,7 +860,7 @@ mousereport(XEvent *e) {
 
 void
 bpress(XEvent *e) {
- struct timeval now;
+ struct timespec now;
         Mousekey *mk;
 
         if(IS_SET(MODE_MOUSE) && !(e->xbutton.state & forceselmod)) {
_AT_@ -877,7 +877,7 @@ bpress(XEvent *e) {
         }
 
         if(e->xbutton.button == Button1) {
- gettimeofday(&now, NULL);
+ clock_gettime(CLOCK_MONOTONIC, &now);
 
                 /* Clear previous selection, logically and visually. */
                 selclear(NULL);
_AT_@ -3709,7 +3709,8 @@ run(void) {
         int w = xw.w, h = xw.h;
         fd_set rfd;
         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 */
         while(1) {
_AT_@ -3725,17 +3726,15 @@ run(void) {
         ttynew();
         cresize(w, h);
 
- gettimeofday(&last, NULL);
+ clock_gettime(CLOCK_MONOTONIC, &last);
         lastblink = last;
 
         for(xev = actionfps;;) {
- long deltatime;
-
                 FD_ZERO(&rfd);
                 FD_SET(cmdfd, &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)
                                 continue;
                         die("select failed: %s
", strerror(errno));
_AT_@ -3752,9 +3751,9 @@ run(void) {
                 if(FD_ISSET(xfd, &rfd))
                         xev = actionfps;
 
- gettimeofday(&now, NULL);
+ clock_gettime(CLOCK_MONOTONIC, &now);
                 drawtimeout.tv_sec = 0;
- drawtimeout.tv_usec = (1000/xfps) * 1000;
+ drawtimeout.tv_nsec = (1000/xfps) * 10E6;
                 tv = &drawtimeout;
 
                 dodraw = 0;
_AT_@ -3789,9 +3788,9 @@ run(void) {
                                 if(blinkset) {
                                         if(TIMEDIFF(now, lastblink) \
> blinktimeout) {
- drawtimeout.tv_usec = 1;
+ drawtimeout.tv_nsec = 1000;
                                         } else {
- drawtimeout.tv_usec = (1000 * \
+ drawtimeout.tv_nsec = (10E6 * \
                                                         (blinktimeout - \
                                                         TIMEDIFF(now,
                                                                 lastblink)));
Received on Wed Jun 25 2014 - 19:42:50 CEST

This archive was generated by hypermail 2.3.0 : Wed Jun 25 2014 - 19:48:16 CEST