Skip to content

Commit

Permalink
Remove parking_lot dependency (#846)
Browse files Browse the repository at this point in the history
The standard library is using a similar implementation to parking_lot now. Other than APIs that are unstable, there shouldn't be that much of a reason to rely on parking_lot anymore. Parts of the cpal codebase already are using std locks. This PR removes the remaining parking_lot uses and replaces them with the standard library's.
  • Loading branch information
james7132 authored Feb 25, 2024
1 parent 4eec3e3 commit bdbbf75
Show file tree
Hide file tree
Showing 8 changed files with 19 additions and 29 deletions.
3 changes: 0 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -41,18 +41,15 @@ windows = { version = "0.52.0", features = [
]}
asio-sys = { version = "0.2", path = "asio-sys", optional = true }
num-traits = { version = "0.2.6", optional = true }
parking_lot = "0.12"

[target.'cfg(any(target_os = "linux", target_os = "dragonfly", target_os = "freebsd", target_os = "netbsd"))'.dependencies]
alsa = "0.8"
libc = "0.2"
parking_lot = "0.12"
jack = { version = "0.11", optional = true }

[target.'cfg(any(target_os = "macos", target_os = "ios"))'.dependencies]
core-foundation-sys = "0.8.2" # For linking to CoreFoundation.framework and handling device name `CFString`s.
mach2 = "0.4" # For access to mach_timebase type.
parking_lot = "0.12"

[target.'cfg(target_os = "macos")'.dependencies]
coreaudio-rs = { version = "0.11", default-features = false, features = ["audio_unit", "core_audio"] }
Expand Down
3 changes: 1 addition & 2 deletions src/host/alsa/enumerate.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
use super::alsa;
use super::parking_lot::Mutex;
use super::{Device, DeviceHandles};
use crate::{BackendSpecificError, DevicesError};
use std::sync::Arc;
use std::sync::{Arc, Mutex};

/// ALSA's implementation for `Devices`.
pub struct Devices {
Expand Down
7 changes: 3 additions & 4 deletions src/host/alsa/mod.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
extern crate alsa;
extern crate libc;
extern crate parking_lot;

use self::alsa::poll::Descriptors;
use self::parking_lot::Mutex;
use crate::traits::{DeviceTrait, HostTrait, StreamTrait};
use crate::{
BackendSpecificError, BufferSize, BuildStreamError, ChannelCount, Data,
Expand All @@ -14,7 +12,7 @@ use crate::{
};
use std::cmp;
use std::convert::TryInto;
use std::sync::Arc;
use std::sync::{Arc, Mutex};
use std::thread::{self, JoinHandle};
use std::time::Duration;
use std::vec::IntoIter as VecIntoIter;
Expand Down Expand Up @@ -252,6 +250,7 @@ impl Device {
let handle_result = self
.handles
.lock()
.unwrap()
.take(&self.name, stream_type)
.map_err(|e| (e, e.errno()));

Expand Down Expand Up @@ -311,7 +310,7 @@ impl Device {
&self,
stream_t: alsa::Direction,
) -> Result<VecIntoIter<SupportedStreamConfigRange>, SupportedStreamConfigsError> {
let mut guard = self.handles.lock();
let mut guard = self.handles.lock().unwrap();
let handle_result = guard
.get_mut(&self.name, stream_t)
.map_err(|e| (e, e.errno()));
Expand Down
3 changes: 1 addition & 2 deletions src/host/asio/device.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
pub type SupportedInputConfigs = std::vec::IntoIter<SupportedStreamConfigRange>;
pub type SupportedOutputConfigs = std::vec::IntoIter<SupportedStreamConfigRange>;

use super::parking_lot::Mutex;
use super::sys;
use crate::BackendSpecificError;
use crate::DefaultStreamConfigError;
Expand All @@ -15,7 +14,7 @@ use crate::SupportedStreamConfigRange;
use crate::SupportedStreamConfigsError;
use std::hash::{Hash, Hasher};
use std::sync::atomic::AtomicI32;
use std::sync::Arc;
use std::sync::{Arc, Mutex};

/// A ASIO Device
#[derive(Clone)]
Expand Down
1 change: 0 additions & 1 deletion src/host/asio/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
extern crate asio_sys as sys;
extern crate parking_lot;

use crate::traits::{DeviceTrait, HostTrait, StreamTrait};
use crate::{
Expand Down
11 changes: 5 additions & 6 deletions src/host/asio/stream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,13 @@ extern crate asio_sys as sys;
extern crate num_traits;

use self::num_traits::PrimInt;
use super::parking_lot::Mutex;
use super::Device;
use crate::{
BackendSpecificError, BufferSize, BuildStreamError, Data, InputCallbackInfo,
OutputCallbackInfo, PauseStreamError, PlayStreamError, SampleFormat, StreamConfig, StreamError,
};
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::Arc;
use std::sync::{Arc, Mutex};
use std::time::Duration;

pub struct Stream {
Expand Down Expand Up @@ -77,7 +76,7 @@ impl Device {
}

// There is 0% chance of lock contention the host only locks when recreating streams.
let stream_lock = asio_streams.lock();
let stream_lock = asio_streams.lock().unwrap();
let asio_stream = match stream_lock.input {
Some(ref asio_stream) => asio_stream,
None => return,
Expand Down Expand Up @@ -281,7 +280,7 @@ impl Device {
}

// There is 0% chance of lock contention the host only locks when recreating streams.
let mut stream_lock = asio_streams.lock();
let mut stream_lock = asio_streams.lock().unwrap();
let asio_stream = match stream_lock.output {
Some(ref mut asio_stream) => asio_stream,
None => return,
Expand Down Expand Up @@ -518,7 +517,7 @@ impl Device {
Err(_) => Err(BuildStreamError::StreamConfigNotSupported),
}?;
let num_channels = config.channels as usize;
let mut streams = self.asio_streams.lock();
let mut streams = self.asio_streams.lock().unwrap();

let buffer_size = match config.buffer_size {
BufferSize::Fixed(v) => Some(v as i32),
Expand Down Expand Up @@ -565,7 +564,7 @@ impl Device {
Err(_) => Err(BuildStreamError::StreamConfigNotSupported),
}?;
let num_channels = config.channels as usize;
let mut streams = self.asio_streams.lock();
let mut streams = self.asio_streams.lock().unwrap();

let buffer_size = match config.buffer_size {
BufferSize::Fixed(v) => Some(v as i32),
Expand Down
19 changes: 9 additions & 10 deletions src/host/coreaudio/macos/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,14 @@ use crate::{
SupportedBufferSize, SupportedStreamConfig, SupportedStreamConfigRange,
SupportedStreamConfigsError,
};
use parking_lot::Mutex;
use std::ffi::CStr;
use std::fmt;
use std::mem;
use std::os::raw::c_char;
use std::ptr::null;
use std::slice;
use std::sync::mpsc::{channel, RecvTimeoutError};
use std::sync::Arc;
use std::sync::{Arc, Mutex};
use std::time::{Duration, Instant};

pub use self::enumerate::{
Expand Down Expand Up @@ -454,7 +453,7 @@ where
E: FnMut(StreamError) + Send + 'static,
{
let stream_copy = stream.clone();
let mut stream_inner = stream.inner.lock();
let mut stream_inner = stream.inner.lock().unwrap();
stream_inner._disconnect_listener = Some(AudioObjectPropertyListener::new(
stream_inner.device_id,
AudioObjectPropertyAddress {
Expand All @@ -464,7 +463,7 @@ where
},
move || {
let _ = stream_copy.pause();
(error_callback.lock())(StreamError::DeviceNotAvailable);
(error_callback.lock().unwrap())(StreamError::DeviceNotAvailable);
},
)?);
Ok(())
Expand Down Expand Up @@ -587,7 +586,7 @@ impl Device {
// TODO: Need a better way to get delay, for now we assume a double-buffer offset.
let callback = match host_time_to_stream_instant(args.time_stamp.mHostTime) {
Err(err) => {
(error_callback.lock())(err.into());
(error_callback.lock().unwrap())(err.into());
return Err(());
}
Ok(cb) => cb,
Expand Down Expand Up @@ -617,7 +616,7 @@ impl Device {
add_disconnect_listener(&stream, error_callback_disconnect)?;
}

stream.inner.lock().audio_unit.start()?;
stream.inner.lock().unwrap().audio_unit.start()?;

Ok(stream)
}
Expand Down Expand Up @@ -691,7 +690,7 @@ impl Device {

let callback = match host_time_to_stream_instant(args.time_stamp.mHostTime) {
Err(err) => {
(error_callback.lock())(err.into());
(error_callback.lock().unwrap())(err.into());
return Err(());
}
Ok(cb) => cb,
Expand Down Expand Up @@ -722,7 +721,7 @@ impl Device {
add_disconnect_listener(&stream, error_callback_disconnect)?;
}

stream.inner.lock().audio_unit.start()?;
stream.inner.lock().unwrap().audio_unit.start()?;

Ok(stream)
}
Expand Down Expand Up @@ -896,7 +895,7 @@ impl Stream {

impl StreamTrait for Stream {
fn play(&self) -> Result<(), PlayStreamError> {
let mut stream = self.inner.lock();
let mut stream = self.inner.lock().unwrap();

if !stream.playing {
if let Err(e) = stream.audio_unit.start() {
Expand All @@ -910,7 +909,7 @@ impl StreamTrait for Stream {
}

fn pause(&self) -> Result<(), PauseStreamError> {
let mut stream = self.inner.lock();
let mut stream = self.inner.lock().unwrap();

if stream.playing {
if let Err(e) = stream.audio_unit.stop() {
Expand Down
1 change: 0 additions & 1 deletion src/host/coreaudio/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
extern crate coreaudio;
extern crate parking_lot;

use self::coreaudio::sys::{
kAudioFormatFlagIsFloat, kAudioFormatFlagIsPacked, kAudioFormatLinearPCM,
Expand Down

0 comments on commit bdbbf75

Please sign in to comment.