Skip to content

Commit d1cf0bf

Browse files
committed
gh-118392: Update genrand_uint32 to use atomic operation
1 parent 23d0371 commit d1cf0bf

File tree

3 files changed

+18
-17
lines changed

3 files changed

+18
-17
lines changed

Include/internal/pycore_pyatomic_ft_wrappers.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ extern "C" {
3535
_Py_atomic_load_uintptr_acquire(&value)
3636
#define FT_ATOMIC_LOAD_PTR_RELAXED(value) \
3737
_Py_atomic_load_ptr_relaxed(&value)
38+
#define FT_ATOMIC_LOAD_INT(value) \
39+
_Py_atomic_load_int(&value)
3840
#define FT_ATOMIC_LOAD_UINT8(value) \
3941
_Py_atomic_load_uint8(&value)
4042
#define FT_ATOMIC_STORE_UINT8(value, new_value) \
@@ -49,6 +51,10 @@ extern "C" {
4951
_Py_atomic_store_ssize_relaxed(&value, new_value)
5052
#define FT_ATOMIC_STORE_UINT8_RELAXED(value, new_value) \
5153
_Py_atomic_store_uint8_relaxed(&value, new_value)
54+
#define FT_ATOMIC_STORE_INT(value, new_value) \
55+
_Py_atomic_store_int(&value, new_value)
56+
#define FT_ATMOIC_INT_INCREMENT(value) \
57+
_Py_atomic_add_int(&value, 1)
5258

5359
#else
5460
#define FT_ATOMIC_LOAD_PTR(value) value
@@ -60,13 +66,16 @@ extern "C" {
6066
#define FT_ATOMIC_LOAD_PTR_ACQUIRE(value) value
6167
#define FT_ATOMIC_LOAD_UINTPTR_ACQUIRE(value) value
6268
#define FT_ATOMIC_LOAD_PTR_RELAXED(value) value
69+
#define FT_ATOMIC_LOAD_INT(value) value
6370
#define FT_ATOMIC_LOAD_UINT8(value) value
6471
#define FT_ATOMIC_STORE_UINT8(value, new_value) value = new_value
6572
#define FT_ATOMIC_STORE_PTR_RELAXED(value, new_value) value = new_value
6673
#define FT_ATOMIC_STORE_PTR_RELEASE(value, new_value) value = new_value
6774
#define FT_ATOMIC_STORE_UINTPTR_RELEASE(value, new_value) value = new_value
6875
#define FT_ATOMIC_STORE_SSIZE_RELAXED(value, new_value) value = new_value
6976
#define FT_ATOMIC_STORE_UINT8_RELAXED(value, new_value) value = new_value
77+
#define FT_ATOMIC_STORE_INT(value, new_value) value = new_value
78+
#define FT_ATMOIC_INT_INCREMENT(value) value++
7079

7180
#endif
7281

Modules/_randommodule.c

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@
7575
#include "pycore_modsupport.h" // _PyArg_NoKeywords()
7676
#include "pycore_moduleobject.h" // _PyModule_GetState()
7777
#include "pycore_pylifecycle.h" // _PyOS_URandomNonblock()
78+
#include "pycore_pyatomic_ft_wrappers.h" // FT_LOAD_INT()
7879

7980
#ifdef HAVE_UNISTD_H
8081
# include <unistd.h> // getpid()
@@ -139,7 +140,8 @@ genrand_uint32(RandomObject *self)
139140
uint32_t *mt;
140141

141142
mt = self->state;
142-
if (self->index >= N) { /* generate N words at one time */
143+
int index = FT_ATOMIC_LOAD_INT(self->index);
144+
if (index >= N) { /* generate N words at one time */
143145
int kk;
144146

145147
for (kk=0;kk<N-M;kk++) {
@@ -153,10 +155,10 @@ genrand_uint32(RandomObject *self)
153155
y = (mt[N-1]&UPPER_MASK)|(mt[0]&LOWER_MASK);
154156
mt[N-1] = mt[M-1] ^ (y >> 1) ^ mag01[y & 0x1U];
155157

156-
self->index = 0;
158+
FT_ATOMIC_STORE_INT(self->index, 0);
157159
}
158160

159-
y = mt[self->index++];
161+
y = mt[FT_ATMOIC_INT_INCREMENT(self->index)];
160162
y ^= (y >> 11);
161163
y ^= (y << 7) & 0x9d2c5680U;
162164
y ^= (y << 15) & 0xefc60000U;
@@ -175,7 +177,6 @@ genrand_uint32(RandomObject *self)
175177
*/
176178

177179
/*[clinic input]
178-
@critical_section
179180
_random.Random.random
180181
181182
self: self(type="RandomObject *")
@@ -185,7 +186,7 @@ random() -> x in the interval [0, 1).
185186

186187
static PyObject *
187188
_random_Random_random_impl(RandomObject *self)
188-
/*[clinic end generated code: output=117ff99ee53d755c input=26492e52d26e8b7b]*/
189+
/*[clinic end generated code: output=117ff99ee53d755c input=afb2a59cbbb00349]*/
189190
{
190191
uint32_t a=genrand_uint32(self)>>5, b=genrand_uint32(self)>>6;
191192
return PyFloat_FromDouble((a*67108864.0+b)*(1.0/9007199254740992.0));
@@ -491,7 +492,6 @@ _random_Random_setstate_impl(RandomObject *self, PyObject *state)
491492
}
492493

493494
/*[clinic input]
494-
@critical_section
495495
_random.Random.getrandbits
496496
497497
self: self(type="RandomObject *")
@@ -503,7 +503,7 @@ getrandbits(k) -> x. Generates an int with k random bits.
503503

504504
static PyObject *
505505
_random_Random_getrandbits_impl(RandomObject *self, int k)
506-
/*[clinic end generated code: output=b402f82a2158887f input=87603cd60f79f730]*/
506+
/*[clinic end generated code: output=b402f82a2158887f input=abfd108bf3c91945]*/
507507
{
508508
int i, words;
509509
uint32_t r;

Modules/clinic/_randommodule.c.h

Lines changed: 2 additions & 10 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)