Skip to content
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

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

Closed
micolous opened this issue May 17, 2023 · 0 comments · Fixed by #610
Closed

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

micolous opened this issue May 17, 2023 · 0 comments · Fixed by #610

Comments

@micolous
Copy link
Contributor

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.

kevinmehall added a commit to kevinmehall/core-foundation-rs that referenced this issue Dec 4, 2023
Following servo#610,
the same reasoning from
servo#550
applies to the remaining CFRunLoop types.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant