]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-112071: Make `_random.Random` methods thread-safe in `--disable-gil` builds (gh...
authorRadislav Chugunov <52372310+chgnrdv@users.noreply.github.com>
Tue, 28 Nov 2023 03:27:39 +0000 (06:27 +0300)
committerGitHub <noreply@github.com>
Tue, 28 Nov 2023 03:27:39 +0000 (03:27 +0000)
Co-authored-by: Donghee Na <donghee.na@python.org>
Modules/_randommodule.c
Modules/clinic/_randommodule.c.h

index 514bec16a347f4b60dfdb0e77e906a603eeccdee..4403e1d132c0570918281dac8bdd11d5f43b9489 100644 (file)
@@ -175,6 +175,7 @@ genrand_uint32(RandomObject *self)
  */
 
 /*[clinic input]
+@critical_section
 _random.Random.random
 
   self: self(type="RandomObject *")
@@ -184,7 +185,7 @@ random() -> x in the interval [0, 1).
 
 static PyObject *
 _random_Random_random_impl(RandomObject *self)
-/*[clinic end generated code: output=117ff99ee53d755c input=afb2a59cbbb00349]*/
+/*[clinic end generated code: output=117ff99ee53d755c input=26492e52d26e8b7b]*/
 {
     uint32_t a=genrand_uint32(self)>>5, b=genrand_uint32(self)>>6;
     return PyFloat_FromDouble((a*67108864.0+b)*(1.0/9007199254740992.0));
@@ -368,6 +369,7 @@ Done:
 }
 
 /*[clinic input]
+@critical_section
 _random.Random.seed
 
   self: self(type="RandomObject *")
@@ -382,7 +384,7 @@ of the current time and the process identifier.
 
 static PyObject *
 _random_Random_seed_impl(RandomObject *self, PyObject *n)
-/*[clinic end generated code: output=0fad1e16ba883681 input=78d6ef0d52532a54]*/
+/*[clinic end generated code: output=0fad1e16ba883681 input=46d01d2ba938c7b1]*/
 {
     if (random_seed(self, n) < 0) {
         return NULL;
@@ -391,6 +393,7 @@ _random_Random_seed_impl(RandomObject *self, PyObject *n)
 }
 
 /*[clinic input]
+@critical_section
 _random.Random.getstate
 
   self: self(type="RandomObject *")
@@ -400,7 +403,7 @@ getstate() -> tuple containing the current state.
 
 static PyObject *
 _random_Random_getstate_impl(RandomObject *self)
-/*[clinic end generated code: output=bf6cef0c092c7180 input=b937a487928c0e89]*/
+/*[clinic end generated code: output=bf6cef0c092c7180 input=b6621f31eb639694]*/
 {
     PyObject *state;
     PyObject *element;
@@ -428,6 +431,7 @@ Fail:
 
 
 /*[clinic input]
+@critical_section
 _random.Random.setstate
 
   self: self(type="RandomObject *")
@@ -438,8 +442,8 @@ setstate(state) -> None.  Restores generator state.
 [clinic start generated code]*/
 
 static PyObject *
-_random_Random_setstate(RandomObject *self, PyObject *state)
-/*[clinic end generated code: output=fd1c3cd0037b6681 input=b3b4efbb1bc66af8]*/
+_random_Random_setstate_impl(RandomObject *self, PyObject *state)
+/*[clinic end generated code: output=babfc2c2eac6b027 input=358e898ec07469b7]*/
 {
     int i;
     unsigned long element;
@@ -479,7 +483,7 @@ _random_Random_setstate(RandomObject *self, PyObject *state)
 }
 
 /*[clinic input]
-
+@critical_section
 _random.Random.getrandbits
 
   self: self(type="RandomObject *")
@@ -491,7 +495,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=8c0e6396dd176fc0]*/
+/*[clinic end generated code: output=b402f82a2158887f input=87603cd60f79f730]*/
 {
     int i, words;
     uint32_t r;
index 757e49e23cacfb2fc110b5cbc28a8a797135dde2..6193acac67e7acc83d6b1c50dbf2ad20508c04d6 100644 (file)
@@ -2,6 +2,7 @@
 preserve
 [clinic start generated code]*/
 
+#include "pycore_critical_section.h"// Py_BEGIN_CRITICAL_SECTION()
 #include "pycore_modsupport.h"    // _PyArg_CheckPositional()
 
 PyDoc_STRVAR(_random_Random_random__doc__,
@@ -19,7 +20,13 @@ _random_Random_random_impl(RandomObject *self);
 static PyObject *
 _random_Random_random(RandomObject *self, PyObject *Py_UNUSED(ignored))
 {
-    return _random_Random_random_impl(self);
+    PyObject *return_value = NULL;
+
+    Py_BEGIN_CRITICAL_SECTION(self);
+    return_value = _random_Random_random_impl(self);
+    Py_END_CRITICAL_SECTION();
+
+    return return_value;
 }
 
 PyDoc_STRVAR(_random_Random_seed__doc__,
@@ -51,7 +58,9 @@ _random_Random_seed(RandomObject *self, PyObject *const *args, Py_ssize_t nargs)
     }
     n = args[0];
 skip_optional:
+    Py_BEGIN_CRITICAL_SECTION(self);
     return_value = _random_Random_seed_impl(self, n);
+    Py_END_CRITICAL_SECTION();
 
 exit:
     return return_value;
@@ -72,7 +81,13 @@ _random_Random_getstate_impl(RandomObject *self);
 static PyObject *
 _random_Random_getstate(RandomObject *self, PyObject *Py_UNUSED(ignored))
 {
-    return _random_Random_getstate_impl(self);
+    PyObject *return_value = NULL;
+
+    Py_BEGIN_CRITICAL_SECTION(self);
+    return_value = _random_Random_getstate_impl(self);
+    Py_END_CRITICAL_SECTION();
+
+    return return_value;
 }
 
 PyDoc_STRVAR(_random_Random_setstate__doc__,
@@ -84,6 +99,21 @@ PyDoc_STRVAR(_random_Random_setstate__doc__,
 #define _RANDOM_RANDOM_SETSTATE_METHODDEF    \
     {"setstate", (PyCFunction)_random_Random_setstate, METH_O, _random_Random_setstate__doc__},
 
+static PyObject *
+_random_Random_setstate_impl(RandomObject *self, PyObject *state);
+
+static PyObject *
+_random_Random_setstate(RandomObject *self, PyObject *state)
+{
+    PyObject *return_value = NULL;
+
+    Py_BEGIN_CRITICAL_SECTION(self);
+    return_value = _random_Random_setstate_impl(self, state);
+    Py_END_CRITICAL_SECTION();
+
+    return return_value;
+}
+
 PyDoc_STRVAR(_random_Random_getrandbits__doc__,
 "getrandbits($self, k, /)\n"
 "--\n"
@@ -106,9 +136,11 @@ _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=5c800a28c2d7b9d1 input=a9049054013a1b77]*/
+/*[clinic end generated code: output=bf49ece1d341b1b6 input=a9049054013a1b77]*/