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:
Jon Gjengset
2019-09-19 11:46:52 -04:00
committed by GitHub
parent d1f60ac4c6
commit e3415d8d61
6 changed files with 168 additions and 195 deletions
@@ -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 || {