-
-
Notifications
You must be signed in to change notification settings - Fork 32.4k
gh-135871: Fix needless spinning in _PyMutex_LockTimed (timeout==0) #135872
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 1 commit
7296a7d
4b5123c
8a4e9ad
3fb8f37
8cbb992
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
* Move the timeout == 0 guard outside the else so a non-blocking call returns immediately after a failed CAS instead of entering the spin loop. * Reload v on every spin iteration, allowing timed/blocking callers to notice an unlock promptly. No-GIL builds now honor the semantics of non-blocking attempts and avoid wasted CPU; GIL builds are unaffected (MAX_SPIN_COUNT == 0).
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
gh-135871: _PyMutex_LockTimed() no longer spins or fails for timeout==0 in no-GIL builds; spin loop now reloads lock state. | ||
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -58,7 +58,7 @@ _PyMutex_LockTimed(PyMutex *m, PyTime_t timeout, _PyLockFlags flags) | |
return PY_LOCK_ACQUIRED; | ||
} | ||
} | ||
else if (timeout == 0) { | ||
if (timeout == 0) { | ||
return PY_LOCK_FAILURE; | ||
} | ||
|
||
|
@@ -89,6 +89,7 @@ _PyMutex_LockTimed(PyMutex *m, PyTime_t timeout, _PyLockFlags flags) | |
// Spin for a bit. | ||
_Py_yield(); | ||
spin_count++; | ||
v = _Py_atomic_load_uint8_relaxed(&m->_bits); | ||
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. This looks like it fixes a logic bug but it also hurts performance See 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. thanks for the review! I'm convinced, this line reduces throughput on my machine by something like 40% for 8 threads. I think changing |
||
continue; | ||
} | ||
|
||
|
Uh oh!
There was an error while loading. Please reload this page.