Skip to content

Make weakref thread-safe without the GIL #111926

Description

@colesbury

Feature or enhancement

The current weakref implementation relies on the GIL for thread-safety.

The nogil-3.12 fork substantially modifies the weakref implementation. I think we can implement a simpler change in CPython 3.13 (main) now that all PyObject's have their own mutex (in --disable-gil builds).

Basic idea

Protect access to the weakrefs linked list using the mutex in the weakly referenced object. Use the critical section API.

Prior implementation: colesbury/nogil-3.12@0dddcb6f9d

Linked PRs

Activity

  1. corona10 commented on Nov 10, 2023

    @corona10
    Member

    Maybe we should check the performance impact the default build first.
    I will try to investigate it.

  2. colesbury commented on Nov 10, 2023

    @colesbury
    ContributorAuthor

    The critical section APIs are no-ops on the default build:

    #else /* !Py_NOGIL */
    // The critical section APIs are no-ops with the GIL.
    # define Py_BEGIN_CRITICAL_SECTION(op)
    # define Py_END_CRITICAL_SECTION()
    # define Py_BEGIN_CRITICAL_SECTION2(a, b)
    # define Py_END_CRITICAL_SECTION2()
    #endif /* !Py_NOGIL */

  3. corona10 commented on Nov 10, 2023

    @corona10
    Member

    I know that there is no op for the default build but the commit you suggested modify some internal structure, I just want to know ovehead from the structure changed, even I actually do not expect real overhead from some refactoring. (I didn't look at deeply now please let me know if I miss something)

  4. colesbury commented on Nov 10, 2023

    @colesbury
    ContributorAuthor

    @corona10 the commit I linked to (in nogil-3.12) is not how I intend to make weakref thread-safe in CPython 3.13. I wouldn't spend too much time investigating it because it's not going to make it's way into 3.13. I linked to it because it's sometimes helpful for me to look back at the nogil-3.12 implementation.

  5. corona10 commented on Nov 16, 2023

    @corona10
    Member

    By the way, I am working on it :)

  6. added 5 commits that reference this issue on Nov 17, 2023
  7. added a commit that references this issue on Jan 1, 2024
  8. 34 remaining items

  9. added 3 commits that reference this issue on Apr 2, 2024
  10. added 4 commits that reference this issue on Apr 17, 2024
  11. added 3 commits that reference this issue on Sep 2, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions