Skip to content

Commit

Permalink
Make SimpleQueue thread-safe with the GIL disabled
Browse files Browse the repository at this point in the history
Methods on SimpleQueue are protected with the per-object lock.
  • Loading branch information
mpage committed Jan 17, 2024
1 parent fa370a7 commit 323a446
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 9 deletions.
18 changes: 12 additions & 6 deletions Modules/_queuemodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,7 @@ maybe_unparked_thread(HandoffData *data, PyObject **item, int has_more_waiters)
}

/*[clinic input]
@critical_section
_queue.SimpleQueue.put
item: object
block: bool = True
Expand All @@ -279,7 +280,7 @@ never blocks. They are provided for compatibility with the Queue class.
static PyObject *
_queue_SimpleQueue_put_impl(simplequeueobject *self, PyObject *item,
int block, PyObject *timeout)
/*[clinic end generated code: output=4333136e88f90d8b input=6e601fa707a782d5]*/
/*[clinic end generated code: output=4333136e88f90d8b input=a16dbb33363c0fa8]*/
{
HandoffData data = {
.handed_off = 0,
Expand All @@ -300,6 +301,7 @@ _queue_SimpleQueue_put_impl(simplequeueobject *self, PyObject *item,
}

/*[clinic input]
@critical_section
_queue.SimpleQueue.put_nowait
item: object
Expand All @@ -312,7 +314,7 @@ for compatibility with the Queue class.

static PyObject *
_queue_SimpleQueue_put_nowait_impl(simplequeueobject *self, PyObject *item)
/*[clinic end generated code: output=0990536715efb1f1 input=36b1ea96756b2ece]*/
/*[clinic end generated code: output=0990536715efb1f1 input=ce949cc2cd8a4119]*/
{
return _queue_SimpleQueue_put_impl(self, item, 0, Py_None);
}
Expand All @@ -327,6 +329,7 @@ empty_error(PyTypeObject *cls)
}

/*[clinic input]
@critical_section
_queue.SimpleQueue.get
cls: defining_class
Expand All @@ -349,7 +352,7 @@ in that case).
static PyObject *
_queue_SimpleQueue_get_impl(simplequeueobject *self, PyTypeObject *cls,
int block, PyObject *timeout_obj)
/*[clinic end generated code: output=5c2cca914cd1e55b input=5b4047bfbc645ec1]*/
/*[clinic end generated code: output=5c2cca914cd1e55b input=f7836c65e5839c51]*/
{
_PyTime_t endtime = 0;

Expand Down Expand Up @@ -431,6 +434,7 @@ _queue_SimpleQueue_get_impl(simplequeueobject *self, PyTypeObject *cls,
}

/*[clinic input]
@critical_section
_queue.SimpleQueue.get_nowait
cls: defining_class
Expand All @@ -445,33 +449,35 @@ raise the Empty exception.
static PyObject *
_queue_SimpleQueue_get_nowait_impl(simplequeueobject *self,
PyTypeObject *cls)
/*[clinic end generated code: output=620c58e2750f8b8a input=842f732bf04216d3]*/
/*[clinic end generated code: output=620c58e2750f8b8a input=d48be63633fefae9]*/
{
return _queue_SimpleQueue_get_impl(self, cls, 0, Py_None);
}

/*[clinic input]
@critical_section
_queue.SimpleQueue.empty -> bool
Return True if the queue is empty, False otherwise (not reliable!).
[clinic start generated code]*/

static int
_queue_SimpleQueue_empty_impl(simplequeueobject *self)
/*[clinic end generated code: output=1a02a1b87c0ef838 input=1a98431c45fd66f9]*/
/*[clinic end generated code: output=1a02a1b87c0ef838 input=96cb22df5a67d831]*/
{
return self->buf.num_items == 0;
}

/*[clinic input]
@critical_section
_queue.SimpleQueue.qsize -> Py_ssize_t
Return the approximate size of the queue (not reliable!).
[clinic start generated code]*/

static Py_ssize_t
_queue_SimpleQueue_qsize_impl(simplequeueobject *self)
/*[clinic end generated code: output=f9dcd9d0a90e121e input=7a74852b407868a1]*/
/*[clinic end generated code: output=f9dcd9d0a90e121e input=e218623cb8c16a79]*/
{
return self->buf.num_items;
}
Expand Down
24 changes: 21 additions & 3 deletions Modules/clinic/_queuemodule.c.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 323a446

Please sign in to comment.