-
-
Notifications
You must be signed in to change notification settings - Fork 33.2k
gh-127582: Make object resurrection thread-safe for free threading. #127612
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
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -697,8 +697,52 @@ _PyObject_SetMaybeWeakref(PyObject *op) | |
} | ||
} | ||
|
||
extern int _PyObject_ResurrectEndSlow(PyObject *op); | ||
#endif | ||
|
||
// Temporarily resurrects an object during deallocation. The refcount is set | ||
// to one. | ||
static inline void | ||
_PyObject_Resurrect(PyObject *op) | ||
{ | ||
assert(Py_REFCNT(op) == 0); | ||
#ifdef Py_REF_DEBUG | ||
_Py_IncRefTotal(_PyThreadState_GET()); | ||
#endif | ||
#ifdef Py_GIL_DISABLED | ||
_Py_atomic_store_uintptr_relaxed(&op->ob_tid, _Py_ThreadId()); | ||
_Py_atomic_store_uint32_relaxed(&op->ob_ref_local, 1); | ||
_Py_atomic_store_ssize_relaxed(&op->ob_ref_shared, 0); | ||
#else | ||
Py_SET_REFCNT(op, 1); | ||
#endif | ||
} | ||
|
||
// Undoes an object resurrection by decrementing the refcount without calling | ||
// _Py_Dealloc(). Returns 0 if the object is dead (the normal case), and | ||
// deallocation should continue. Returns 1 if the object is still alive. | ||
static inline int | ||
_PyObject_ResurrectEnd(PyObject *op) | ||
{ | ||
#ifdef Py_REF_DEBUG | ||
_Py_DecRefTotal(_PyThreadState_GET()); | ||
#endif | ||
#ifndef Py_GIL_DISABLED | ||
Py_SET_REFCNT(op, Py_REFCNT(op) - 1); | ||
return Py_REFCNT(op) != 0; | ||
#else | ||
uint32_t local = _Py_atomic_load_uint32_relaxed(&op->ob_ref_local); | ||
Py_ssize_t shared = _Py_atomic_load_ssize_acquire(&op->ob_ref_shared); | ||
if (_Py_IsOwnedByCurrentThread(op) && local == 1 && shared == 0) { | ||
// Fast-path: object has a single refcount and is owned by this thread | ||
_Py_atomic_store_uint32_relaxed(&op->ob_ref_local, 0); | ||
return 0; | ||
} | ||
// Slow-path: object has a shared refcount or is not owned by this thread | ||
return _PyObject_ResurrectEndSlow(op); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I presume the slow path isn't inline so that compiler will inline the fast path and code will be smaller? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, I think that exact split of what's in the "fast-path" vs. "slow-path" doesn't matter too much here, but it would be unwieldy to try to put everything in the inline function. In particular, |
||
#endif | ||
} | ||
|
||
/* Tries to incref op and returns 1 if successful or 0 otherwise. */ | ||
static inline int | ||
_Py_TryIncref(PyObject *op) | ||
|
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How about naming it something like
_PyObject_ResurrectStart
? That would make its corresponding_PyObject_ResurrectEnd
more obvious or maybe change_PyObject_ResurrectEnd
->_PyObject_UnResurrect
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I changed it to
_PyObject_ResurrectStart