Blame SOURCES/00370-GIL-monotonic-clock.patch

43e83c
diff --git a/Makefile.pre.in b/Makefile.pre.in
43e83c
index 8da1965..9864fe2 100644
43e83c
--- a/Makefile.pre.in
43e83c
+++ b/Makefile.pre.in
43e83c
@@ -884,7 +884,8 @@ regen-opcode-targets:
43e83c
 		$(srcdir)/Python/opcode_targets.h.new
43e83c
 	$(UPDATE_FILE) $(srcdir)/Python/opcode_targets.h $(srcdir)/Python/opcode_targets.h.new
43e83c
 
43e83c
-Python/ceval.o: $(srcdir)/Python/opcode_targets.h $(srcdir)/Python/ceval_gil.h
43e83c
+Python/ceval.o: $(srcdir)/Python/opcode_targets.h $(srcdir)/Python/ceval_gil.h \
43e83c
+		$(srcdir)/Python/condvar.h
43e83c
 
43e83c
 Python/frozen.o: $(srcdir)/Python/importlib.h $(srcdir)/Python/importlib_external.h
43e83c
 
43e83c
@@ -1706,7 +1707,7 @@ patchcheck: @DEF_MAKE_RULE@
43e83c
 
43e83c
 # Dependencies
43e83c
 
43e83c
-Python/thread.o: @THREADHEADERS@
43e83c
+Python/thread.o: @THREADHEADERS@ $(srcdir)/Python/condvar.h
43e83c
 
43e83c
 # Declare targets that aren't real files
43e83c
 .PHONY: all build_all sharedmods check-clean-src oldsharedmods test quicktest
