8000 gh-123471: Make concurrent iteration over itertools.pairwise safe under free-threading by eendebakpt · Pull Request #123848 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

gh-123471: Make concurrent iteration over itertools.pairwise safe under free-threading #123848

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

Closed
wants to merge 8 commits into from
Closed
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 setting po->old atomic
  • Loading branch information
eendebakpt committed Sep 8, 2024
commit eed6486f1bf6a4058ec94f60b6f396a98972e2d2
24 changes: 18 additions & 6 deletions Modules/itertoolsmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -350,29 +350,34 @@ pairwise_next(pairwiseobject *po)
if (old == NULL) {
old = (*Py_TYPE(it)->tp_iternext)(it);
Py_XSETREF(po->old, old);
if (old == NULL) {
#ifndef Py_GIL_DISABLED
if (old == NULL) {
Py_CLEAR(po->it);
#else
_Py_atomic_store_int_relaxed(&po->iterator_exhausted, 1);
#endif
return NULL;
}
it = po->it;
if (it == NULL) {
Py_CLEAR(po->old);
return NULL;
}
#else
if (old == NULL) {
_Py_atomic_store_int_relaxed(&po->iterator_exhausted, 1);
return NULL;
}
#endif
}
Py_INCREF(old);
new = (*Py_TYPE(it)->tp_iternext)(it);
if (new == NULL) {
#ifndef Py_GIL_DISABLED
Py_CLEAR(po->it);
Py_CLEAR(po->old);
#else
_Py_atomic_store_int_relaxed(&po->iterator_exhausted, 1);
PyObject *po_old = ( PyObject *)_Py_atomic_exchange_ptr(&po->old, 0);
Py_XDECREF(po_old);
#endif
Py_CLEAR(po->old);
Py_DECREF(old);
return NULL;
}
Expand Down Expand Up @@ -400,7 +405,14 @@ pairwise_next(pairwiseobject *po)
}
}

Py_XSETREF(po->old, new); // this should be atomic in the FT build
#ifndef Py_GIL_DISABLED
Py_XSETREF(po->old, new);
#else
// this should be atomic in the FT build
PyObject *po_old = ( PyObject *)_Py_atomic_exchange_ptr(&po->old, new);
Py_XDECREF(po_old);
#endif

Py_DECREF(old);
return result;
}
Expand Down
0