Skip to content

Commit 50851bf

Browse files
authored
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>
1 parent 113c472 commit 50851bf

File tree

3 files changed

+18
-9
lines changed

3 files changed

+18
-9
lines changed

.github/workflows/ci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ jobs:
4949
matrix:
5050
# When updating this, the reminder to update the minimum supported
5151
# Rust version in Cargo.toml.
52-
rust: ['1.36']
52+
rust: ['1.41.0']
5353
steps:
5454
- uses: actions/checkout@v4
5555
- name: Install Rust

Cargo.toml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ name = "async-dup"
66
version = "1.2.2"
77
authors = ["Stjepan Glavina <stjepang@gmail.com>"]
88
edition = "2018"
9-
rust-version = "1.36"
9+
rust-version = "1.41"
1010
description = "Duplicate an async I/O handle"
1111
license = "Apache-2.0 OR MIT"
1212
repository = "https://github.com/smol-rs/async-dup"
@@ -18,7 +18,6 @@ exclude = ["/.*"]
1818

1919
[dependencies]
2020
futures-io = "0.3.5"
21-
simple-mutex = "1.1.5"
2221

2322
[dev-dependencies]
2423
futures = { version = "0.3.5", default-features = false, features = ["std"] }

src/lib.rs

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ use std::hash::{Hash, Hasher};
6060
use std::io::{self, IoSlice, IoSliceMut, SeekFrom};
6161
use std::ops::{Deref, DerefMut};
6262
use std::pin::Pin;
63+
use std::sync::TryLockError;
6364
use std::task::{Context, Poll};
6465

6566
use futures_io::{AsyncRead, AsyncSeek, AsyncWrite};
@@ -226,7 +227,7 @@ where
226227
/// - `impl<T> AsyncWrite for &Mutex<T> where T: AsyncWrite + Unpin {}`
227228
/// - `impl<T> AsyncSeek for Mutex<T> where T: AsyncSeek + Unpin {}`
228229
/// - `impl<T> AsyncSeek for &Mutex<T> where T: AsyncSeek + Unpin {}`
229-
pub struct Mutex<T>(simple_mutex::Mutex<T>);
230+
pub struct Mutex<T>(std::sync::Mutex<T>);
230231

231232
impl<T> Mutex<T> {
232233
/// Creates a new mutex.
@@ -256,7 +257,7 @@ impl<T> Mutex<T> {
256257
/// assert_eq!(*guard, 10);
257258
/// ```
258259
pub fn lock(&self) -> MutexGuard<'_, T> {
259-
MutexGuard(self.0.lock())
260+
MutexGuard(self.0.lock().unwrap_or_else(|e| e.into_inner()))
260261
}
261262

262263
/// Attempts to acquire the mutex.
@@ -278,7 +279,16 @@ impl<T> Mutex<T> {
278279
/// # ;
279280
/// ```
280281
pub fn try_lock(&self) -> Option<MutexGuard<'_, T>> {
281-
self.0.try_lock().map(MutexGuard)
282+
self.0
283+
.try_lock()
284+
.map_or_else(
285+
|e| match e {
286+
TryLockError::Poisoned(e) => Some(e.into_inner()),
287+
TryLockError::WouldBlock => None,
288+
},
289+
Some,
290+
)
291+
.map(MutexGuard)
282292
}
283293

284294
/// Consumes the mutex, returning the underlying data.
@@ -292,7 +302,7 @@ impl<T> Mutex<T> {
292302
/// assert_eq!(mutex.into_inner(), 10);
293303
/// ```
294304
pub fn into_inner(self) -> T {
295-
self.0.into_inner()
305+
self.0.into_inner().unwrap_or_else(|e| e.into_inner())
296306
}
297307

298308
/// Returns a mutable reference to the underlying data.
@@ -310,7 +320,7 @@ impl<T> Mutex<T> {
310320
/// assert_eq!(*mutex.lock(), 10);
311321
/// ```
312322
pub fn get_mut(&mut self) -> &mut T {
313-
self.0.get_mut()
323+
self.0.get_mut().unwrap_or_else(|e| e.into_inner())
314324
}
315325
}
316326

@@ -451,7 +461,7 @@ impl<T: AsyncSeek + Unpin> AsyncSeek for &Mutex<T> {
451461
}
452462

453463
/// A guard that releases the mutex when dropped.
454-
pub struct MutexGuard<'a, T>(simple_mutex::MutexGuard<'a, T>);
464+
pub struct MutexGuard<'a, T>(std::sync::MutexGuard<'a, T>);
455465

456466
impl<T: fmt::Debug> fmt::Debug for MutexGuard<'_, T> {
457467
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {

0 commit comments

Comments
 (0)