changeset 49:6a3726fa7aad

Reduce times of invoking gettimeofday()
author Thinker K.F. Li <thinker@branda.to>
date Sat, 09 Aug 2008 18:34:24 +0800
parents bdf711cbf0fb
children c986e45c1e91
files src/X_main.c
diffstat 1 files changed, 17 insertions(+), 12 deletions(-) [+]
line wrap: on
line diff
--- a/src/X_main.c	Sat Aug 09 18:26:20 2008 +0800
+++ b/src/X_main.c	Sat Aug 09 18:34:24 2008 +0800
@@ -90,23 +90,25 @@
     int nfds;
     struct timeval tmo;
     mb_timeval_t mb_tmo;
-    int r;
+    int r, r1;
 
     XSelectInput(display, win, PointerMotionMask | ExposureMask);
     XFlush(display);
 
     xcon = XConnectionNumber(display);
     nfds = xcon + 1;
+
+    r = gettimeofday(&tmo, NULL);
+    if(r == -1) {
+	perror("gettimeofday");
+	return;
+    }
+    MB_TIMEVAL_SET(&mb_tmo, tmo.tv_sec, tmo.tv_usec);
+
     while(1) {
 	FD_ZERO(&rds);
 	FD_SET(xcon, &rds);
-	r = gettimeofday(&tmo, NULL);
-	if(r == -1) {
-	    perror("gettimeofday");
-	    return;
-	}
 
-	MB_TIMEVAL_SET(&mb_tmo, tmo.tv_sec, tmo.tv_usec);
 	r = mb_tman_next_timeout(tman, &mb_tmo, &mb_tmo);
 	if(r != OK)
 	    r = select(nfds, &rds, NULL, NULL, NULL);
@@ -120,12 +122,15 @@
 	    perror("select");
 	    return;
 	}
+
+	r1 = gettimeofday(&tmo, NULL);
+	if(r1 == -1) {
+	    perror("gettimeofday");
+	    return;
+	}
+	MB_TIMEVAL_SET(&mb_tmo, tmo.tv_sec, tmo.tv_usec);
+
 	if(r == 0) {
-	    r = gettimeofday(&tmo, NULL);
-	    if(r == -1) {
-		perror("gettimeofday");
-		return;
-	    }
 	    MB_TIMEVAL_SET(&mb_tmo, tmo.tv_sec, tmo.tv_usec);
 	    mb_tman_handle_timeout(tman, &mb_tmo);
 	    rdman_redraw_changed(rdman);