]> git.pond.sub.org Git - empserver/commitdiff
Fix empth_sleep() for argument in the past and pthreads
authorMarkus Armbruster <armbru@pond.sub.org>
Sun, 14 Jun 2009 15:49:41 +0000 (17:49 +0200)
committerMarkus Armbruster <armbru@pond.sub.org>
Sun, 19 Jul 2009 18:11:52 +0000 (14:11 -0400)
Calculation of sleep duration suffered integer underflow for unsigned
time_t and arguments in the past.  This made empth_sleep() sleep for
"a few" years instead of not at all.

src/lib/empthread/pthread.c

index 319b272e433b384c2a12ffe89f4ea1ece4d4691b..6d08611f55de45922f73e736e3aaa6044f9d3afe 100644 (file)
@@ -345,14 +345,16 @@ int
 empth_sleep(time_t until)
 {
     empth_t *ctx = pthread_getspecific(ctx_key);
+    time_t now;
     struct timeval tv;
     int res;
 
-    empth_status("going to sleep %ld sec", until - time(0));
     pthread_mutex_unlock(&mtx_ctxsw);
     do {
-       tv.tv_sec = until - time(NULL);
+       now = time(NULL);
+       tv.tv_sec = until >= now ? until - now : 0;
        tv.tv_usec = 0;
+       empth_status("going to sleep %ld sec", (long)tv.tv_sec);
        res = select(0, NULL, NULL, NULL, &tv);
     } while (res < 0 && !ctx->wakeup);
     empth_status("sleep done. Waiting for lock");