diff --git a/Include/internal/pycore_pyatomic_ft_wrappers.h b/Include/internal/pycore_pyatomic_ft_wrappers.h index bbfc462a733d0e..9c51e8ebf8252d 100644 --- a/Include/internal/pycore_pyatomic_ft_wrappers.h +++ b/Include/internal/pycore_pyatomic_ft_wrappers.h @@ -35,6 +35,8 @@ extern "C" { _Py_atomic_load_uintptr_acquire(&value) #define FT_ATOMIC_LOAD_PTR_RELAXED(value) \ _Py_atomic_load_ptr_relaxed(&value) +#define FT_ATOMIC_LOAD_INT(value) \ + _Py_atomic_load_int(&value) #define FT_ATOMIC_LOAD_UINT8(value) \ _Py_atomic_load_uint8(&value) #define FT_ATOMIC_STORE_UINT8(value, new_value) \ @@ -49,6 +51,10 @@ extern "C" { _Py_atomic_store_ssize_relaxed(&value, new_value) #define FT_ATOMIC_STORE_UINT8_RELAXED(value, new_value) \ _Py_atomic_store_uint8_relaxed(&value, new_value) +#define FT_ATOMIC_STORE_INT(value, new_value) \ + _Py_atomic_store_int(&value, new_value) +#define FT_ATMOIC_INT_INCREMENT(value) \ + _Py_atomic_add_int(&value, 1) #else #define FT_ATOMIC_LOAD_PTR(value) value @@ -60,6 +66,7 @@ extern "C" { #define FT_ATOMIC_LOAD_PTR_ACQUIRE(value) value #define FT_ATOMIC_LOAD_UINTPTR_ACQUIRE(value) value #define FT_ATOMIC_LOAD_PTR_RELAXED(value) value +#define FT_ATOMIC_LOAD_INT(value) value #define FT_ATOMIC_LOAD_UINT8(value) value #define FT_ATOMIC_STORE_UINT8(value, new_value) value = new_value #define FT_ATOMIC_STORE_PTR_RELAXED(value, new_value) value = new_value @@ -67,6 +74,8 @@ extern "C" { #define FT_ATOMIC_STORE_UINTPTR_RELEASE(value, new_value) value = new_value #define FT_ATOMIC_STORE_SSIZE_RELAXED(value, new_value) value = new_value #define FT_ATOMIC_STORE_UINT8_RELAXED(value, new_value) value = new_value +#define FT_ATOMIC_STORE_INT(value, new_value) value = new_value +#define FT_ATMOIC_INT_INCREMENT(value) value++ #endif diff --git a/Modules/_randommodule.c b/Modules/_randommodule.c index 56b891dfe0f85f..3a579c896de1ae 100644 --- a/Modules/_randommodule.c +++ b/Modules/_randommodule.c @@ -75,6 +75,7 @@ #include "pycore_modsupport.h" // _PyArg_NoKeywords() #include "pycore_moduleobject.h" // _PyModule_GetState() #include "pycore_pylifecycle.h" // _PyOS_URandomNonblock() +#include "pycore_pyatomic_ft_wrappers.h" // FT_LOAD_INT() #ifdef HAVE_UNISTD_H # include // getpid() @@ -139,7 +140,8 @@ genrand_uint32(RandomObject *self) uint32_t *mt; mt = self->state; - if (self->index >= N) { /* generate N words at one time */ + int index = FT_ATOMIC_LOAD_INT(self->index); + if (index >= N) { /* generate N words at one time */ int kk; for (kk=0;kk> 1) ^ mag01[y & 0x1U]; - self->index = 0; + FT_ATOMIC_STORE_INT(self->index, 0); } - y = mt[self->index++]; + y = mt[FT_ATMOIC_INT_INCREMENT(self->index)]; y ^= (y >> 11); y ^= (y << 7) & 0x9d2c5680U; y ^= (y << 15) & 0xefc60000U; @@ -175,7 +177,6 @@ genrand_uint32(RandomObject *self) */ /*[clinic input] -@critical_section _random.Random.random self: self(type="RandomObject *") @@ -185,7 +186,7 @@ random() -> x in the interval [0, 1). static PyObject * _random_Random_random_impl(RandomObject *self) -/*[clinic end generated code: output=117ff99ee53d755c input=26492e52d26e8b7b]*/ +/*[clinic end generated code: output=117ff99ee53d755c input=afb2a59cbbb00349]*/ { uint32_t a=genrand_uint32(self)>>5, b=genrand_uint32(self)>>6; return PyFloat_FromDouble((a*67108864.0+b)*(1.0/9007199254740992.0)); @@ -491,7 +492,6 @@ _random_Random_setstate_impl(RandomObject *self, PyObject *state) } /*[clinic input] -@critical_section _random.Random.getrandbits self: self(type="RandomObject *") @@ -503,7 +503,7 @@ getrandbits(k) -> x. Generates an int with k random bits. static PyObject * _random_Random_getrandbits_impl(RandomObject *self, int k) -/*[clinic end generated code: output=b402f82a2158887f input=87603cd60f79f730]*/ +/*[clinic end generated code: output=b402f82a2158887f input=abfd108bf3c91945]*/ { int i, words; uint32_t r; diff --git a/Modules/clinic/_randommodule.c.h b/Modules/clinic/_randommodule.c.h index 6193acac67e7ac..c001e3a6333eea 100644 --- a/Modules/clinic/_randommodule.c.h +++ b/Modules/clinic/_randommodule.c.h @@ -20,13 +20,7 @@ _random_Random_random_impl(RandomObject *self); static PyObject * _random_Random_random(RandomObject *self, PyObject *Py_UNUSED(ignored)) { - PyObject *return_value = NULL; - - Py_BEGIN_CRITICAL_SECTION(self); - return_value = _random_Random_random_impl(self); - Py_END_CRITICAL_SECTION(); - - return return_value; + return _random_Random_random_impl(self); } PyDoc_STRVAR(_random_Random_seed__doc__, @@ -136,11 +130,9 @@ _random_Random_getrandbits(RandomObject *self, PyObject *arg) if (k == -1 && PyErr_Occurred()) { goto exit; } - Py_BEGIN_CRITICAL_SECTION(self); return_value = _random_Random_getrandbits_impl(self, k); - Py_END_CRITICAL_SECTION(); exit: return return_value; } -/*[clinic end generated code: output=bf49ece1d341b1b6 input=a9049054013a1b77]*/ +/*[clinic end generated code: output=2ae7839e9d1ba07c input=a9049054013a1b77]*/