diff options
author | Donghee Na <donghee.na@python.org> | 2024-03-10 00:45:42 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-03-10 00:45:42 (GMT) |
commit | 5b2f21faf388d8de5b388996cfd4f03430085764 (patch) | |
tree | 90027d649da8c4b1fcc5bc3d846c2f2c2e4274e2 /Objects | |
parent | 17d31bf3843c384873999a15ce683cc3654f46ae (diff) | |
download | cpython-5b2f21faf388d8de5b388996cfd4f03430085764.zip cpython-5b2f21faf388d8de5b388996cfd4f03430085764.tar.gz cpython-5b2f21faf388d8de5b388996cfd4f03430085764.tar.bz2 |
gh-112087: Make list.sort to be thread-safe for PEP 703. (gh-116553)
Diffstat (limited to 'Objects')
-rw-r--r-- | Objects/clinic/listobject.c.h | 4 | ||||
-rw-r--r-- | Objects/listobject.c | 11 |
2 files changed, 10 insertions, 5 deletions
diff --git a/Objects/clinic/listobject.c.h b/Objects/clinic/listobject.c.h index a61550a..b90dc0a 100644 --- a/Objects/clinic/listobject.c.h +++ b/Objects/clinic/listobject.c.h @@ -268,7 +268,9 @@ list_sort(PyListObject *self, PyObject *const *args, Py_ssize_t nargs, PyObject goto exit; } skip_optional_kwonly: + Py_BEGIN_CRITICAL_SECTION(self); return_value = list_sort_impl(self, keyfunc, reverse); + Py_END_CRITICAL_SECTION(); exit: return return_value; @@ -452,4 +454,4 @@ list___reversed__(PyListObject *self, PyObject *Py_UNUSED(ignored)) { return list___reversed___impl(self); } -/*[clinic end generated code: output=26dfb2c9846348f9 input=a9049054013a1b77]*/ +/*[clinic end generated code: output=a77eda9931ec0c20 input=a9049054013a1b77]*/ diff --git a/Objects/listobject.c b/Objects/listobject.c index c2aaab0..164f363 100644 --- a/Objects/listobject.c +++ b/Objects/listobject.c @@ -2577,6 +2577,7 @@ unsafe_tuple_compare(PyObject *v, PyObject *w, MergeState *ms) * duplicated). */ /*[clinic input] +@critical_section list.sort * @@ -2596,7 +2597,7 @@ The reverse flag can be set to sort in descending order. static PyObject * list_sort_impl(PyListObject *self, PyObject *keyfunc, int reverse) -/*[clinic end generated code: output=57b9f9c5e23fbe42 input=a74c4cd3ec6b5c08]*/ +/*[clinic end generated code: output=57b9f9c5e23fbe42 input=667bf25d0e3a3676]*/ { MergeState ms; Py_ssize_t nremaining; @@ -2623,7 +2624,7 @@ list_sort_impl(PyListObject *self, PyObject *keyfunc, int reverse) saved_ob_item = self->ob_item; saved_allocated = self->allocated; Py_SET_SIZE(self, 0); - self->ob_item = NULL; + FT_ATOMIC_STORE_PTR_RELEASE(self->ob_item, NULL); self->allocated = -1; /* any operation will reset it to >= 0 */ if (keyfunc == NULL) { @@ -2843,8 +2844,8 @@ keyfunc_fail: final_ob_item = self->ob_item; i = Py_SIZE(self); Py_SET_SIZE(self, saved_ob_size); - self->ob_item = saved_ob_item; - self->allocated = saved_allocated; + FT_ATOMIC_STORE_PTR_RELEASE(self->ob_item, saved_ob_item); + FT_ATOMIC_STORE_SSIZE_RELAXED(self->allocated, saved_allocated); if (final_ob_item != NULL) { /* we cannot use list_clear() for this because it does not guarantee that the list is really empty when it returns */ @@ -2870,7 +2871,9 @@ PyList_Sort(PyObject *v) PyErr_BadInternalCall(); return -1; } + Py_BEGIN_CRITICAL_SECTION(v); v = list_sort_impl((PyListObject *)v, NULL, 0); + Py_END_CRITICAL_SECTION(); if (v == NULL) return -1; Py_DECREF(v); |