Skip to content

Commit d41132c

Browse files
committed
signed long race
1 parent cf25726 commit d41132c

File tree

6 files changed

+57
-2
lines changed

6 files changed

+57
-2
lines changed

Include/cpython/pyatomic.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -333,6 +333,9 @@ _Py_atomic_load_short_relaxed(const short *obj);
333333
static inline unsigned short
334334
_Py_atomic_load_ushort_relaxed(const unsigned short *obj);
335335

336+
static inline long
337+
_Py_atomic_load_long_relaxed(const long *obj);
338+
336339
static inline int8_t
337340
_Py_atomic_load_int8_relaxed(const int8_t *obj);
338341

@@ -508,6 +511,9 @@ _Py_atomic_store_ushort_release(unsigned short *obj, unsigned short value);
508511
static inline void
509512
_Py_atomic_store_uint_release(unsigned int *obj, unsigned int value);
510513

514+
static inline void
515+
_Py_atomic_store_long_release(long *obj, long value);
516+
511517
static inline int
512518
_Py_atomic_load_int_acquire(const int *obj);
513519

Include/cpython/pyatomic_gcc.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -322,6 +322,10 @@ static inline unsigned short
322322
_Py_atomic_load_ushort_relaxed(const unsigned short *obj)
323323
{ return __atomic_load_n(obj, __ATOMIC_RELAXED); }
324324

325+
static inline long
326+
_Py_atomic_load_long_relaxed(const long *obj)
327+
{ return __atomic_load_n(obj, __ATOMIC_RELAXED); }
328+
325329
static inline int8_t
326330
_Py_atomic_load_int8_relaxed(const int8_t *obj)
327331
{ return __atomic_load_n(obj, __ATOMIC_RELAXED); }
@@ -544,6 +548,10 @@ static inline void
544548
_Py_atomic_store_uint_release(unsigned int *obj, unsigned int value)
545549
{ __atomic_store_n(obj, value, __ATOMIC_RELEASE); }
546550

551+
static inline void
552+
_Py_atomic_store_long_release(long *obj, long value)
553+
{ __atomic_store_n(obj, value, __ATOMIC_RELEASE); }
554+
547555
static inline void
548556
_Py_atomic_store_ssize_release(Py_ssize_t *obj, Py_ssize_t value)
549557
{ __atomic_store_n(obj, value, __ATOMIC_RELEASE); }

Include/cpython/pyatomic_msc.h

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -658,6 +658,12 @@ _Py_atomic_load_ushort_relaxed(const unsigned short *obj)
658658
return *(volatile unsigned short *)obj;
659659
}
660660

661+
static inline long
662+
_Py_atomic_load_long_relaxed(const long *obj)
663+
{
664+
return *(volatile long *)obj;
665+
}
666+
661667
static inline int8_t
662668
_Py_atomic_load_int8_relaxed(const int8_t *obj)
663669
{
@@ -1053,6 +1059,19 @@ _Py_atomic_store_uint_release(unsigned int *obj, unsigned int value)
10531059
#endif
10541060
}
10551061

1062+
static inline void
1063+
_Py_atomic_store_long_release(long *obj, long value)
1064+
{
1065+
#if defined(_M_X64) || defined(_M_IX86)
1066+
*(long volatile *)obj = value;
1067+
#elif defined(_M_ARM64)
1068+
_Py_atomic_ASSERT_ARG_TYPE(unsigned __int64);
1069+
__stlr64((unsigned __int64 volatile *)obj, (unsigned __int64)value);
1070+
#else
1071+
# error "no implementation of _Py_atomic_store_long_release"
1072+
#endif
1073+
}
1074+
10561075
static inline void
10571076
_Py_atomic_store_ssize_release(Py_ssize_t *obj, Py_ssize_t value)
10581077
{

Include/cpython/pyatomic_std.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -547,6 +547,14 @@ _Py_atomic_load_ushort_relaxed(const unsigned short *obj)
547547
memory_order_relaxed);
548548
}
549549

550+
static inline long
551+
_Py_atomic_load_long_relaxed(const long *obj)
552+
{
553+
_Py_USING_STD;
554+
return atomic_load_explicit((const _Atomic(long)*)obj,
555+
memory_order_relaxed);
556+
}
557+
550558
static inline int8_t
551559
_Py_atomic_load_int8_relaxed(const int8_t *obj)
552560
{
@@ -967,6 +975,14 @@ _Py_atomic_store_uint_release(unsigned int *obj, unsigned int value)
967975
memory_order_release);
968976
}
969977

978+
static inline void
979+
_Py_atomic_store_long_release(long *obj, long value)
980+
{
981+
_Py_USING_STD;
982+
atomic_store_explicit((_Atomic(long)*)obj, value,
983+
memory_order_release);
984+
}
985+
970986
static inline void
971987
_Py_atomic_store_ssize_release(Py_ssize_t *obj, Py_ssize_t value)
972988
{

Include/internal/pycore_pyatomic_ft_wrappers.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,10 @@ extern "C" {
8585
_Py_atomic_store_uint_release(&value, new_value)
8686
#define FT_ATOMIC_LOAD_UINT_RELAXED(value) \
8787
_Py_atomic_load_uint_relaxed(&value)
88+
#define FT_ATOMIC_STORE_LONG_RELEASE(value, new_value) \
89+
_Py_atomic_store_long_release(&value, new_value)
90+
#define FT_ATOMIC_LOAD_LONG_RELAXED(value) \
91+
_Py_atomic_load_long_relaxed(&value)
8892

8993
#else
9094
#define FT_ATOMIC_LOAD_PTR(value) value
@@ -120,6 +124,8 @@ extern "C" {
120124
#define FT_ATOMIC_STORE_INT_RELEASE(value, new_value) value = new_value
121125
#define FT_ATOMIC_LOAD_UINT_RELAXED(value) value
122126
#define FT_ATOMIC_STORE_UINT_RELEASE(value, new_value) value = new_value
127+
#define FT_ATOMIC_LOAD_LONG_RELAXED(value) value
128+
#define FT_ATOMIC_STORE_LONG_RELEASE(value, new_value) value = new_value
123129

124130
#endif
125131

Python/structmember.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ PyMember_GetOne(const char *obj_addr, PyMemberDef *l)
5555
v = PyLong_FromUnsignedLong(FT_ATOMIC_LOAD_UINT_RELAXED(*(unsigned int*)addr));
5656
break;
5757
case Py_T_LONG:
58-
v = PyLong_FromLong(*(long*)addr);
58+
v = PyLong_FromLong(FT_ATOMIC_LOAD_LONG_RELAXED(*(long*)addr));
5959
break;
6060
case Py_T_ULONG:
6161
v = PyLong_FromUnsignedLong(*(unsigned long*)addr);
@@ -251,7 +251,7 @@ PyMember_SetOne(char *addr, PyMemberDef *l, PyObject *v)
251251
break;
252252
}
253253
case Py_T_LONG:{
254-
*(long*)addr = PyLong_AsLong(v);
254+
FT_ATOMIC_STORE_LONG_RELEASE(*(long*)addr, PyLong_AsLong(v));
255255
if ((*(long*)addr == -1) && PyErr_Occurred())
256256
return -1;
257257
break;

0 commit comments

Comments
 (0)