diff options
author | Jon Gjengset <jon@thesquareplanet.com> | 2019-09-19 11:46:52 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-09-19 11:46:52 -0400 |
commit | e3415d8d615d95fcf4534c8596232bc549786663 (patch) | |
tree | b33305d8d8ea2b313f39cda1217d0471682ee0f4 /tokio | |
parent | d1f60ac4c69792211f9c9f3bcff1559bc0cb837a (diff) |
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.
Diffstat (limited to 'tokio')
-rw-r--r-- | tokio/examples/chat.rs | 15 | ||||
-rw-r--r-- | tokio/src/sync.rs | 4 |
2 files changed, 11 insertions, 8 deletions
diff --git a/tokio/examples/chat.rs b/tokio/examples/chat.rs index 719a2530..7fc317d8 100644 --- a/tokio/examples/chat.rs +++ b/tokio/examples/chat.rs @@ -27,12 +27,15 @@ #![warn(rust_2018_idioms)] use futures::{Poll, SinkExt, Stream, StreamExt}; -use std::{collections::HashMap, env, error::Error, io, net::SocketAddr, pin::Pin, task::Context}; +use std::{ + collections::HashMap, env, error::Error, io, net::SocketAddr, pin::Pin, sync::Arc, + task::Context, +}; use tokio::{ self, codec::{Framed, LinesCodec, LinesCodecError}, net::{TcpListener, TcpStream}, - sync::{mpsc, Lock}, + sync::{mpsc, Mutex}, }; #[tokio::main] @@ -42,7 +45,7 @@ async fn main() -> Result<(), Box<dyn Error>> { // The server task will hold a handle to this. For every new client, the // `state` handle is cloned and passed into the task that processes the // client connection. - let state = Lock::new(Shared::new()); + let state = Arc::new(Mutex::new(Shared::new())); let addr = env::args().nth(1).unwrap_or("127.0.0.1:6142".to_string()); @@ -58,7 +61,7 @@ async fn main() -> Result<(), Box<dyn Error>> { let (stream, addr) = listener.accept().await?; // Clone a handle to the `Shared` state for the new connection. - let state = state.clone(); + let state = Arc::clone(&state); // Spawn our handler to be run asynchronously. tokio::spawn(async move { @@ -129,7 +132,7 @@ impl Shared { impl Peer { /// Create a new instance of `Peer`. async fn new( - mut state: Lock<Shared>, + state: Arc<Mutex<Shared>>, lines: Framed<TcpStream, LinesCodec>, ) -> io::Result<Peer> { // Get the client socket address @@ -184,7 +187,7 @@ impl Stream for Peer { /// Process an individual chat client async fn process( - mut state: Lock<Shared>, + state: Arc<Mutex<Shared>>, stream: TcpStream, addr: SocketAddr, ) -> Result<(), Box<dyn Error>> { diff --git a/tokio/src/sync.rs b/tokio/src/sync.rs index d563d214..30471325 100644 --- a/tokio/src/sync.rs +++ b/tokio/src/sync.rs @@ -9,9 +9,9 @@ //! from one task to another. //! - [mpsc](mpsc/index.html), a multi-producer, single-consumer channel for //! sending values between tasks. -//! - [lock](lock/index.html), an asynchronous `Mutex`-like type. +//! - [`Mutex`](struct.Mutex.html), an asynchronous `Mutex`-like type. //! - [watch](watch/index.html), a single-producer, multi-consumer channel that //! only stores the **most recently** sent value. pub use tokio_sync::{mpsc, oneshot, watch}; -pub use tokio_sync::{Lock, LockGuard}; +pub use tokio_sync::{Mutex, MutexGuard}; |