• Alice Ryhl's avatar
    rust: list: add tracking for ListArc · a4802631
    Alice Ryhl authored
    Add the ability to track whether a ListArc exists for a given value,
    allowing for the creation of ListArcs without going through UniqueArc.
    
    The `impl_list_arc_safe!` macro is extended with a `tracked_by` strategy
    that defers the tracking of ListArcs to a field of the struct.
    Additionally, the AtomicListArcTracker type is introduced, which can
    track whether a ListArc exists using an atomic. By deferring the
    tracking to a field of type AtomicListArcTracker, structs gain the
    ability to create ListArcs without going through a UniqueArc.
    
    Rust Binder uses this for some objects where we want to be able to
    insert them into a linked list at any time. Using the
    AtomicListArcTracker, we are able to check whether an item is already in
    the list, and if not, we can create a `ListArc` and push it.
    
    The macro has the ability to defer the tracking of ListArcs to a field,
    using whatever strategy that field has. Since we don't add any
    strategies other than AtomicListArcTracker, another similar option would
    be to hard-code that the field should be an AtomicListArcTracker.
    However, Rust Binder has a case where the AtomicListArcTracker is not
    stored directly in the struct, but in a sub-struct. Furthermore, the
    outer struct is generic:
    
    struct Wrapper<T: ?Sized> {
        links: ListLinks,
        inner: T,
    }
    
    Here, the Wrapper struct implements ListArcSafe with `tracked_by inner`,
    and then the various types used with `inner` also uses the macro to
    implement ListArcSafe. Some of them use the untracked strategy, and some
    of them use tracked_by with an AtomicListArcTracker. This way, Wrapper
    just inherits whichever choice `inner` has made.
    Reviewed-by: default avatarBenno Lossin <benno.lossin@proton.me>
    Signed-off-by: default avatarAlice Ryhl <aliceryhl@google.com>
    Link: https://lore.kernel.org/r/20240814-linked-list-v5-3-f5f5e8075da0@google.comSigned-off-by: default avatarMiguel Ojeda <ojeda@kernel.org>
    a4802631
arc.rs 19.7 KB