]> git.sur5r.net Git - i3/i3status/blobdiff - i3status.c
Merge pull request #95 from Watcom/nanosleep
[i3/i3status] / i3status.c
index 37814256a78586e71a06d891c0246d1c99adfcc3..ec204e59140572b3e957d98b1890bba625b7dacb 100644 (file)
@@ -62,8 +62,7 @@ cfg_t *cfg, *cfg_general, *cfg_section;
 
 void **cur_instance;
 
-pthread_cond_t i3status_sleep_cond = PTHREAD_COND_INITIALIZER;
-pthread_mutex_t i3status_sleep_mutex = PTHREAD_MUTEX_INITIALIZER;
+pthread_t main_thread;
 
 /*
  * Set the exit_upon_signal flag, because one cannot do anything in a safe
@@ -452,6 +451,7 @@ int main(int argc, char *argv[]) {
     struct sigaction action;
     memset(&action, 0, sizeof(struct sigaction));
     action.sa_handler = fatalsig;
+    main_thread = pthread_self();
 
     /* Exit upon SIGPIPE because when we have nowhere to write to, gathering system
      * information is pointless. Also exit explicitly on SIGTERM and SIGINT because
@@ -577,7 +577,6 @@ int main(int argc, char *argv[]) {
     char buffer[4096];
 
     void **per_instance = calloc(cfg_size(cfg, "order"), sizeof(*per_instance));
-    pthread_mutex_lock(&i3status_sleep_mutex);
 
     while (1) {
         if (exit_upon_signal) {
@@ -713,21 +712,13 @@ int main(int argc, char *argv[]) {
         fflush(stdout);
 
         /* To provide updates on every full second (as good as possible)
-         * we don’t use sleep(interval) but we sleep until the next second.
-         * We also align to 60 seconds modulo interval such
+         * we don’t use sleep(interval) but we sleep until the next
+         * second (with microsecond precision) plus (interval-1)
+         * seconds. We also align to 60 seconds modulo interval such
          * that we start with :00 on every new minute. */
-        struct timespec ts;
-#if defined(__APPLE__)
-        gettimeofday(&tv, NULL);
-        ts.tv_sec = tv.tv_sec;
-#else
-        clock_gettime(CLOCK_REALTIME, &ts);
-#endif
-        ts.tv_sec += interval - (ts.tv_sec % interval);
-        ts.tv_nsec = 0;
-
-        /* Sleep to absolute time 'ts', unless the condition
-         * 'i3status_sleep_cond' is signaled from another thread */
-        pthread_cond_timedwait(&i3status_sleep_cond, &i3status_sleep_mutex, &ts);
+        struct timeval current_timeval;
+        gettimeofday(&current_timeval, NULL);
+        struct timespec ts = {interval - 1 - (current_timeval.tv_sec % interval), (10e5 - current_timeval.tv_usec) * 1000};
+        nanosleep(&ts, NULL);
     }
 }