summaryrefslogtreecommitdiffstats
path: root/Python
diff options
context:
space:
mode:
authorAndrew Rogers <32688592+adr26@users.noreply.github.com>2024-02-02 13:50:51 (GMT)
committerGitHub <noreply@github.com>2024-02-02 13:50:51 (GMT)
commitb3f0b698daf2438a6e59d5d19ccb34acdba0bffc (patch)
treec13e5926d093a35c2e7b63b525af2ef5b4733fe3 /Python
parentd29f57f6036353b4e705a42637177442bf7e07e5 (diff)
downloadcpython-b3f0b698daf2438a6e59d5d19ccb34acdba0bffc.zip
cpython-b3f0b698daf2438a6e59d5d19ccb34acdba0bffc.tar.gz
cpython-b3f0b698daf2438a6e59d5d19ccb34acdba0bffc.tar.bz2
gh-104530: Enable native Win32 condition variables by default (GH-104531)
Diffstat (limited to 'Python')
-rw-r--r--Python/ceval_gil.c8
-rw-r--r--Python/condvar.h23
-rw-r--r--Python/pystate.c12
-rw-r--r--Python/thread_nt.h22
4 files changed, 35 insertions, 30 deletions
diff --git a/Python/ceval_gil.c b/Python/ceval_gil.c
index f3b1692..ad90359 100644
--- a/Python/ceval_gil.c
+++ b/Python/ceval_gil.c
@@ -610,8 +610,16 @@ PyEval_SaveThread(void)
void
PyEval_RestoreThread(PyThreadState *tstate)
{
+#ifdef MS_WINDOWS
+ int err = GetLastError();
+#endif
+
_Py_EnsureTstateNotNULL(tstate);
_PyThreadState_Attach(tstate);
+
+#ifdef MS_WINDOWS
+ SetLastError(err);
+#endif
}
diff --git a/Python/condvar.h b/Python/condvar.h
index d54db94..dcabed6 100644
--- a/Python/condvar.h
+++ b/Python/condvar.h
@@ -260,13 +260,13 @@ PyMUTEX_UNLOCK(PyMUTEX_T *cs)
return 0;
}
-
Py_LOCAL_INLINE(int)
PyCOND_INIT(PyCOND_T *cv)
{
InitializeConditionVariable(cv);
return 0;
}
+
Py_LOCAL_INLINE(int)
PyCOND_FINI(PyCOND_T *cv)
{
@@ -279,27 +279,32 @@ PyCOND_WAIT(PyCOND_T *cv, PyMUTEX_T *cs)
return SleepConditionVariableSRW(cv, cs, INFINITE, 0) ? 0 : -1;
}
-/* This implementation makes no distinction about timeouts. Signal
- * 2 to indicate that we don't know.
- */
+/* return 0 for success, 1 on timeout, -1 on error */
Py_LOCAL_INLINE(int)
PyCOND_TIMEDWAIT(PyCOND_T *cv, PyMUTEX_T *cs, long long us)
{
- return SleepConditionVariableSRW(cv, cs, (DWORD)(us/1000), 0) ? 2 : -1;
+ BOOL success = SleepConditionVariableSRW(cv, cs, (DWORD)(us/1000), 0);
+ if (!success) {
+ if (GetLastError() == ERROR_TIMEOUT) {
+ return 1;
+ }
+ return -1;
+ }
+ return 0;
}
Py_LOCAL_INLINE(int)
PyCOND_SIGNAL(PyCOND_T *cv)
{
- WakeConditionVariable(cv);
- return 0;
+ WakeConditionVariable(cv);
+ return 0;
}
Py_LOCAL_INLINE(int)
PyCOND_BROADCAST(PyCOND_T *cv)
{
- WakeAllConditionVariable(cv);
- return 0;
+ WakeAllConditionVariable(cv);
+ return 0;
}
diff --git a/Python/pystate.c b/Python/pystate.c
index 27b6d05..7836c17 100644
--- a/Python/pystate.c
+++ b/Python/pystate.c
@@ -2488,7 +2488,17 @@ PyGILState_Check(void)
return 0;
}
- return (tstate == gilstate_tss_get(runtime));
+#ifdef MS_WINDOWS
+ int err = GetLastError();
+#endif
+
+ PyThreadState *tcur = gilstate_tss_get(runtime);
+
+#ifdef MS_WINDOWS
+ SetLastError(err);
+#endif
+
+ return (tstate == tcur);
}
PyGILState_STATE
diff --git a/Python/thread_nt.h b/Python/thread_nt.h
index 14b9cdd..044e9fa 100644
--- a/Python/thread_nt.h
+++ b/Python/thread_nt.h
@@ -444,16 +444,7 @@ PyThread_set_key_value(int key, void *value)
void *
PyThread_get_key_value(int key)
{
- /* because TLS is used in the Py_END_ALLOW_THREAD macro,
- * it is necessary to preserve the windows error state, because
- * it is assumed to be preserved across the call to the macro.
- * Ideally, the macro should be fixed, but it is simpler to
- * do it here.
- */
- DWORD error = GetLastError();
- void *result = TlsGetValue(key);
- SetLastError(error);
- return result;
+ return TlsGetValue(key);
}
void
@@ -525,14 +516,5 @@ void *
PyThread_tss_get(Py_tss_t *key)
{
assert(key != NULL);
- /* because TSS is used in the Py_END_ALLOW_THREAD macro,
- * it is necessary to preserve the windows error state, because
- * it is assumed to be preserved across the call to the macro.
- * Ideally, the macro should be fixed, but it is simpler to
- * do it here.
- */
- DWORD error = GetLastError();
- void *result = TlsGetValue(key->_key);
- SetLastError(error);
- return result;
+ return TlsGetValue(key->_key);
}