Skip to content

Commit

Permalink
Make list_ass_item to be thread-safe
Browse files Browse the repository at this point in the history
  • Loading branch information
corona10 committed Feb 17, 2024
1 parent dc4e52f commit b1293ed
Showing 1 changed file with 24 additions and 5 deletions.
29 changes: 24 additions & 5 deletions Objects/listobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -853,20 +853,39 @@ list_inplace_repeat(PyObject *_self, Py_ssize_t n)
}

static int
list_ass_item(PyObject *aa, Py_ssize_t i, PyObject *v)
list_ass_item_locked(PyListObject *a, Py_ssize_t i, PyObject *v)
{
PyListObject *a = (PyListObject *)aa;
if (!valid_index(i, Py_SIZE(a))) {
PyErr_SetString(PyExc_IndexError,
"list assignment index out of range");
return -1;
}
if (v == NULL)
return list_ass_slice(a, i, i+1, v);
Py_SETREF(a->ob_item[i], Py_NewRef(v));
PyObject *tmp = a->ob_item[i];
if (v == NULL) {
Py_ssize_t size = Py_SIZE(a);
for (Py_ssize_t idx = i; idx < size - 1; idx++) {
FT_ATOMIC_STORE_PTR_RELAXED(a->ob_item[idx], a->ob_item[idx + 1]);
}
Py_SET_SIZE(a, size - 1);
}
else {
FT_ATOMIC_STORE_PTR_RELEASE(a->ob_item[i], Py_NewRef(v));
}
Py_DECREF(tmp);
return 0;
}

static int
list_ass_item(PyObject *aa, Py_ssize_t i, PyObject *v)
{
int ret;
PyListObject *a = (PyListObject *)aa;
Py_BEGIN_CRITICAL_SECTION(a);
ret = list_ass_item_locked(a, i, v);
Py_END_CRITICAL_SECTION();
return ret;
}

/*[clinic input]
@critical_section
list.insert
Expand Down

0 comments on commit b1293ed

Please sign in to comment.