Skip to content

Commit

Permalink
pythongh-112087: Update list impl to be thread-safe with manual CS (p…
Browse files Browse the repository at this point in the history
  • Loading branch information
corona10 committed Jan 16, 2024
1 parent 5094690 commit 42b90cf
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 18 deletions.
40 changes: 38 additions & 2 deletions Objects/clinic/listobject.c.h

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

69 changes: 53 additions & 16 deletions Objects/listobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -263,15 +263,22 @@ PyList_SetItem(PyObject *op, Py_ssize_t i,
PyErr_BadInternalCall();
return -1;
}
if (!valid_index(i, Py_SIZE(op))) {
int ret;
PyListObject *self = ((PyListObject *)op);
Py_BEGIN_CRITICAL_SECTION(self);
if (!valid_index(i, Py_SIZE(self))) {
Py_XDECREF(newitem);
PyErr_SetString(PyExc_IndexError,
"list assignment index out of range");
return -1;
ret = -1;
goto end;
}
p = ((PyListObject *)op) -> ob_item + i;
p = self->ob_item + i;
Py_XSETREF(*p, newitem);
return 0;
ret = 0;
end:
Py_END_CRITICAL_SECTION();
return ret;
}

static int
Expand Down Expand Up @@ -309,7 +316,12 @@ PyList_Insert(PyObject *op, Py_ssize_t where, PyObject *newitem)
PyErr_BadInternalCall();
return -1;
}
return ins1((PyListObject *)op, where, newitem);
PyListObject *self = (PyListObject *)op;
int err;
Py_BEGIN_CRITICAL_SECTION(self);
err = ins1(self, where, newitem);
Py_END_CRITICAL_SECTION();
return err;
}

/* internal, used by _PyList_AppendTakeRef */
Expand Down Expand Up @@ -804,9 +816,13 @@ static PyObject *
list_insert_impl(PyListObject *self, Py_ssize_t index, PyObject *object)
/*[clinic end generated code: output=7f35e32f60c8cb78 input=b1987ca998a4ae2d]*/
{
if (ins1(self, index, object) == 0)
Py_RETURN_NONE;
return NULL;
PyObject *ret = Py_None;
Py_BEGIN_CRITICAL_SECTION(self);
if (ins1(self, index, object) < 0) {
ret = NULL;
}
Py_END_CRITICAL_SECTION();
return ret;
}

/*[clinic input]
Expand All @@ -825,19 +841,21 @@ py_list_clear_impl(PyListObject *self)
}

/*[clinic input]
@critical_section
list.copy
Return a shallow copy of the list.
[clinic start generated code]*/

static PyObject *
list_copy_impl(PyListObject *self)
/*[clinic end generated code: output=ec6b72d6209d418e input=6453ab159e84771f]*/
/*[clinic end generated code: output=ec6b72d6209d418e input=81c54b0c7bb4f73d]*/
{
return list_slice(self, 0, Py_SIZE(self));
}

/*[clinic input]
@critical_section
list.append
object: object
Expand All @@ -847,8 +865,8 @@ Append object to the end of the list.
[clinic start generated code]*/

static PyObject *
list_append(PyListObject *self, PyObject *object)
/*[clinic end generated code: output=7c096003a29c0eae input=43a3fe48a7066e91]*/
list_append_impl(PyListObject *self, PyObject *object)
/*[clinic end generated code: output=78423561d92ed405 input=122b0853de54004f]*/
{
if (_PyList_AppendTakeRef(self, Py_NewRef(object)) < 0) {
return NULL;
Expand Down Expand Up @@ -1006,6 +1024,7 @@ _PyList_Extend(PyListObject *self, PyObject *iterable)


/*[clinic input]
@critical_section self iterable
list.extend as py_list_extend
iterable: object
Expand All @@ -1015,8 +1034,8 @@ Extend list by appending elements from the iterable.
[clinic start generated code]*/

static PyObject *
py_list_extend(PyListObject *self, PyObject *iterable)
/*[clinic end generated code: output=b8e0bff0ceae2abd input=9a8376a8633ed3ba]*/
py_list_extend_impl(PyListObject *self, PyObject *iterable)
/*[clinic end generated code: output=a2f115ceace2c845 input=1d42175414e1a5f3]*/
{
return _PyList_Extend(self, iterable);
}
Expand Down Expand Up @@ -2612,8 +2631,11 @@ PyList_Reverse(PyObject *v)
PyErr_BadInternalCall();
return -1;
}
if (Py_SIZE(self) > 1)
Py_BEGIN_CRITICAL_SECTION(self);
if (Py_SIZE(self) > 1) {
reverse_slice(self->ob_item, self->ob_item + Py_SIZE(self));
}
Py_END_CRITICAL_SECTION()
return 0;
}

Expand All @@ -2624,7 +2646,12 @@ PyList_AsTuple(PyObject *v)
PyErr_BadInternalCall();
return NULL;
}
return _PyTuple_FromArray(((PyListObject *)v)->ob_item, Py_SIZE(v));
PyObject *ret;
PyListObject *self = (PyListObject *)v;
Py_BEGIN_CRITICAL_SECTION(self);
ret = _PyTuple_FromArray(self->ob_item, Py_SIZE(v));
Py_END_CRITICAL_SECTION();
return ret;
}

PyObject *
Expand Down Expand Up @@ -2773,7 +2800,7 @@ list_traverse(PyObject *self, visitproc visit, void *arg)
}

static PyObject *
list_richcompare(PyObject *v, PyObject *w, int op)
list_richcompare_impl(PyObject *v, PyObject *w, int op)
{
PyListObject *vl, *wl;
Py_ssize_t i;
Expand Down Expand Up @@ -2828,6 +2855,16 @@ list_richcompare(PyObject *v, PyObject *w, int op)
return PyObject_RichCompare(vl->ob_item[i], wl->ob_item[i], op);
}

static PyObject *
list_richcompare(PyObject *v, PyObject *w, int op)
{
PyObject *ret;
Py_BEGIN_CRITICAL_SECTION2(v, w);
ret = list_richcompare_impl(v, w, op);
Py_END_CRITICAL_SECTION2()
return ret;
}

/*[clinic input]
list.__init__
Expand Down

0 comments on commit 42b90cf

Please sign in to comment.