Remove parking_lot dependency (#846)

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.
This commit is contained in:
James Liu 2024-02-24 16:48:53 -08:00 • committed by GitHub
parent 4eec3e3441
commit bdbbf75d82
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
8 changed files with 19 additions and 29 deletions

View file

@ -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 {

View file

@ -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,
@ -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;
@ -252,6 +250,7 @@ impl Device {
let handle_result = self
.handles
.lock()
.unwrap()
.take(&self.name, stream_type)
.map_err(|e| (e, e.errno()));
@ -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()));

View file

@ -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;
@ -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)]

View file

@ -1,5 +1,4 @@
extern crate asio_sys as sys;
extern crate parking_lot;
use crate::traits::{DeviceTrait, HostTrait, StreamTrait};
use crate::{

View file

@ -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 {
@ -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,
@ -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,
@ -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),
@ -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),

View file

@ -28,7 +28,6 @@ use crate::{
SupportedBufferSize, SupportedStreamConfig, SupportedStreamConfigRange,
SupportedStreamConfigsError,
};
use parking_lot::Mutex;
use std::ffi::CStr;
use std::fmt;
use std::mem;
@ -36,7 +35,7 @@ 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::{
@ -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 {
@ -464,7 +463,7 @@ where
},
move || {
let _ = stream_copy.pause();
(error_callback.lock())(StreamError::DeviceNotAvailable);
(error_callback.lock().unwrap())(StreamError::DeviceNotAvailable);
},
)?);
Ok(())
@ -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,
@ -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)
}
@ -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,
@ -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)
}
@ -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() {
@ -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() {

View file

@ -1,5 +1,4 @@
extern crate coreaudio;
extern crate parking_lot;
use self::coreaudio::sys::{
kAudioFormatFlagIsFloat, kAudioFormatFlagIsPacked, kAudioFormatLinearPCM,