Skip to content

CFRunLoop looks thread safe, but core-foundation-rs doesn't mark it as such #550

@micolous

Description

@micolous

The core-foundation-rs CFRunLoop type does not appear to be marked as Send or Sync, and I suspect they should be.

Apple's Core Foundation docs appear to suggest those types are generally thread-safe (emphasis mine):

Core Foundation is sufficiently thread-safe that, if you program with care, you should not run into any problems related to competing threads. It is thread-safe in the common cases, such as when you query, retain, release, and pass around immutable objects. Even central shared objects that might be queried from more than one thread are reliably thread-safe.

Like Cocoa, Core Foundation is not thread-safe when it comes to mutations to objects or their contents. (...)

Looking at another usage, mozilla/authenticator-rs passes a CFRunLoop between threads in a SendableRunLoop type: it schedules a CFRunLoopObserver in a new thread, which then sends back the current CFRunLooop over a mpsc::channel and picks it up from the current thread.

The C __CFRunLoop type also looks like it has a mutex around mutation operations (example), so it looks a lot like this should be safe to pass between threads... and you'd need to do this anyway if you want to schedule work on another thread.

Very few types in this crate (currently four) are marked as Send or Sync; and there's probably other types that could be marked that way too. Apple's documentation seems to suggest that Core Foundation types could be non-thread-safe (just you should assume safe unless otherwise stated), so I don't think that implementing Send + Sync for T: TCFType is wise.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions