Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Add more atomics
  • Loading branch information
Fidget-Spinner committed Jul 14, 2024
commit da8415dee9fd519cd8fa9f2c6f6cad75c8d91ab0
4 changes: 4 additions & 0 deletions Include/cpython/pyatomic_gcc.h
Original file line number Diff line number Diff line change
Expand Up @@ -516,6 +516,10 @@ static inline int
_Py_atomic_load_int_acquire(const int *obj)
{ return __atomic_load_n(obj, __ATOMIC_ACQUIRE); }

static inline void
_Py_atomic_store_uint8_release(uint8_t *obj, uint8_t value)
{ __atomic_store_n(obj, value, __ATOMIC_RELEASE); }

static inline void
_Py_atomic_store_uint32_release(uint32_t *obj, uint32_t value)
{ __atomic_store_n(obj, value, __ATOMIC_RELEASE); }
Expand Down
13 changes: 13 additions & 0 deletions Include/cpython/pyatomic_msc.h
Original file line number Diff line number Diff line change
Expand Up @@ -989,6 +989,19 @@ _Py_atomic_load_int_acquire(const int *obj)
#endif
}

static inline void
_Py_atomic_store_uint8_release(uint8_t *obj, uint8_t value)
{
#if defined(_M_X64) || defined(_M_IX86)
*(uint8_t volatile *)obj = value;
#elif defined(_M_ARM64)
_Py_atomic_ASSERT_ARG_TYPE(unsigned __int8);
__stlr8((unsigned __int8 volatile *)obj, (unsigned __int8)value);
#else
# error "no implementation of _Py_atomic_store_uint32_release"
#endif
}

static inline void
_Py_atomic_store_uint32_release(uint32_t *obj, uint32_t value)
{
Expand Down
8 changes: 8 additions & 0 deletions Include/cpython/pyatomic_std.h
Original file line number Diff line number Diff line change
Expand Up @@ -911,6 +911,14 @@ _Py_atomic_load_int_acquire(const int *obj)
memory_order_acquire);
}

static inline void
_Py_atomic_store_uint8_release(uint8_t *obj, uint8_t value)
{
_Py_USING_STD;
atomic_store_explicit((_Atomic(uint8_t)*)obj, value,
memory_order_release);
}

static inline void
_Py_atomic_store_uint32_release(uint32_t *obj, uint32_t value)
{
Expand Down
4 changes: 2 additions & 2 deletions Objects/object.c
Original file line number Diff line number Diff line change
Expand Up @@ -2360,10 +2360,10 @@ new_reference(PyObject *op)
#if !defined(Py_GIL_DISABLED)
op->ob_refcnt = 1;
#else
op->ob_tid = _Py_ThreadId();
_Py_atomic_store_uintptr_release(&op->ob_tid, _Py_ThreadId());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we want to do this, at least not for now. I want to wait until we fix all the other races before we address new_reference because there are perf implications and the race is in practice benign.

The "release" memory order is also stronger (and more expensive on arm64) than necessary.

op->_padding = 0;
op->ob_mutex = (PyMutex){ 0 };
op->ob_gc_bits = 0;
_Py_atomic_store_uint8_release(&op->ob_gc_bits, 0);
_Py_atomic_store_uint32_release(&op->ob_ref_local, 1);
_Py_atomic_store_ssize_release(&op->ob_ref_shared, 0);
#endif
Expand Down