10000 gh-112087: Make list_{concat, repeat, inplace_repeat, ass_item) to be thread-safe by corona10 · Pull Request #115605 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

gh-112087: Make list_{concat, repeat, inplace_repeat, ass_item) to be thread-safe #115605

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Feb 21, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Make list_ass_item to be thread-safe
  • Loading branch information
corona10 committed Feb 17, 2024
commit b1293edc8ed18893734440677e84ca8f5530b95c
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