Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 34k
gh-112087: Make list_{slice, ass_slice, subscript} to be threadsafe#116233
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
Uh oh!
There was an error while loading. Please reload this page.
Merged
Changes from all commits
Commits
Show all changes
12 commits Select commit Hold shift + click to select a range
d9ffc53 gh-112087: Make list_{slice, ass_slice, subscript} to be threadsafe
corona10 cab2d99 Remove unnecessary atomic operations
corona10 8b0120d Update
corona10 1e19253 Address code review
corona10 c25ca79 nit
corona10 1ed3329 Change name
corona10 033c141 Fix
corona10 12529a8 Remove inline
corona10 3335211 fix
corona10 11b3017 Address code review
corona10 9ac76ca Address code review
corona10 6c037d4 Add test case
corona10 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -515,7 +515,7 @@ list_item(PyObject *aa, Py_ssize_t i) | ||
| } | ||
| static PyObject * | ||
| list_slice(PyListObject *a, Py_ssize_t ilow, Py_ssize_t ihigh) | ||
colesbury marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading. Please reload this page. | ||
| list_slice_lock_held(PyListObject *a, Py_ssize_t ilow, Py_ssize_t ihigh) | ||
| { | ||
| PyListObject *np; | ||
| PyObject **src, **dest; | ||
| @@ -559,7 +559,7 @@ PyList_GetSlice(PyObject *a, Py_ssize_t ilow, Py_ssize_t ihigh) | ||
| else if (ihigh > Py_SIZE(a)){ | ||
| ihigh = Py_SIZE(a); | ||
| } | ||
| ret = list_slice((PyListObject *)a, ilow, ihigh); | ||
| ret = list_slice_lock_held((PyListObject *)a, ilow, ihigh); | ||
| Py_END_CRITICAL_SECTION(); | ||
| return ret; | ||
| } | ||
| @@ -584,13 +584,13 @@ list_concat_lock_held(PyListObject *a, PyListObject *b) | ||
| dest = np->ob_item; | ||
| for (i = 0; i < Py_SIZE(a); i++){ | ||
| PyObject *v = src[i]; | ||
| FT_ATOMIC_STORE_PTR_RELAXED(dest[i], Py_NewRef(v)); | ||
corona10 marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading. Please reload this page. | ||
| dest[i] = Py_NewRef(v); | ||
| } | ||
| src = b->ob_item; | ||
| dest = np->ob_item + Py_SIZE(a); | ||
| for (i = 0; i < Py_SIZE(b); i++){ | ||
| PyObject *v = src[i]; | ||
| FT_ATOMIC_STORE_PTR_RELAXED(dest[i], Py_NewRef(v)); | ||
| dest[i] = Py_NewRef(v); | ||
| } | ||
| Py_SET_SIZE(np, size); | ||
| return (PyObject *)np; | ||
| @@ -636,15 +636,15 @@ list_repeat_lock_held(PyListObject *a, Py_ssize_t n) | ||
| _Py_RefcntAdd(elem, n); | ||
| PyObject **dest_end = dest + output_size; | ||
| while (dest < dest_end){ | ||
| FT_ATOMIC_STORE_PTR_RELAXED(*dest++, elem); | ||
| *dest++ = elem; | ||
corona10 marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading. Please reload this page. | ||
| } | ||
| } | ||
| else{ | ||
| PyObject **src = a->ob_item; | ||
| PyObject **src_end = src + input_size; | ||
| while (src < src_end){ | ||
| _Py_RefcntAdd(*src, n); | ||
| FT_ATOMIC_STORE_PTR_RELAXED(*dest++, *src++); | ||
| *dest++ = *src++; | ||
| } | ||
| _Py_memory_repeat((char *)np->ob_item, sizeof(PyObject *)*output_size, | ||
| @@ -718,7 +718,7 @@ list_clear_slot(PyObject *self) | ||
| * guaranteed the call cannot fail. | ||
| */ | ||
| static int | ||
| list_ass_slice(PyListObject *a, Py_ssize_t ilow, Py_ssize_t ihigh, PyObject *v) | ||
| list_ass_slice_lock_held(PyListObject *a, Py_ssize_t ilow, Py_ssize_t ihigh, PyObject *v) | ||
| { | ||
| /* Because [X]DECREF can recursively invoke list operations on | ||
| this list, we must postpone all [X]DECREF activity until | ||
| @@ -741,15 +741,6 @@ list_ass_slice(PyListObject *a, Py_ssize_t ilow, Py_ssize_t ihigh, PyObject *v) | ||
| if (v == NULL) | ||
| n = 0; | ||
| else{ | ||
| if (a == b){ | ||
| /* Special case "a[i:j] = a" -- copy b first */ | ||
| v = list_slice(b, 0, Py_SIZE(b)); | ||
| if (v == NULL) | ||
| return result; | ||
| result = list_ass_slice(a, ilow, ihigh, v); | ||
| Py_DECREF(v); | ||
| return result; | ||
| } | ||
| v_as_SF = PySequence_Fast(v, "can only assign an iterable"); | ||
| if(v_as_SF == NULL) | ||
| goto Error; | ||
| @@ -823,6 +814,34 @@ list_ass_slice(PyListObject *a, Py_ssize_t ilow, Py_ssize_t ihigh, PyObject *v) | ||
| #undef b | ||
| } | ||
| static int | ||
| list_ass_slice(PyListObject *a, Py_ssize_t ilow, Py_ssize_t ihigh, PyObject *v) | ||
| { | ||
| int ret; | ||
| if (a == (PyListObject *)v){ | ||
| Py_BEGIN_CRITICAL_SECTION(a); | ||
| Py_ssize_t n = PyList_GET_SIZE(a); | ||
| PyObject *copy = list_slice_lock_held(a, 0, n); | ||
| if (copy == NULL){ | ||
| return -1; | ||
| } | ||
| ret = list_ass_slice_lock_held(a, ilow, ihigh, copy); | ||
| Py_DECREF(copy); | ||
| Py_END_CRITICAL_SECTION(); | ||
| } | ||
| else if (v != NULL && PyList_CheckExact(v)){ | ||
| Py_BEGIN_CRITICAL_SECTION2(a, v); | ||
| ret = list_ass_slice_lock_held(a, ilow, ihigh, v); | ||
| Py_END_CRITICAL_SECTION2(); | ||
| } | ||
| else{ | ||
| Py_BEGIN_CRITICAL_SECTION(a); | ||
| ret = list_ass_slice_lock_held(a, ilow, ihigh, v); | ||
| Py_END_CRITICAL_SECTION(); | ||
| } | ||
| return ret; | ||
| } | ||
| int | ||
| PyList_SetSlice(PyObject *a, Py_ssize_t ilow, Py_ssize_t ihigh, PyObject *v) | ||
| { | ||
| @@ -956,7 +975,7 @@ static PyObject * | ||
| list_copy_impl(PyListObject *self) | ||
| /*[clinic end generated code: output=ec6b72d6209d418e input=81c54b0c7bb4f73d]*/ | ||
| { | ||
| return list_slice(self, 0, Py_SIZE(self)); | ||
| return list_slice_lock_held(self, 0, Py_SIZE(self)); | ||
| } | ||
| /*[clinic input] | ||
| @@ -2884,8 +2903,7 @@ list_remove_impl(PyListObject *self, PyObject *value) | ||
| int cmp = PyObject_RichCompareBool(obj, value, Py_EQ); | ||
| Py_DECREF(obj); | ||
| if (cmp > 0){ | ||
| if (list_ass_slice(self, i, i+1, | ||
| (PyObject *)NULL) == 0) | ||
| if (list_ass_slice_lock_held(self, i, i+1, NULL) == 0) | ||
| Py_RETURN_NONE; | ||
| return NULL; | ||
| } | ||
| @@ -3085,6 +3103,45 @@ static PySequenceMethods list_as_sequence ={ | ||
| list_inplace_repeat, /* sq_inplace_repeat */ | ||
| }; | ||
| static inline PyObject * | ||
| list_slice_step_lock_held(PyListObject *a, Py_ssize_t start, Py_ssize_t step, Py_ssize_t len) | ||
| { | ||
| PyListObject *np = (PyListObject *)list_new_prealloc(len); | ||
| if (np == NULL){ | ||
| return NULL; | ||
| } | ||
| size_t cur; | ||
| Py_ssize_t i; | ||
| PyObject **src = a->ob_item; | ||
| PyObject **dest = np->ob_item; | ||
| for (cur = start, i = 0; i < len; | ||
| cur += (size_t)step, i++){ | ||
| PyObject *v = src[cur]; | ||
| dest[i] = Py_NewRef(v); | ||
| } | ||
| Py_SET_SIZE(np, len); | ||
| return (PyObject *)np; | ||
| } | ||
| static PyObject * | ||
| list_slice_wrap(PyListObject *aa, Py_ssize_t start, Py_ssize_t stop, Py_ssize_t step) | ||
| { | ||
| PyObject *res = NULL; | ||
| Py_BEGIN_CRITICAL_SECTION(aa); | ||
| Py_ssize_t len = PySlice_AdjustIndices(Py_SIZE(aa), &start, &stop, step); | ||
| if (len <= 0){ | ||
| res = PyList_New(0); | ||
| } | ||
| else if (step == 1){ | ||
| res = list_slice_lock_held(aa, start, stop); | ||
| } | ||
| else{ | ||
| res = list_slice_step_lock_held(aa, start, step, len); | ||
| } | ||
| Py_END_CRITICAL_SECTION(); | ||
| return res; | ||
| } | ||
| static PyObject * | ||
| list_subscript(PyObject* _self, PyObject* item) | ||
| { | ||
| @@ -3099,38 +3156,11 @@ list_subscript(PyObject* _self, PyObject* item) | ||
| return list_item((PyObject *)self, i); | ||
| } | ||
| else if (PySlice_Check(item)){ | ||
| Py_ssize_t start, stop, step, slicelength, i; | ||
| size_t cur; | ||
| PyObject* result; | ||
| PyObject* it; | ||
| PyObject **src, **dest; | ||
| Py_ssize_t start, stop, step; | ||
| if (PySlice_Unpack(item, &start, &stop, &step) < 0){ | ||
| return NULL; | ||
| } | ||
| slicelength = PySlice_AdjustIndices(Py_SIZE(self), &start, &stop, | ||
| step); | ||
| if (slicelength <= 0){ | ||
| return PyList_New(0); | ||
| } | ||
| else if (step == 1){ | ||
| return list_slice(self, start, stop); | ||
| } | ||
| else{ | ||
| result = list_new_prealloc(slicelength); | ||
| if (!result) return NULL; | ||
| src = self->ob_item; | ||
| dest = ((PyListObject *)result)->ob_item; | ||
| for (cur = start, i = 0; i < slicelength; | ||
| cur += (size_t)step, i++){ | ||
| it = Py_NewRef(src[cur]); | ||
| dest[i] = it; | ||
| } | ||
| Py_SET_SIZE(result, slicelength); | ||
| return result; | ||
| } | ||
| return list_slice_wrap(self, start, stop, step); | ||
| } | ||
| else{ | ||
| PyErr_Format(PyExc_TypeError, | ||
| @@ -3241,8 +3271,10 @@ list_ass_subscript(PyObject* _self, PyObject* item, PyObject* value) | ||
| /* protect against a[::-1] = a */ | ||
| if (self == (PyListObject*)value){ | ||
| seq = list_slice((PyListObject*)value, 0, | ||
| PyList_GET_SIZE(value)); | ||
| Py_BEGIN_CRITICAL_SECTION(value); | ||
| seq = list_slice_lock_held((PyListObject*)value, 0, | ||
| Py_SIZE(value)); | ||
| Py_END_CRITICAL_SECTION(); | ||
| } | ||
| else{ | ||
| seq = PySequence_Fast(value, | ||
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.