summaryrefslogtreecommitdiffstats
path: root/Modules/clinic
diff options
context:
space:
mode:
authormpage <mpage@meta.com>2024-01-23 19:25:41 (GMT)
committerGitHub <noreply@github.com>2024-01-23 19:25:41 (GMT)
commit925907ea362c4c014086be48625ac7dd67645cfc (patch)
tree2c6ad103f5296532d8cd8e0bcdb66623a3ad636d /Modules/clinic
parent441affc9e7f419ef0b68f734505fa2f79fe653c7 (diff)
downloadcpython-925907ea362c4c014086be48625ac7dd67645cfc.zip
cpython-925907ea362c4c014086be48625ac7dd67645cfc.tar.gz
cpython-925907ea362c4c014086be48625ac7dd67645cfc.tar.bz2
gh-113884: Make queue.SimpleQueue thread-safe when the GIL is disabled (#114161)
* use the ParkingLot API to manage waiting threads * use Argument Clinic's critical section directive to protect queue methods * remove unnecessary overflow check Co-authored-by: Erlend E. Aasland <erlend.aasland@protonmail.com>
Diffstat (limited to 'Modules/clinic')
-rw-r--r--Modules/clinic/_queuemodule.c.h24
1 files changed, 21 insertions, 3 deletions
diff --git a/Modules/clinic/_queuemodule.c.h b/Modules/clinic/_queuemodule.c.h
index 8e2a430..b3b6b8e 100644
--- a/Modules/clinic/_queuemodule.c.h
+++ b/Modules/clinic/_queuemodule.c.h
@@ -6,6 +6,7 @@ preserve
# include "pycore_gc.h" // PyGC_Head
# include "pycore_runtime.h" // _Py_ID()
#endif
+#include "pycore_critical_section.h"// Py_BEGIN_CRITICAL_SECTION()
#include "pycore_modsupport.h" // _PyArg_NoKeywords()
PyDoc_STRVAR(simplequeue_new__doc__,
@@ -107,7 +108,9 @@ _queue_SimpleQueue_put(simplequeueobject *self, PyObject *const *args, Py_ssize_
}
timeout = args[2];
skip_optional_pos:
+ Py_BEGIN_CRITICAL_SECTION(self);
return_value = _queue_SimpleQueue_put_impl(self, item, block, timeout);
+ Py_END_CRITICAL_SECTION();
exit:
return return_value;
@@ -165,7 +168,9 @@ _queue_SimpleQueue_put_nowait(simplequeueobject *self, PyObject *const *args, Py
goto exit;
}
item = args[0];
+ Py_BEGIN_CRITICAL_SECTION(self);
return_value = _queue_SimpleQueue_put_nowait_impl(self, item);
+ Py_END_CRITICAL_SECTION();
exit:
return return_value;
@@ -244,7 +249,9 @@ _queue_SimpleQueue_get(simplequeueobject *self, PyTypeObject *cls, PyObject *con
}
timeout_obj = args[1];
skip_optional_pos:
+ Py_BEGIN_CRITICAL_SECTION(self);
return_value = _queue_SimpleQueue_get_impl(self, cls, block, timeout_obj);
+ Py_END_CRITICAL_SECTION();
exit:
return return_value;
@@ -269,11 +276,18 @@ _queue_SimpleQueue_get_nowait_impl(simplequeueobject *self,
static PyObject *
_queue_SimpleQueue_get_nowait(simplequeueobject *self, PyTypeObject *cls, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
{
+ PyObject *return_value = NULL;
+
if (nargs) {
PyErr_SetString(PyExc_TypeError, "get_nowait() takes no arguments");
- return NULL;
+ goto exit;
}
- return _queue_SimpleQueue_get_nowait_impl(self, cls);
+ Py_BEGIN_CRITICAL_SECTION(self);
+ return_value = _queue_SimpleQueue_get_nowait_impl(self, cls);
+ Py_END_CRITICAL_SECTION();
+
+exit:
+ return return_value;
}
PyDoc_STRVAR(_queue_SimpleQueue_empty__doc__,
@@ -294,7 +308,9 @@ _queue_SimpleQueue_empty(simplequeueobject *self, PyObject *Py_UNUSED(ignored))
PyObject *return_value = NULL;
int _return_value;
+ Py_BEGIN_CRITICAL_SECTION(self);
_return_value = _queue_SimpleQueue_empty_impl(self);
+ Py_END_CRITICAL_SECTION();
if ((_return_value == -1) && PyErr_Occurred()) {
goto exit;
}
@@ -322,7 +338,9 @@ _queue_SimpleQueue_qsize(simplequeueobject *self, PyObject *Py_UNUSED(ignored))
PyObject *return_value = NULL;
Py_ssize_t _return_value;
+ Py_BEGIN_CRITICAL_SECTION(self);
_return_value = _queue_SimpleQueue_qsize_impl(self);
+ Py_END_CRITICAL_SECTION();
if ((_return_value == -1) && PyErr_Occurred()) {
goto exit;
}
@@ -331,4 +349,4 @@ _queue_SimpleQueue_qsize(simplequeueobject *self, PyObject *Py_UNUSED(ignored))
exit:
return return_value;
}
-/*[clinic end generated code: output=457310b20cb61cf8 input=a9049054013a1b77]*/
+/*[clinic end generated code: output=242950edc8f7dfd7 input=a9049054013a1b77]*/