Repository navigation
Conversation
…ree-threaded build KeepRef() takes the critical section on its target so that it is safe from any caller, and for a type that keeps a reference to the stored object it then calls PyCData_GetContainer(), which takes the critical section on the root object. The Simple.value setter already holds the lock on self, taken by its Argument Clinic wrapper, and for a top-level object the root is self as well, so `c_char_p().value = b"x"` entered the same critical section three times and `c_int().value = 1` twice (the "nothing to keep" early return sits inside the locked region). Add PyCData_GetContainer_lock_held() for a caller that holds the root's lock, use it from KeepRef_lock_held() when the target is its own root, and call KeepRef_lock_held() from the setter. Measured on the free-threaded build: c_int().value = 1 35 -> 22 ns, c_char_p().value = b"x" 39 -> 22 ns. (cherry picked from commit 5ca398f)
…nts setter The setter runs under a two-object critical section on the pointer and the new target, then calls KeepRef() twice on the pointer and GetKeepedObjects() on the target; both helpers lock their argument again. Call the lock-held variants instead, adding GetKeepedObjects_lock_held() for the target. Measured on the free-threaded build: p.contents = c_int(2) 86 -> 58 ns. (cherry picked from commit 576731e)
PyCData_set() already requires its caller to hold the critical section on the destination object and asserts it, but finished by calling KeepRef(), which takes that lock again. Every store of a Python object into a Structure or Union field, an Array item or a Pointer item went through it. Call KeepRef_lock_held() instead. Measured on the free-threaded build: Structure int field assignment 38 -> 24 ns, c_char_p field assignment 69 -> 54 ns. (cherry picked from commit ead0976)
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Fixing re-entering an already held lock in ctypes. This involves creating lock free version of implementation and also providing wrappers which also lock for callers that do not already hold a critical section.
Micro benchmark estimates.
c_int().value = 1c_char_p().value = b"x"pointer.contents = c_int(2)Structure.i = 1Structure.p = b"x"