m: Remove the simple-mutex dependency

Remove the simple-mutex dependency

It appears that this was originally done when std::sync::Mutex used the
PThreads implementation, which was much slower than other
contemporary Mutex implementations (like parking_lot). Therefore it
made sense to use a custom Mutex implementation here. However std
Mutexes now use futexes, which are much faster than the previous
implementation.

In addition, the original git history for simple-mutex appears to be lost
to time. The only copy of the source code is on crates.io, and the crate
is not owned by anyone. So it is problematic to use it anyways, as
updating it would require us to go through the painstaking process of
reclaiming the name.

This commit removes the dependency on simple-mutex and replaces its
usages with std::sync::Mutex for the reasons listed above. The
performance impact of this change has not been measured, but I believe
it to be negligible.

Also bumps MSRV to 1.41.

Signed-off-by: John Nunley <dev@notgull.net>
This commit is contained in:
John Nunley 2023-09-20 19:29:36 -07:00 committed by GitHub
parent 113c472443
commit 50851bfd8e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 18 additions and 9 deletions

View File

@ -49,7 +49,7 @@ jobs:
matrix:
# When updating this, the reminder to update the minimum supported
# Rust version in Cargo.toml.
rust: ['1.36']
rust: ['1.41.0']
steps:
- uses: actions/checkout@v4
- name: Install Rust

View File

@ -6,7 +6,7 @@ name = "async-dup"
version = "1.2.2"
authors = ["Stjepan Glavina <stjepang@gmail.com>"]
edition = "2018"
rust-version = "1.36"
rust-version = "1.41"
description = "Duplicate an async I/O handle"
license = "Apache-2.0 OR MIT"
repository = "https://github.com/smol-rs/async-dup"
@ -18,7 +18,6 @@ exclude = ["/.*"]
[dependencies]
futures-io = "0.3.5"
simple-mutex = "1.1.5"
[dev-dependencies]
futures = { version = "0.3.5", default-features = false, features = ["std"] }

View File

@ -60,6 +60,7 @@ use std::hash::{Hash, Hasher};
use std::io::{self, IoSlice, IoSliceMut, SeekFrom};
use std::ops::{Deref, DerefMut};
use std::pin::Pin;
use std::sync::TryLockError;
use std::task::{Context, Poll};
use futures_io::{AsyncRead, AsyncSeek, AsyncWrite};
@ -226,7 +227,7 @@ where
/// - `impl<T> AsyncWrite for &Mutex<T> where T: AsyncWrite + Unpin {}`
/// - `impl<T> AsyncSeek for Mutex<T> where T: AsyncSeek + Unpin {}`
/// - `impl<T> AsyncSeek for &Mutex<T> where T: AsyncSeek + Unpin {}`
pub struct Mutex<T>(simple_mutex::Mutex<T>);
pub struct Mutex<T>(std::sync::Mutex<T>);
impl<T> Mutex<T> {
/// Creates a new mutex.
@ -256,7 +257,7 @@ impl<T> Mutex<T> {
/// assert_eq!(*guard, 10);
/// ```
pub fn lock(&self) -> MutexGuard<'_, T> {
MutexGuard(self.0.lock())
MutexGuard(self.0.lock().unwrap_or_else(|e| e.into_inner()))
}
/// Attempts to acquire the mutex.
@ -278,7 +279,16 @@ impl<T> Mutex<T> {
/// # ;
/// ```
pub fn try_lock(&self) -> Option<MutexGuard<'_, T>> {
self.0.try_lock().map(MutexGuard)
self.0
.try_lock()
.map_or_else(
|e| match e {
TryLockError::Poisoned(e) => Some(e.into_inner()),
TryLockError::WouldBlock => None,
},
Some,
)
.map(MutexGuard)
}
/// Consumes the mutex, returning the underlying data.
@ -292,7 +302,7 @@ impl<T> Mutex<T> {
/// assert_eq!(mutex.into_inner(), 10);
/// ```
pub fn into_inner(self) -> T {
self.0.into_inner()
self.0.into_inner().unwrap_or_else(|e| e.into_inner())
}
/// Returns a mutable reference to the underlying data.
@ -310,7 +320,7 @@ impl<T> Mutex<T> {
/// assert_eq!(*mutex.lock(), 10);
/// ```
pub fn get_mut(&mut self) -> &mut T {
self.0.get_mut()
self.0.get_mut().unwrap_or_else(|e| e.into_inner())
}
}
@ -451,7 +461,7 @@ impl<T: AsyncSeek + Unpin> AsyncSeek for &Mutex<T> {
}
/// A guard that releases the mutex when dropped.
pub struct MutexGuard<'a, T>(simple_mutex::MutexGuard<'a, T>);
pub struct MutexGuard<'a, T>(std::sync::MutexGuard<'a, T>);
impl<T: fmt::Debug> fmt::Debug for MutexGuard<'_, T> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {