I am trying to send an FFI pointer type to another thread. The struct it points to has been generated by bindgen from opensles-sys
Here is my wrapper struct:
pub struct AndroidAudioIO {
sl_output_buffer_queue: NonNull<SLObjectItf>,
}
unsafe impl Send for AndroidAudioIO{}
The SLObjectItf type is an alias for *const *const SLObjectItf_ whose definition is generated by bindgen. It's a collection of FFI function pointers.
#[repr(C)]
#[derive(Debug, Copy, Clone)]
pub struct SLObjectItf_ {
pub Realize: ::std::option::Option<
unsafe extern "C" fn(self_: SLObjectItf, async: SLboolean) -> SLresult,
>,
// More of the same pattern, only extern "C" function pointers, no data
}
I tried adding unsafe impl Send for SLObjectItf_{} and other variants to no avail.
error[E0277]: `std::ptr::NonNull<*const *const opensles::bindings::SLObjectItf_>` cannot be shared between threads safely
--> src/lib.rs:12:1
|
12 | / lazy_static! {
13 | | static ref engine:Option<mynoise::Engine<Box<audio::AndroidAudioIO>>> = None;
14 | | }
| |_^ `std::ptr::NonNull<*const *const opensles::bindings::SLObjectItf_>` cannot be shared between threads safely
|
= help: within `audio::AndroidAudioIO`, the trait `std::marker::Sync` is not implemented for `std::ptr::NonNull<*const *const opensles::bindings::SLObjectItf_>`
= note: required because it appears within the type `audio::AndroidAudioIO`
The reason why I only care about Send but not Sync is that a single thread (the RT audio thread) interacts with this struct, but it is being created on another thread, hence the need to Send the pointer across to the correct thread.
Sendand/orSyncto any struct. Pointers are not Send for a certain reason. Who is responsible for cleaning or syncing the resource? You could use aMutex/Arcto gainSendandSync. - hellowSDLObjectItf_is generated, I must then advise you to only implementSend/Syncon a high level API layer. You could also include the reasoning behind implementing Send but not Sync into the question. - E_net4Send— but the compiler cares aboutSync:the trait `std::marker::Sync` is not implemented- Shepmaster