Skip to content

Conversation

@roderickvd
Copy link
Member

Fixex standard clippy warnings, as well as a more impactful change in CoreAudio (macOS): to resolve the clippy::arc-with-non-send-sync warning, it replaces Arc<Mutex<StreamInner>> with Rc<RefCell<StreamInner>>.

CoreAudio changes (review requested)

The main change replaces Arc<Mutex<StreamInner>> with Rc<RefCell<StreamInner>> in the macOS CoreAudio implementation. Rationale:

  • StreamInner contains AudioUnit and AudioObjectPropertyListener which are not Send/Sync
  • The Arc<Mutex<>> provided no thread-safety benefits since the inner types aren't thread-safe
  • Stream usage patterns in CPAL are single-threaded
  • Rc<RefCell<>> is more appropriate for single-threaded shared ownership

This changes fundamental synchronization primitives in the CoreAudio backend. While the change is theoretically sound and all tests pass, extra scrutiny would be appreciated to ensure that we are not missing some edge cases.

- Replace `format!` with Rust 1.58+ inline format strings
- Use `Rc<RefCell<...>>` instead of `Arc<Mutex<...>>` for CoreAudio streams
- Update CoreAudio disconnect listener and stream methods to use `Rc<RefCell<...>>`
- Fix CoreAudio error code comparisons and comments
@roderickvd
Copy link
Member Author

@wgibbs-rs @tomoyanonymous as recent CoreAudio contributors, would you help to review?

This comment was marked as resolved.

@roderickvd roderickvd requested a review from Copilot August 5, 2025 10:47

This comment was marked as resolved.

@roderickvd roderickvd requested a review from Copilot August 5, 2025 11:17

This comment was marked as resolved.

Copy link
Contributor

@tomoyanonymous tomoyanonymous left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is an additional suggetion but about the refactoring related to this, in E and D in build_input/output_stream_raw and add_disconnect_listener, Send is not necessary for them.

@roderickvd
Copy link
Member Author

This is an additional suggetion but about the refactoring related to this, in E and D in build_input/output_stream_raw and add_disconnect_listener, Send is not necessary for them.

This is a very interesting point. While correct for CoreAudio, I believe that other backends (WASAPI, ALSA) do spawn threads where callbacks run, so the trait's Send bounds serve a real purpose there.

@tomoyanonymous
Copy link
Contributor

Ah, I missed that the constraint came from DeviceTrait. Then it can be left them as they are. The other parts looks good to me.

@wgibbs-rs
Copy link
Member

@roderickvd This looks great to me. I reviewed what you created for CoreAudio on my own device, and it worked first try. You fixed the mutability issues in StreamTrait, which is perfect, as well as everything else.

Previously, I tried working with multi-threading and it failed, and I was confused with the use of an Arc<Mutex<>>, but now that there's explicitly no-multithreading, I think that should be included in the documentation. Let me look and see if there's a place to put that in the docs.rs

@wgibbs-rs
Copy link
Member

Never mind, I was mistaken on documentation. This is all internal, so documentation isn't necessary.

Apart from that, this looks great and runs on my MacBook Air M1.

Remove changelog entry for CoreAudio Arc<Mutex> to Rc<RefCell> refactor
as it's an internal implementation detail that doesn't affect public API
or user-observable behavior.
@roderickvd
Copy link
Member Author

Thanks for the quick reviews. I removed the changelog entry, as it the change isn't user-facing. Merging now.

@roderickvd roderickvd merged commit 0d51fad into RustAudio:master Aug 5, 2025
15 checks passed
@roderickvd roderickvd deleted the refactor/clippy-fixes branch August 5, 2025 20:52
roderickvd added a commit to roderickvd/cpal that referenced this pull request Aug 30, 2025
… CoreAudio (RustAudio#998)

- Replace `format!` with Rust 1.58+ inline format strings
- Use `Rc<RefCell<...>>` instead of `Arc<Mutex<...>>` for CoreAudio streams
- Update CoreAudio disconnect listener and stream methods to use `Rc<RefCell<...>>`
- Fix CoreAudio error code comparisons and comments
lautarodragan added a commit to lautarodragan/jolteon that referenced this pull request Sep 29, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants