Skip to content

Commit

Permalink
fix drop
Browse files Browse the repository at this point in the history
  • Loading branch information
olafklingt committed Jul 20, 2024
1 parent 6da1c8f commit ee943dc
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 20 deletions.
14 changes: 6 additions & 8 deletions src/client/async_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,15 +104,13 @@ impl<N, P> AsyncClient<N, P> {

// Helper function for deactivating. Any function that calls this should
// have ownership of self and no longer use it after this call.
unsafe fn maybe_deactivate(&mut self) -> Result<CallbackContext<N, P>, Error> {
unsafe fn maybe_deactivate(&mut self) -> Result<Box<CallbackContext<N, P>>, Error> {
let _m = CREATE_OR_DESTROY_CLIENT_MUTEX.lock().unwrap();
if self.callback.is_none() {
return Err(Error::ClientIsNoLongerAlive);
}
let client = self.callback.as_ref().unwrap().client.raw();
// Prevent the callback from being deallocated in case deactivation
// fails.
let callback = Box::into_raw(self.callback.take().unwrap());
let cb = self.callback.take().unwrap();
let client = cb.client.raw();

// deactivate
sleep_on_test();
Expand All @@ -123,16 +121,16 @@ impl<N, P> AsyncClient<N, P> {
// clear the callbacks
sleep_on_test();
clear_callbacks(client)?;

// done, take ownership of callback
Ok(*Box::from_raw(callback))
Ok(cb)
}
}

/// Closes the client.
impl<N, P> Drop for AsyncClient<N, P> {
/// Deactivate and close the client.
// Deactivate and close the client.
fn drop(&mut self) {
println!("drop async client is called");
let _ = unsafe { self.maybe_deactivate() };
}
}
Expand Down
27 changes: 15 additions & 12 deletions src/client/callbacks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ pub trait NotificationHandler: Send {
pub trait ProcessHandler: Send {
/// Indicates whether or not this process handler represents a
/// slow-sync client
const SLOW_SYNC:bool = false;
const SLOW_SYNC: bool = false;

/// Called whenever there is work to be done.
///
Expand Down Expand Up @@ -108,11 +108,12 @@ pub trait ProcessHandler: Send {
/// It should return `false` until the handler is ready process audio.
///
/// Ignored unless Self::SLOW_SYNC == true.
fn sync(&mut self,
_: &Client,
_state: crate::TransportState,
_pos: &crate::TransportPosition
)->bool {
fn sync(
&mut self,
_: &Client,
_state: crate::TransportState,
_pos: &crate::TransportPosition,
) -> bool {
true
}
}
Expand Down Expand Up @@ -156,7 +157,7 @@ where
unsafe extern "C" fn sync<N, P>(
state: jack_sys::jack_transport_state_t,
pos: *mut jack_sys::jack_position_t,
data: *mut libc::c_void
data: *mut libc::c_void,
) -> libc::c_int
where
N: 'static + Send + Sync + NotificationHandler,
Expand All @@ -166,10 +167,10 @@ where
match ctx.process.sync(
&ctx.client,
crate::Transport::state_from_ffi(state),
&*(pos as *mut crate::TransportPosition)
&*(pos as *mut crate::TransportPosition),
) {
true => 1,
false => 0
false => 0,
}
}

Expand Down Expand Up @@ -295,9 +296,11 @@ where
/// # TODO
///
/// * Implement correctly. Freezes on my system.
pub unsafe fn clear_callbacks(_client: *mut j::jack_client_t) -> Result<(), Error> {
// j::jack_set_thread_init_callback(client, None, ptr::null_mut());
// j::jack_set_process_callback(client, None, ptr::null_mut());
//maybe this makes sense now? it doesn't disturb my program
pub unsafe fn clear_callbacks(client: *mut j::jack_client_t) -> Result<(), Error> {
j::jack_set_thread_init_callback(client, None, std::ptr::null_mut());
j::jack_set_process_callback(client, None, std::ptr::null_mut());
Ok(())
}

Expand Down
1 change: 1 addition & 0 deletions src/client/client_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -659,6 +659,7 @@ impl Client {
impl Drop for Client {
fn drop(&mut self) {
let _m = CREATE_OR_DESTROY_CLIENT_MUTEX.lock().unwrap();
println!("drop client is called");

debug_assert!(!self.raw().is_null()); // Rep invariant
// Close the client
Expand Down

0 comments on commit ee943dc

Please sign in to comment.