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