mirror of
https://github.com/tokio-rs/tokio.git
synced 2026-08-28 00:00:11 +02:00
sync: Make Lock more similar to std::sync::Mutex (#1573)
This renames `Lock` to `Mutex`, and brings the API more in line with `std::sync::Mutex`. In partcular, locking now only takes `&self`, with the expectation that you place the `Mutex` in an `Arc` (or something similar) to share it between threads. Fixes #1544. Part of #1210.
This commit is contained in:
@@ -1,12 +1,13 @@
|
||||
#![warn(rust_2018_idioms)]
|
||||
|
||||
use tokio_sync::Lock;
|
||||
use std::sync::Arc;
|
||||
use tokio_sync::Mutex;
|
||||
use tokio_test::task::spawn;
|
||||
use tokio_test::{assert_pending, assert_ready};
|
||||
|
||||
#[test]
|
||||
fn straight_execution() {
|
||||
let mut l = Lock::new(100);
|
||||
let l = Mutex::new(100);
|
||||
|
||||
{
|
||||
let mut t = spawn(l.lock());
|
||||
@@ -22,21 +23,15 @@ fn straight_execution() {
|
||||
}
|
||||
{
|
||||
let mut t = spawn(l.lock());
|
||||
let mut g = assert_ready!(t.poll());
|
||||
let g = assert_ready!(t.poll());
|
||||
assert_eq!(&*g, &98);
|
||||
|
||||
// We can continue to access the guard even if the lock is dropped
|
||||
drop(t);
|
||||
drop(l);
|
||||
*g = 97;
|
||||
assert_eq!(&*g, &97);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn readiness() {
|
||||
let mut l1 = Lock::new(100);
|
||||
let mut l2 = l1.clone();
|
||||
let l1 = Arc::new(Mutex::new(100));
|
||||
let l2 = Arc::clone(&l1);
|
||||
let mut t1 = spawn(l1.lock());
|
||||
let mut t2 = spawn(l2.lock());
|
||||
|
||||
@@ -55,7 +50,7 @@ fn readiness() {
|
||||
#[test]
|
||||
#[ignore]
|
||||
fn lock() {
|
||||
let mut lock = Lock::new(false);
|
||||
let mut lock = Mutex::new(false);
|
||||
|
||||
let mut lock2 = lock.clone();
|
||||
std::thread::spawn(move || {
|
||||
Reference in New Issue
Block a user