jack device mutex -> rwlock

This commit is contained in:
🪞👃🪞 2024-07-05 19:53:16 +03:00
parent 52ea78a466
commit 33de8bbf1d
4 changed files with 17 additions and 13 deletions

View file

@ -5,7 +5,7 @@ pub struct JackDevice {
/// The active JACK client of this device. /// The active JACK client of this device.
pub client: DynamicAsyncClient, pub client: DynamicAsyncClient,
/// The device state, encapsulated for sharing between threads. /// The device state, encapsulated for sharing between threads.
pub state: Arc<Mutex<Box<dyn Device>>>, pub state: Arc<RwLock<Box<dyn Device>>>,
/// Unowned copies of the device's JACK ports, for connecting to the device. /// Unowned copies of the device's JACK ports, for connecting to the device.
/// The "real" readable/writable `Port`s are owned by the `state`. /// The "real" readable/writable `Port`s are owned by the `state`.
pub ports: UnownedJackPorts, pub ports: UnownedJackPorts,
@ -22,8 +22,12 @@ ports!(JackDevice {
}); });
impl JackDevice { impl JackDevice {
/// Returns a locked mutex of the state's contents. /// Returns a locked mutex of the state's contents.
pub fn state (&self) -> MutexGuard<Box<dyn Device>> { pub fn state (&self) -> LockResult<RwLockReadGuard<Box<dyn Device>>> {
self.state.lock().unwrap() self.state.read()
}
/// Returns a locked mutex of the state's contents.
pub fn state_mut (&self) -> LockResult<RwLockWriteGuard<Box<dyn Device>>> {
self.state.write()
} }
pub fn connect_midi_in (&self, index: usize, port: &Port<Unowned>) -> Usually<()> { pub fn connect_midi_in (&self, index: usize, port: &Port<Unowned>) -> Usually<()> {
Ok(self.client.as_client().connect_ports(port, self.midi_ins()?[index])?) Ok(self.client.as_client().connect_ports(port, self.midi_ins()?[index])?)

View file

@ -41,7 +41,7 @@ impl Jack {
.map(|p|Ok(p.name()?)).collect::<Usually<Vec<_>>>()?; .map(|p|Ok(p.name()?)).collect::<Usually<Vec<_>>>()?;
let audio_ins = owned_ports.audio_ins.values() let audio_ins = owned_ports.audio_ins.values()
.map(|p|Ok(p.name()?)).collect::<Usually<Vec<_>>>()?; .map(|p|Ok(p.name()?)).collect::<Usually<Vec<_>>>()?;
let state = Arc::new(Mutex::new(state(owned_ports) as Box<dyn Device>)); let state = Arc::new(RwLock::new(state(owned_ports) as Box<dyn Device>));
let client = self.client.activate_async( let client = self.client.activate_async(
Notifications(Box::new({ Notifications(Box::new({
let _state = state.clone(); let _state = state.clone();
@ -53,7 +53,7 @@ impl Jack {
ClosureProcessHandler::new(Box::new({ ClosureProcessHandler::new(Box::new({
let state = state.clone(); let state = state.clone();
move|c: &Client, s: &ProcessScope|{ move|c: &Client, s: &ProcessScope|{
state.lock().unwrap().process(c, s) state.write().unwrap().process(c, s)
} }
}) as BoxedProcessHandler) }) as BoxedProcessHandler)
)?; )?;

View file

@ -53,17 +53,17 @@ impl Track {
device: 0, device: 0,
}) })
} }
pub fn device (&self) -> Option<MutexGuard<Box<dyn Device>>> { pub fn device (&self) -> Option<RwLockReadGuard<Box<dyn Device>>> {
self.get_device(self.device) self.get_device(self.device)
} }
pub fn first_device (&self) -> Option<MutexGuard<Box<dyn Device>>> { pub fn first_device (&self) -> Option<RwLockReadGuard<Box<dyn Device>>> {
self.get_device(0) self.get_device(0)
} }
pub fn last_device (&self) -> Option<MutexGuard<Box<dyn Device>>> { pub fn last_device (&self) -> Option<RwLockReadGuard<Box<dyn Device>>> {
self.get_device(self.devices.len().saturating_sub(1)) self.get_device(self.devices.len().saturating_sub(1))
} }
pub fn get_device (&self, i: usize) -> Option<MutexGuard<Box<dyn Device>>> { pub fn get_device (&self, i: usize) -> Option<RwLockReadGuard<Box<dyn Device>>> {
self.devices.get(i).map(|d|d.state.lock().unwrap()) self.devices.get(i).map(|d|d.state.read().unwrap())
} }
pub fn add_device (&mut self, device: JackDevice) -> Usually<&mut JackDevice> { pub fn add_device (&mut self, device: JackDevice) -> Usually<&mut JackDevice> {
self.devices.push(device); self.devices.push(device);

View file

@ -62,7 +62,7 @@ impl<'a> ChainView<'a> {
//y2 = y2 + 1; //y2 = y2 + 1;
//} //}
let width = width.saturating_sub(x).saturating_sub(x2); let width = width.saturating_sub(x).saturating_sub(x2);
let frame = device.state.lock().unwrap() let frame = device.state.read().unwrap()
.render(buf, Rect { x: x + x2, y, width, height })?; .render(buf, Rect { x: x + x2, y, width, height })?;
let style = if i == track.device { let style = if i == track.device {
Some(if self.focused { Some(if self.focused {
@ -103,12 +103,12 @@ impl<'a> ChainView<'a> {
let mut w = 0u16; let mut w = 0u16;
let mut frames = vec![]; let mut frames = vec![];
let track = self.track.as_ref().unwrap(); let track = self.track.as_ref().unwrap();
for (i, device) in track.devices.iter().enumerate() { for (_i, device) in track.devices.iter().enumerate() {
//let midi_ins = device.midi_ins()?; //let midi_ins = device.midi_ins()?;
//let midi_outs = device.midi_outs()?; //let midi_outs = device.midi_outs()?;
//let audio_ins = device.audio_ins()?; //let audio_ins = device.audio_ins()?;
//let audio_outs = device.audio_outs()?; //let audio_outs = device.audio_outs()?;
let device = device.state.lock().unwrap(); let device = device.state.read().unwrap();
//let style_midi = Style::default().black().bold().on_green(); //let style_midi = Style::default().black().bold().on_green();
//let style_audio = Style::default().black().bold().on_red(); //let style_audio = Style::default().black().bold().on_red();
//y = y + midi_ins.len() as u16; //y = y + midi_ins.len() as u16;