Skip to content

Commit

Permalink
Test switchThread()
Browse files Browse the repository at this point in the history
  • Loading branch information
corona10 committed Sep 28, 2024
1 parent 60a43e9 commit 7fa925b
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 2 deletions.
4 changes: 4 additions & 0 deletions Include/cpython/pyatomic_msc.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#endif

#include <intrin.h>
#include <windows.h>

#define _Py_atomic_ASSERT_ARG_TYPE(TYPE) \
Py_BUILD_ASSERT(sizeof(*obj) == sizeof(TYPE))
Expand Down Expand Up @@ -315,6 +316,7 @@ _Py_atomic_exchange_int64(int64_t *obj, int64_t value)
if (_Py_atomic_compare_exchange_int64(obj, &old_value, value)) {
return old_value;
}
SwitchToThread();
}
#endif
}
Expand Down Expand Up @@ -431,6 +433,7 @@ _Py_atomic_and_uint64(uint64_t *obj, uint64_t value)
if (_Py_atomic_compare_exchange_uint64(obj, &old_value, new_value)) {
return old_value;
}
SwitchToThread();
}
#endif
}
Expand Down Expand Up @@ -486,6 +489,7 @@ _Py_atomic_or_uint64(uint64_t *obj, uint64_t value)
if (_Py_atomic_compare_exchange_uint64(obj, &old_value, new_value)) {
return old_value;
}
SwitchToThread();
}
#endif
}
Expand Down
2 changes: 0 additions & 2 deletions Include/internal/pycore_cell.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ _PyStackRef _PyCell_GetStackRef(PyCellObject *cell)
{
PyObject *value;
#ifdef Py_GIL_DISABLED
_Py_atomic_fence_acquire();
value = _Py_atomic_load_ptr(&cell->ob_ref);
if (value != NULL) {
if (_Py_IsImmortal(value) || _PyObject_HasDeferredRefcount(value)) {
Expand All @@ -58,7 +57,6 @@ _PyStackRef _PyCell_GetStackRef(PyCellObject *cell)
return _PyStackRef_FromPyObjectSteal(value);
}
}
_Py_atomic_fence_release();
#endif
value = PyCell_GetRef(cell);
if (value == NULL) {
Expand Down
17 changes: 17 additions & 0 deletions Include/internal/pycore_object.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ extern "C" {
#include "pycore_pystate.h" // _PyInterpreterState_GET()
#include "pycore_typeid.h" // _PyType_IncrefSlow

#ifdef MS_WINDOWS
# define WIN32_LEAN_AND_MEAN
# include <windows.h> // SwitchToThread()
#endif

#define _Py_IMMORTAL_REFCNT_LOOSE ((_Py_IMMORTAL_REFCNT >> 1) + 1)

// This value is added to `ob_ref_shared` for objects that use deferred
Expand Down Expand Up @@ -127,6 +132,14 @@ extern void _Py_DecRefTotal(PyThreadState *);
interp->object_state.reftotal--
#endif

static inline void _SwitchThread(void)
{
#ifdef MS_WINDOWS
SwitchToThread();
#endif
return;
}

// Increment reference count by n
static inline void _Py_RefcntAdd(PyObject* op, Py_ssize_t n)
{
Expand Down Expand Up @@ -549,6 +562,7 @@ _Py_TryIncRefShared(PyObject *op)
_Py_INCREF_STAT_INC();
return 1;
}
_SwitchThread();
}
}

Expand Down Expand Up @@ -583,6 +597,7 @@ _Py_XGetRef(PyObject **ptr)
if (_Py_TryIncrefCompare(ptr, value)) {
return value;
}
_SwitchThread();
}
}

Expand Down Expand Up @@ -625,6 +640,7 @@ _Py_NewRefWithLock(PyObject *op)
new_shared)) {
return op;
}
_SwitchThread();
}
}

Expand Down Expand Up @@ -653,6 +669,7 @@ _PyObject_SetMaybeWeakref(PyObject *op)
&op->ob_ref_shared, &shared, shared | _Py_REF_MAYBE_WEAKREF)) {
return;
}
_SwitchThread();
}
}

Expand Down

0 comments on commit 7fa925b

Please sign in to comment.