8000 gh-112087: Make PyList_{Append,Size,GetSlice} to be thread-safe by corona10 · Pull Request #114651 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

gh-112087: Make PyList_{Append,Size,GetSlice} to be thread-safe #114651

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
Jan 31, 2024
Merged
Show file tree
Hide file tree
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
Address code review
  • Loading branch information
corona10 committed Jan 30, 2024
commit 553db97d744ecf8d8f413a5335a6208396dd225f
2 changes: 1 addition & 1 deletion Include/object.h
Original file line number Diff line number Diff line change
Expand Up @@ -429,7 +429,7 @@ static inline void Py_SET_SIZE(PyVarObject *ob, Py_ssize_t size) {
assert(ob->ob_base.ob_type != &PyLong_Type);
assert(ob->ob_base.ob_type != &PyBool_Type);
#ifdef Py_GIL_DISABLED
_Py_atomic_store_ssize_relaxed(&(_PyVarObject_CAST(ob)->ob_size), size);
_Py_atomic_store_ssize_relaxed(&ob->ob_size, size);
#else
ob->ob_size = size;
#endif
Expand Down
18 changes: 6 additions & 12 deletions Objects/listobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,6 @@ get_list_state(void)
}
#endif

#ifdef Py_GIL_DISABLED
#define _Py_SET_ITEMREF(op, i, v) _Py_atomic_store_ptr_relaxed(&((PyListObject *)(op))->ob_item[i], Py_NewRef(v))
#else
#define _Py_SET_ITEMREF(op, i, v) ((PyListObject *)(op))->ob_item[i] = Py_NewRef(v)
#endif


/* Ensure ob_item has room for at least newsize elements, and set
* ob_size to newsize. If newsize > ob_size on entry, the content
* of the new slots at exit is undefined heap trash; it's the caller's
Expand Down Expand Up @@ -252,7 +245,7 @@ PyList_GetItem(PyObject *op, Py_ssize_t i)
PyErr_BadInternalCall();
return NULL;
}
if (!valid_index(i, PyList_GET_SIZE(op))) {
if (!valid_index(i, Py_SIZE(op))) {
_Py_DECLARE_STR(list_err, "list index out of range");
PyErr_SetObject(PyExc_IndexError, &_Py_STR(list_err));
return NULL;
Expand Down Expand Up @@ -348,15 +341,15 @@ _PyList_AppendTakeRefListResize(PyListObject *self, PyObject *newitem)
int
PyList_Append(PyObject *op, PyObject *newitem)
{
int ret = -1;
if (PyList_Check(op) && (newitem != NULL)) {
int ret;
Py_BEGIN_CRITICAL_SECTION(op);
ret = _PyList_AppendTakeRef((PyListObject *)op, Py_NewRef(newitem));
Py_END_CRITICAL_SECTION();
return ret;
}
PyErr_BadInternalCall();
return ret;
return -1;
}

/* Methods */
Expand Down Expand Up @@ -495,7 +488,7 @@ static PyObject *
list_slice(PyListObject *a, Py_ssize_t ilow, Py_ssize_t ihigh)
{
PyListObject *np;
PyObject **src;
PyObject **src, **dest;
Py_ssize_t i, len;
len = ihigh - ilow;
if (len <= 0) {
Expand All @@ -506,9 +499,10 @@ list_slice(PyListObject *a, Py_ssize_t ilow, Py_ssize_t ihigh)
return NULL;

src = a->ob_item + ilow;
dest = np->ob_item;
for (i = 0; i < len; i++) {
PyObject *v = src[i];
_Py_SET_ITEMREF(np, i, v);
dest[i] = Py_NewRef(v);
}
Py_SET_SIZE(np, len);
return (PyObject *)np;
Expand Down
0