Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: switch to async-mutex for Mutex implementation #822

Merged
merged 2 commits into from Jun 26, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 3 additions & 1 deletion Cargo.toml
Expand Up @@ -48,6 +48,7 @@ std = [
"slab",
"wasm-bindgen-futures",
"futures-channel",
"async-mutex",
]
alloc = [
"futures-core/alloc",
Expand All @@ -58,6 +59,7 @@ tokio02 = ["smol/tokio02"]
[dependencies]
async-attributes = { version = "1.1.1", optional = true }
async-task = { version = "3.0.0", optional = true }
async-mutex = { version = "1.1.3", optional = true }
crossbeam-utils = { version = "0.7.2", optional = true }
futures-core = { version = "0.3.4", optional = true, default-features = false }
futures-io = { version = "0.3.4", optional = true }
Expand All @@ -75,7 +77,7 @@ futures-timer = { version = "3.0.2", optional = true }
surf = { version = "1.0.3", optional = true }

[target.'cfg(not(target_os = "unknown"))'.dependencies]
smol = { version = "0.1.14", optional = true }
smol = { version = "0.1.17", optional = true }

[target.'cfg(target_arch = "wasm32")'.dependencies]
futures-timer = { version = "3.0.2", optional = true, features = ["wasm-bindgen"] }
Expand Down
8 changes: 4 additions & 4 deletions src/sync/condvar.rs
Expand Up @@ -2,7 +2,7 @@ use std::fmt;
use std::pin::Pin;
use std::time::Duration;

use super::mutex::{guard_lock, MutexGuard};
use super::MutexGuard;
use crate::future::{timeout, Future};
use crate::sync::WakerSet;
use crate::task::{Context, Poll};
Expand Down Expand Up @@ -120,7 +120,7 @@ impl Condvar {
/// ```
#[allow(clippy::needless_lifetimes)]
pub async fn wait<'a, T>(&self, guard: MutexGuard<'a, T>) -> MutexGuard<'a, T> {
let mutex = guard_lock(&guard);
let mutex = MutexGuard::source(&guard);

self.await_notify(guard).await;

Expand Down Expand Up @@ -228,7 +228,7 @@ impl Condvar {
guard: MutexGuard<'a, T>,
dur: Duration,
) -> (MutexGuard<'a, T>, WaitTimeoutResult) {
let mutex = guard_lock(&guard);
let mutex = MutexGuard::source(&guard);
match timeout(dur, self.wait(guard)).await {
Ok(guard) => (guard, WaitTimeoutResult(false)),
Err(_) => (mutex.lock().await, WaitTimeoutResult(true)),
Expand Down Expand Up @@ -281,7 +281,7 @@ impl Condvar {
where
F: FnMut(&mut T) -> bool,
{
let mutex = guard_lock(&guard);
let mutex = MutexGuard::source(&guard);
match timeout(dur, self.wait_until(guard, condition)).await {
Ok(guard) => (guard, WaitTimeoutResult(false)),
Err(_) => (mutex.lock().await, WaitTimeoutResult(true)),
Expand Down
5 changes: 3 additions & 2 deletions src/sync/mod.rs
Expand Up @@ -176,10 +176,11 @@
#[doc(inline)]
pub use std::sync::{Arc, Weak};

pub use mutex::{Mutex, MutexGuard};
#[doc(inline)]
pub use async_mutex::{Mutex, MutexGuard};

pub use rwlock::{RwLock, RwLockReadGuard, RwLockWriteGuard};

mod mutex;
mod rwlock;

cfg_unstable! {
Expand Down
294 changes: 0 additions & 294 deletions src/sync/mutex.rs

This file was deleted.

4 changes: 2 additions & 2 deletions tests/timeout.rs
Expand Up @@ -10,9 +10,9 @@ wasm_bindgen_test::wasm_bindgen_test_configure!(run_in_browser);
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)]
fn timeout_future_many() {
task::block_on(async {
let futures = (0..100)
let futures = (0..10)
.map(|i| {
timeout(Duration::from_millis(i * 20), async move {
timeout(Duration::from_millis(i * 50), async move {
task::sleep(Duration::from_millis(i)).await;
Ok::<(), async_std::future::TimeoutError>(())
})
Expand Down