Skip to content

Mutex and RwLock arc method #74942

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

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions library/std/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,7 @@
#![feature(unwind_attributes)]
#![feature(vec_into_raw_parts)]
#![feature(wake_trait)]
#![feature(mutex_arc)]
// NB: the above list is sorted to minimize merge conflicts.
#![default_lib_allocator]

Expand Down
17 changes: 17 additions & 0 deletions library/std/src/sync/mutex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use crate::fmt;
use crate::mem;
use crate::ops::{Deref, DerefMut};
use crate::ptr;
use crate::sync::Arc;
use crate::sys_common::mutex as sys;
use crate::sys_common::poison::{self, LockResult, TryLockError, TryLockResult};

Expand Down Expand Up @@ -228,6 +229,22 @@ impl<T> Mutex<T> {
}
m
}

/// Creates a new mutex in an unlocked state ready for use, and wraps it in an [`Arc`].
///
/// [`Arc`]: ../../std/sync/struct.Arc.html
///
/// # Examples
///
/// ```
/// use std::sync::Mutex;
///
/// let mutex = Mutex::arc(0);
/// ```
#[unstable(feature = "mutex_arc", issue = "74866")]
pub fn arc(t: T) -> Arc<Mutex<T>> {
Arc::new(Mutex::new(t))
}
}

impl<T: ?Sized> Mutex<T> {
Expand Down
4 changes: 2 additions & 2 deletions library/std/src/sync/mutex/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ fn test_into_inner_drop() {

#[test]
fn test_into_inner_poison() {
let m = Arc::new(Mutex::new(NonCopy(10)));
let m = Mutex::arc(NonCopy(10));
let m2 = m.clone();
let _ = thread::spawn(move || {
let _lock = m2.lock().unwrap();
Expand All @@ -107,7 +107,7 @@ fn test_get_mut() {

#[test]
fn test_get_mut_poison() {
let m = Arc::new(Mutex::new(NonCopy(10)));
let m = Mutex::arc(NonCopy(10));
let m2 = m.clone();
let _ = thread::spawn(move || {
let _lock = m2.lock().unwrap();
Expand Down
21 changes: 19 additions & 2 deletions library/std/src/sync/rwlock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use crate::fmt;
use crate::mem;
use crate::ops::{Deref, DerefMut};
use crate::ptr;
use crate::sync::Arc;
use crate::sys_common::poison::{self, LockResult, TryLockError, TryLockResult};
use crate::sys_common::rwlock as sys;

Expand Down Expand Up @@ -133,6 +134,22 @@ impl<T> RwLock<T> {
data: UnsafeCell::new(t),
}
}

/// Creates a new instance of an `RwLock<T>` which is unlocked, and wraps it in an [`Arc`].
///
/// [`Arc`]: ../../std/sync/struct.Arc.html
///
/// # Examples
///
/// ```
/// use std::sync::RwLock;
///
/// let lock = RwLock::arc(5);
/// ```
#[unstable(feature = "mutex_arc", issue = "74866")]
pub fn arc(t: T) -> Arc<RwLock<T>> {
Arc::new(RwLock::new(t))
}
}

impl<T: ?Sized> RwLock<T> {
Expand Down Expand Up @@ -164,7 +181,7 @@ impl<T: ?Sized> RwLock<T> {
/// use std::sync::{Arc, RwLock};
/// use std::thread;
///
/// let lock = Arc::new(RwLock::new(1));
/// let lock = RwLock::arc(1);
/// let c_lock = Arc::clone(&lock);
///
/// let n = lock.read().unwrap();
Expand Down Expand Up @@ -320,7 +337,7 @@ impl<T: ?Sized> RwLock<T> {
/// use std::sync::{Arc, RwLock};
/// use std::thread;
///
/// let lock = Arc::new(RwLock::new(0));
/// let lock = RwLock::arc(0);
/// let c_lock = Arc::clone(&lock);
///
/// let _ = thread::spawn(move || {
Expand Down
2 changes: 1 addition & 1 deletion library/std/src/sync/rwlock/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ fn frob() {
const N: u32 = 10;
const M: usize = 1000;

let r = Arc::new(RwLock::new(()));
let r = RwLock::arc(());

let (tx, rx) = channel::<()>();
for _ in 0..N {
Expand Down