summaryrefslogtreecommitdiffstats
path: root/Modules/rotatingtree.c
diff options
context:
space:
mode:
authorAN Long <aisk@users.noreply.github.com>2024-02-29 23:04:16 (GMT)
committerGitHub <noreply@github.com>2024-02-29 23:04:16 (GMT)
commitca56c3a172e4ca08c278ddaf66ac2943a1d93288 (patch)
tree679d69bc4e6855acc499abf542770bb19ef9d804 /Modules/rotatingtree.c
parent6a95676bb526261434dd068d6c49927c44d24a9b (diff)
downloadcpython-ca56c3a172e4ca08c278ddaf66ac2943a1d93288.zip
cpython-ca56c3a172e4ca08c278ddaf66ac2943a1d93288.tar.gz
cpython-ca56c3a172e4ca08c278ddaf66ac2943a1d93288.tar.bz2
gh-103092: Add a mutex to make the PRNG state of rotatingtree concurrent-safe (#115301)
Diffstat (limited to 'Modules/rotatingtree.c')
-rw-r--r--Modules/rotatingtree.c9
1 files changed, 9 insertions, 0 deletions
diff --git a/Modules/rotatingtree.c b/Modules/rotatingtree.c
index 07e08bc..217e495 100644
--- a/Modules/rotatingtree.c
+++ b/Modules/rotatingtree.c
@@ -1,3 +1,9 @@
+#ifndef Py_BUILD_CORE_BUILTIN
+# define Py_BUILD_CORE_MODULE 1
+#endif
+
+#include "Python.h"
+#include "pycore_lock.h"
#include "rotatingtree.h"
#define KEY_LOWER_THAN(key1, key2) ((char*)(key1) < (char*)(key2))
@@ -10,17 +16,20 @@
static unsigned int random_value = 1;
static unsigned int random_stream = 0;
+static PyMutex random_mutex = {0};
static int
randombits(int bits)
{
int result;
+ PyMutex_Lock(&random_mutex);
if (random_stream < (1U << bits)) {
random_value *= 1082527;
random_stream = random_value;
}
result = random_stream & ((1<<bits)-1);
random_stream >>= bits;
+ PyMutex_Unlock(&random_mutex);
return result;
}