43e83c
diff --git a/Python/ceval.c b/Python/ceval.c
43e83c
index 0b30cc1..3f1300c 100644
43e83c
--- a/Python/ceval.c
43e83c
+++ b/Python/ceval.c
43e83c
@@ -232,6 +232,7 @@ PyEval_InitThreads(void)
43e83c
 {
43e83c
     if (gil_created())
43e83c
         return;
43e83c
+    PyThread_init_thread();
43e83c
     create_gil();
43e83c
     take_gil(PyThreadState_GET());
43e83c
     main_thread = PyThread_get_thread_ident();
43e83c
diff --git a/Python/condvar.h b/Python/condvar.h
43e83c
index 9a71b17..39a420f 100644
43e83c
--- a/Python/condvar.h
43e83c
+++ b/Python/condvar.h
43e83c
@@ -59,20 +59,6 @@
43e83c
 
43e83c
 #include <pthread.h>
43e83c
 
43e83c
-#define PyCOND_ADD_MICROSECONDS(tv, interval) \
43e83c
-do { /* TODO: add overflow and truncation checks */ \
43e83c
-    tv.tv_usec += (long) interval; \
43e83c
-    tv.tv_sec += tv.tv_usec / 1000000; \
43e83c
-    tv.tv_usec %= 1000000; \
43e83c
-} while (0)
43e83c
-
43e83c
-/* We assume all modern POSIX systems have gettimeofday() */
43e83c
-#ifdef GETTIMEOFDAY_NO_TZ
43e83c
-#define PyCOND_GETTIMEOFDAY(ptv) gettimeofday(ptv)
43e83c
-#else
43e83c
-#define PyCOND_GETTIMEOFDAY(ptv) gettimeofday(ptv, (struct timezone *)NULL)
43e83c
-#endif
43e83c
-
43e83c
 /* The following functions return 0 on success, nonzero on error */
43e83c
 #define PyMUTEX_T pthread_mutex_t
43e83c
 #define PyMUTEX_INIT(mut)       pthread_mutex_init((mut), NULL)
43e83c
@@ -81,32 +67,30 @@ do { /* TODO: add overflow and truncation checks */ \
43e83c
 #define PyMUTEX_UNLOCK(mut)     pthread_mutex_unlock(mut)
43e83c
 
43e83c
 #define PyCOND_T pthread_cond_t
43e83c
-#define PyCOND_INIT(cond)       pthread_cond_init((cond), NULL)
43e83c
+#define PyCOND_INIT(cond)       _PyThread_cond_init(cond)
43e83c
 #define PyCOND_FINI(cond)       pthread_cond_destroy(cond)
43e83c
 #define PyCOND_SIGNAL(cond)     pthread_cond_signal(cond)
43e83c
 #define PyCOND_BROADCAST(cond)  pthread_cond_broadcast(cond)
43e83c
 #define PyCOND_WAIT(cond, mut)  pthread_cond_wait((cond), (mut))
43e83c
 
43e83c
+/* These private functions are implemented in Python/thread_pthread.h */
43e83c
+int _PyThread_cond_init(PyCOND_T *cond);
43e83c
+void _PyThread_cond_after(long long us, struct timespec *abs);
43e83c
+
43e83c
 /* return 0 for success, 1 on timeout, -1 on error */
43e83c
 Py_LOCAL_INLINE(int)
43e83c
 PyCOND_TIMEDWAIT(PyCOND_T *cond, PyMUTEX_T *mut, long long us)
43e83c
 {
43e83c
-    int r;
43e83c
-    struct timespec ts;
43e83c
-    struct timeval deadline;
43e83c
-
43e83c
-    PyCOND_GETTIMEOFDAY(&deadline);
43e83c
-    PyCOND_ADD_MICROSECONDS(deadline, us);
43e83c
-    ts.tv_sec = deadline.tv_sec;
43e83c
-    ts.tv_nsec = deadline.tv_usec * 1000;
43e83c
-
43e83c
-    r = pthread_cond_timedwait((cond), (mut), &ts);
43e83c
-    if (r == ETIMEDOUT)
43e83c
+    struct timespec abs;
43e83c
+    _PyThread_cond_after(us, &abs;;
43e83c
+    int ret = pthread_cond_timedwait(cond, mut, &abs;;
43e83c
+    if (ret == ETIMEDOUT) {
43e83c
         return 1;
43e83c
-    else if (r)
43e83c
+    }
43e83c
+    if (ret) {
43e83c
         return -1;
43e83c
-    else
43e83c
-        return 0;
43e83c
+    }
43e83c
+    return 0;
43e83c
 }
43e83c
 
43e83c
 #elif defined(NT_THREADS)
43e83c
diff --git a/Python/thread.c b/Python/thread.c
43e83c
index 63eeb1e..c5d0e59 100644
43e83c
--- a/Python/thread.c
43e83c
+++ b/Python/thread.c
43e83c
@@ -6,6 +6,7 @@
43e83c
    Stuff shared by all thread_*.h files is collected here. */
43e83c
 
43e83c
 #include "Python.h"
43e83c
+#include "condvar.h"
43e83c
 
43e83c
 #ifndef _POSIX_THREADS
43e83c
 /* This means pthreads are not implemented in libc headers, hence the macro
43e83c
diff --git a/Python/thread_pthread.h b/Python/thread_pthread.h
43e83c
index baea71f..7dc295e 100644
43e83c
--- a/Python/thread_pthread.h
43e83c
+++ b/Python/thread_pthread.h
43e83c
@@ -66,16 +66,6 @@
43e83c
 #endif
43e83c
 #endif
43e83c
 
43e83c
-#if !defined(pthread_attr_default)
43e83c
-#  define pthread_attr_default ((pthread_attr_t *)NULL)
43e83c
-#endif
43e83c
-#if !defined(pthread_mutexattr_default)
43e83c
-#  define pthread_mutexattr_default ((pthread_mutexattr_t *)NULL)
43e83c
-#endif
43e83c
-#if !defined(pthread_condattr_default)
43e83c
-#  define pthread_condattr_default ((pthread_condattr_t *)NULL)
43e83c
-#endif
43e83c
-
43e83c
 
43e83c
 /* Whether or not to use semaphores directly rather than emulating them with
43e83c
  * mutexes and condition variables:
43e83c
@@ -120,6 +110,56 @@ do { \
43e83c
 } while(0)
43e83c
 
43e83c
 
43e83c
+/*
43e83c
+ * pthread_cond support
43e83c
+ */
43e83c
+
43e83c
+#if defined(HAVE_PTHREAD_CONDATTR_SETCLOCK) && defined(HAVE_CLOCK_GETTIME) && defined(CLOCK_MONOTONIC)
43e83c
+// monotonic is supported statically.  It doesn't mean it works on runtime.
43e83c
+#define CONDATTR_MONOTONIC
43e83c
+#endif
43e83c
+
43e83c
+// NULL when pthread_condattr_setclock(CLOCK_MONOTONIC) is not supported.
43e83c
+static pthread_condattr_t *condattr_monotonic = NULL;
43e83c
+
43e83c
+static void
43e83c
+init_condattr()
43e83c
+{
43e83c
+#ifdef CONDATTR_MONOTONIC
43e83c
+    static pthread_condattr_t ca;
43e83c
+    pthread_condattr_init(&ca);
43e83c
+    if (pthread_condattr_setclock(&ca, CLOCK_MONOTONIC) == 0) {
43e83c
+        condattr_monotonic = &ca;  // Use monotonic clock
43e83c
+    }
43e83c
+#endif
43e83c
+}
43e83c
+
43e83c
+int
43e83c
+_PyThread_cond_init(PyCOND_T *cond)
43e83c
+{
43e83c
+    return pthread_cond_init(cond, condattr_monotonic);
43e83c
+}
43e83c
+
43e83c
+void
43e83c
+_PyThread_cond_after(long long us, struct timespec *abs)
43e83c
+{
43e83c
+#ifdef CONDATTR_MONOTONIC
43e83c
+    if (condattr_monotonic) {
43e83c
+        clock_gettime(CLOCK_MONOTONIC, abs);
43e83c
+        abs->tv_sec  += us / 1000000;
43e83c
+        abs->tv_nsec += (us % 1000000) * 1000;
43e83c
+        abs->tv_sec  += abs->tv_nsec / 1000000000;
43e83c
+        abs->tv_nsec %= 1000000000;
43e83c
+        return;
43e83c
+    }
43e83c
+#endif
43e83c
+
43e83c
+    struct timespec ts;
43e83c
+    MICROSECONDS_TO_TIMESPEC(us, ts);
43e83c
+    *abs = ts;
43e83c
+}
43e83c
+
43e83c
+
43e83c
 /* A pthread mutex isn't sufficient to model the Python lock type
43e83c
  * because, according to Draft 5 of the docs (P1003.4a/D5), both of the
43e83c
  * following are undefined:
43e83c
@@ -175,6 +215,7 @@ PyThread__init_thread(void)
43e83c
     extern void pthread_init(void);
43e83c
     pthread_init();
43e83c
 #endif
43e83c
+    init_condattr();
43e83c
 }
43e83c
 
43e83c
 #endif /* !_HAVE_BSDI */
43e83c
@@ -449,8 +490,7 @@ PyThread_allocate_lock(void)
43e83c
         memset((void *)lock, '\0', sizeof(pthread_lock));
43e83c
         lock->locked = 0;
43e83c
 
43e83c
-        status = pthread_mutex_init(&lock->mut,
43e83c
-                                    pthread_mutexattr_default);
43e83c
+        status = pthread_mutex_init(&lock->mut, NULL);
43e83c
         CHECK_STATUS_PTHREAD("pthread_mutex_init");
43e83c
         /* Mark the pthread mutex underlying a Python mutex as
43e83c
            pure happens-before.  We can't simply mark the
43e83c
@@ -459,8 +499,7 @@ PyThread_allocate_lock(void)
43e83c
            will cause errors. */
43e83c
         _Py_ANNOTATE_PURE_HAPPENS_BEFORE_MUTEX(&lock->mut);
43e83c
 
43e83c
-        status = pthread_cond_init(&lock->lock_released,
43e83c
-                                   pthread_condattr_default);
43e83c
+        status = _PyThread_cond_init(&lock->lock_released);
43e83c
         CHECK_STATUS_PTHREAD("pthread_cond_init");
43e83c
 
43e83c
         if (error) {
43e83c
@@ -519,9 +558,10 @@ PyThread_acquire_lock_timed(PyThread_type_lock lock, PY_TIMEOUT_T microseconds,
43e83c
             success = PY_LOCK_ACQUIRED;
43e83c
         }
43e83c
         else if (microseconds != 0) {
43e83c
-            struct timespec ts;
43e83c
-            if (microseconds > 0)
43e83c
-                MICROSECONDS_TO_TIMESPEC(microseconds, ts);
43e83c
+            struct timespec abs;
43e83c
+            if (microseconds > 0) {
43e83c
+                _PyThread_cond_after(microseconds, &abs;;
43e83c
+            }
43e83c
             /* continue trying until we get the lock */
43e83c
 
43e83c
             /* mut must be locked by me -- part of the condition
43e83c
@@ -530,10 +570,13 @@ PyThread_acquire_lock_timed(PyThread_type_lock lock, PY_TIMEOUT_T microseconds,
43e83c
                 if (microseconds > 0) {
43e83c
                     status = pthread_cond_timedwait(
43e83c
                         &thelock->lock_released,
43e83c
-                        &thelock->mut, &ts);
43e83c
+                        &thelock->mut, &abs;;
43e83c
+                    if (status == 1) {
43e83c
+                        break;
43e83c
+                    }
43e83c
                     if (status == ETIMEDOUT)
43e83c
                         break;
43e83c
-                    CHECK_STATUS_PTHREAD("pthread_cond_timed_wait");
43e83c
+                    CHECK_STATUS_PTHREAD("pthread_cond_timedwait");
43e83c
                 }
43e83c
                 else {
43e83c
                     status = pthread_cond_wait(
43e83c
diff --git a/configure.ac b/configure.ac
43e83c
index a0e3613..8a17559 100644
43e83c
--- a/configure.ac
43e83c
+++ b/configure.ac
43e83c
@@ -3582,7 +3582,7 @@ AC_CHECK_FUNCS(alarm accept4 setitimer getitimer bind_textdomain_codeset chown \
43e83c
  memrchr mbrtowc mkdirat mkfifo \
43e83c
  mkfifoat mknod mknodat mktime mremap nice openat pathconf pause pipe2 plock poll \
43e83c
  posix_fallocate posix_fadvise pread \
43e83c
- pthread_init pthread_kill putenv pwrite readlink readlinkat readv realpath renameat \
43e83c
+ pthread_condattr_setclock pthread_init pthread_kill putenv pwrite readlink readlinkat readv realpath renameat \
43e83c
  select sem_open sem_timedwait sem_getvalue sem_unlink sendfile setegid seteuid \
43e83c
  setgid sethostname \
43e83c
  setlocale setregid setreuid setresuid setresgid setsid setpgid setpgrp setpriority setuid setvbuf \