Skip to content

Commit

Permalink
fix drop (#198)
Browse files Browse the repository at this point in the history
* fix client drop
* fix warnings
  • Loading branch information
olafklingt authored Sep 8, 2024
1 parent 6da1c8f commit 4a84441
Show file tree
Hide file tree
Showing 14 changed files with 93 additions and 121 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ license = "MIT"
name = "jack"
readme = "README.md"
repository = "https://github.com/RustAudio/rust-jack"
version = "0.11.4"
version = "0.12.0"

[dependencies]
bitflags = "1"
Expand Down
16 changes: 4 additions & 12 deletions examples/playback_capture.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,10 @@ fn main() {

// Register ports. They will be used in a callback that will be
// called when new data is available.
let in_a = client
.register_port("rust_in_l", jack::AudioIn::default())
.unwrap();
let in_b = client
.register_port("rust_in_r", jack::AudioIn::default())
.unwrap();
let mut out_a = client
.register_port("rust_out_l", jack::AudioOut::default())
.unwrap();
let mut out_b = client
.register_port("rust_out_r", jack::AudioOut::default())
.unwrap();
let in_a = client.register_port("rust_in_l", jack::AudioIn).unwrap();
let in_b = client.register_port("rust_in_r", jack::AudioIn).unwrap();
let mut out_a = client.register_port("rust_out_l", jack::AudioOut).unwrap();
let mut out_b = client.register_port("rust_out_r", jack::AudioOut).unwrap();
let process_callback = move |_: &jack::Client, ps: &jack::ProcessScope| -> jack::Control {
let out_a_p = out_a.as_mut_slice(ps);
let out_b_p = out_b.as_mut_slice(ps);
Expand Down
4 changes: 2 additions & 2 deletions examples/show_midi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,10 @@ fn main() {

// process logic
let mut maker = client
.register_port("rust_midi_maker", jack::MidiOut::default())
.register_port("rust_midi_maker", jack::MidiOut)
.unwrap();
let shower = client
.register_port("rust_midi_shower", jack::MidiIn::default())
.register_port("rust_midi_shower", jack::MidiIn)
.unwrap();

let cback = move |_: &jack::Client, ps: &jack::ProcessScope| -> jack::Control {
Expand Down
4 changes: 1 addition & 3 deletions examples/sine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,7 @@ fn main() {
jack::Client::new("rust_jack_sine", jack::ClientOptions::NO_START_SERVER).unwrap();

// 2. register port
let mut out_port = client
.register_port("sine_out", jack::AudioOut::default())
.unwrap();
let mut out_port = client.register_port("sine_out", jack::AudioOut).unwrap();

// 3. define process callback handler
let mut frequency = 220.0;
Expand Down
1 change: 1 addition & 0 deletions jack-sys/src/types.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#![allow(non_camel_case_types)]
#[cfg(not(target_os = "windows"))]
pub type jack_native_thread_t = ::libc::pthread_t;
pub type jack_uuid_t = u64;
Expand Down
13 changes: 5 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,15 +121,14 @@ 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) {
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
2 changes: 1 addition & 1 deletion src/client/client_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ use crate::{
pub type InternalClientID = j::jack_intclient_t;

#[allow(dead_code)]
pub struct Client(
*mut j::jack_client_t,
Arc<()>,
Expand Down Expand Up @@ -659,7 +660,6 @@ impl Client {
impl Drop for Client {
fn drop(&mut self) {
let _m = CREATE_OR_DESTROY_CLIENT_MUTEX.lock().unwrap();

debug_assert!(!self.raw().is_null()); // Rep invariant
// Close the client
sleep_on_test();
Expand Down
20 changes: 4 additions & 16 deletions src/client/test_callback.rs
Original file line number Diff line number Diff line change
Expand Up @@ -201,14 +201,8 @@ fn client_cback_reports_xruns() {
#[test]
fn client_cback_calls_port_registered() {
let ac = active_test_client("client_cback_cpr");
let _pa = ac
.as_client()
.register_port("pa", AudioIn::default())
.unwrap();
let _pb = ac
.as_client()
.register_port("pb", AudioIn::default())
.unwrap();
let _pa = ac.as_client().register_port("pa", AudioIn).unwrap();
let _pb = ac.as_client().register_port("pb", AudioIn).unwrap();
let counter = ac.deactivate().unwrap().1;
assert_eq!(
counter.port_register_history.len(),
Expand All @@ -224,14 +218,8 @@ fn client_cback_calls_port_registered() {
#[test]
fn client_cback_calls_port_unregistered() {
let ac = active_test_client("client_cback_cpr");
let pa = ac
.as_client()
.register_port("pa", AudioIn::default())
.unwrap();
let pb = ac
.as_client()
.register_port("pb", AudioIn::default())
.unwrap();
let pa = ac.as_client().register_port("pa", AudioIn).unwrap();
let pb = ac.as_client().register_port("pb", AudioIn).unwrap();
ac.as_client().unregister_port(pa).unwrap();
ac.as_client().unregister_port(pb).unwrap();
let counter = ac.deactivate().unwrap().1;
Expand Down
8 changes: 4 additions & 4 deletions src/port/audio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,10 +107,10 @@ mod test {
#[test]
fn port_audio_can_read_write() {
let c = open_test_client("port_audio_crw");
let in_a = c.register_port("ia", AudioIn::default()).unwrap();
let in_b = c.register_port("ib", AudioIn::default()).unwrap();
let mut out_a = c.register_port("oa", AudioOut::default()).unwrap();
let mut out_b = c.register_port("ob", AudioOut::default()).unwrap();
let in_a = c.register_port("ia", AudioIn).unwrap();
let in_b = c.register_port("ib", AudioIn).unwrap();
let mut out_a = c.register_port("oa", AudioOut).unwrap();
let mut out_b = c.register_port("ob", AudioOut).unwrap();
let (signal_succeed, did_succeed) = bounded(1_000);
let process_callback = move |_: &Client, ps: &ProcessScope| -> Control {
let exp_a = 0.312_443;
Expand Down
20 changes: 10 additions & 10 deletions src/port/midi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -275,8 +275,8 @@ mod test {
stream,
collected: Vec::new(),
collector,
midi_in: client.register_port("in", MidiIn::default()).unwrap(),
midi_out: client.register_port("out", MidiOut::default()).unwrap(),
midi_in: client.register_port("in", MidiIn).unwrap(),
midi_out: client.register_port("out", MidiOut).unwrap(),
}
}

Expand Down Expand Up @@ -307,10 +307,10 @@ mod test {
fn port_midi_can_read_write() {
// open clients and ports
let c = open_test_client("port_midi_crw");
let in_a = c.register_port("ia", MidiIn::default()).unwrap();
let in_b = c.register_port("ib", MidiIn::default()).unwrap();
let mut out_a = c.register_port("oa", MidiOut::default()).unwrap();
let mut out_b = c.register_port("ob", MidiOut::default()).unwrap();
let in_a = c.register_port("ia", MidiIn).unwrap();
let in_b = c.register_port("ib", MidiIn).unwrap();
let mut out_a = c.register_port("oa", MidiOut).unwrap();
let mut out_b = c.register_port("ob", MidiOut).unwrap();

// set callback routine
let (signal_succeed, did_succeed) = bounded(1_000);
Expand Down Expand Up @@ -366,7 +366,7 @@ mod test {
fn port_midi_can_get_max_event_size() {
// open clients and ports
let c = open_test_client("port_midi_cglc");
let mut out_p = c.register_port("op", MidiOut::default()).unwrap();
let mut out_p = c.register_port("op", MidiOut).unwrap();

// set callback routine
let process_callback = move |_: &Client, ps: &ProcessScope| -> Control {
Expand All @@ -393,7 +393,7 @@ mod test {
fn port_midi_cant_exceed_max_event_size() {
// open clients and ports
let c = open_test_client("port_midi_cglc");
let mut out_p = c.register_port("op", MidiOut::default()).unwrap();
let mut out_p = c.register_port("op", MidiOut).unwrap();

// set callback routine
let process_callback = move |_: &Client, ps: &ProcessScope| -> Control {
Expand Down Expand Up @@ -437,8 +437,8 @@ mod test {
fn port_midi_iter() {
// open clients and ports
let c = open_test_client("port_midi_iter");
let in_p = c.register_port("ip", MidiIn::default()).unwrap();
let mut out_p = c.register_port("op", MidiOut::default()).unwrap();
let in_p = c.register_port("ip", MidiIn).unwrap();
let mut out_p = c.register_port("op", MidiOut).unwrap();

// set callback routine
let process_callback = move |_: &Client, ps: &ProcessScope| -> Control {
Expand Down
Loading

0 comments on commit 4a84441

Please sign in to comment.