Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/lib/libpthread PR/47703: Yasushi Oshima: pthread_cond_timedw...



details:   https://anonhg.NetBSD.org/src/rev/5e40b762ccbf
branches:  trunk
changeset: 785739:5e40b762ccbf
user:      christos <christos%NetBSD.org@localhost>
date:      Thu Mar 28 18:07:12 2013 +0000

description:
PR/47703: Yasushi Oshima: pthread_cond_timedwait() does not wait
after call pthread_condattr_setclock(CLOCK_MONOTONIC)

_lwp_park(2) expects a realtime clock, and it gets passed a monotonic
one.  Since monotonic < real, it never sleeps. This patch adjusts
the monotonic clock to be a real one before it passes is to
_lwp_park(2). This is the minimal hacky fix and it will be fixed
properly in _lwp_park(2) in the future.

XXX: pullup to 6.

diffstat:

 lib/libpthread/pthread_cond.c |  27 +++++++++++++++++++++++----
 1 files changed, 23 insertions(+), 4 deletions(-)

diffs (62 lines):

diff -r ddb78770ad71 -r 5e40b762ccbf lib/libpthread/pthread_cond.c
--- a/lib/libpthread/pthread_cond.c     Thu Mar 28 18:06:48 2013 +0000
+++ b/lib/libpthread/pthread_cond.c     Thu Mar 28 18:07:12 2013 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: pthread_cond.c,v 1.59 2013/03/21 16:49:12 christos Exp $       */
+/*     $NetBSD: pthread_cond.c,v 1.60 2013/03/28 18:07:12 christos Exp $       */
 
 /*-
  * Copyright (c) 2001, 2006, 2007, 2008 The NetBSD Foundation, Inc.
@@ -46,7 +46,7 @@
  */
 
 #include <sys/cdefs.h>
-__RCSID("$NetBSD: pthread_cond.c,v 1.59 2013/03/21 16:49:12 christos Exp $");
+__RCSID("$NetBSD: pthread_cond.c,v 1.60 2013/03/28 18:07:12 christos Exp $");
 
 #include <stdlib.h>
 #include <errno.h>
@@ -75,6 +75,13 @@
 __strong_alias(__libc_cond_timedwait,pthread_cond_timedwait)
 __strong_alias(__libc_cond_destroy,pthread_cond_destroy)
 
+static clockid_t
+pthread_cond_getclock(const pthread_cond_t *cond)
+{
+       return cond->ptc_private ? 
+           *(clockid_t *)cond->ptc_private : CLOCK_REALTIME;
+}
+
 int
 pthread_cond_init(pthread_cond_t *cond, const pthread_condattr_t *attr)
 {
@@ -135,6 +142,19 @@
        pthread__error(EPERM, "Mutex not locked in condition wait",
            mutex->ptm_owner != NULL);
        if (abstime != NULL) {
+               /*
+                * XXX: This should be done in the kernel to avoid
+                * extra system calls! 
+                */
+               if (pthread_cond_getclock(cond) == CLOCK_MONOTONIC) {
+                       struct timespec mono, real;
+                       if (clock_gettime(CLOCK_REALTIME, &real) == -1 ||
+                           clock_gettime(CLOCK_MONOTONIC, &mono) == -1)
+                               return errno;
+                       timespecsub(abstime, &mono, &mono);
+                       timespecadd(&mono, &real, &mono);
+                       abstime = &mono;
+               }
                pthread__error(EINVAL, "Invalid wait time", 
                    (abstime->tv_sec >= 0) &&
                    (abstime->tv_nsec >= 0) &&
@@ -390,8 +410,7 @@
                diff.tv_sec = 99999999;
                diff.tv_nsec = 0;
        } else {
-               clockid_t clck = cond->ptc_private ?
-                   *(clockid_t *)cond->ptc_private : CLOCK_REALTIME;
+               clockid_t clck = pthread_cond_getclock(cond);
                clock_gettime(clck, &now);
                if  (timespeccmp(abstime, &now, <))
                        timespecclear(&diff);



Home | Main Index | Thread Index | Old Index