Skip to content

Commit b2d4007

Browse files
authored
Replace crossbeam with std channel in mini-tokio (#822)
1 parent ec2ebb3 commit b2d4007

File tree

2 files changed

+7
-10
lines changed

2 files changed

+7
-10
lines changed

tutorial-code/mini-tokio/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,3 @@ publish = false
99

1010
[dependencies]
1111
futures = "0.3"
12-
crossbeam = "0.8"

tutorial-code/mini-tokio/src/main.rs

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,13 @@ use futures::future::BoxFuture;
66
use std::cell::RefCell;
77
use std::future::Future;
88
use std::pin::Pin;
9-
use std::sync::{Arc, Mutex};
9+
use std::sync::{mpsc, Arc, Mutex};
1010
use std::task::{Context, Poll, Waker};
1111
use std::thread;
1212
use std::time::{Duration, Instant};
1313
// A utility that allows us to implement a `std::task::Waker` without having to
1414
// use `unsafe` code.
1515
use futures::task::{self, ArcWake};
16-
// Used as a channel to queue scheduled tasks.
17-
use crossbeam::channel;
1816

1917
// Main entry point. A mini-tokio instance is created and a few tasks are
2018
// spawned. Our mini-tokio implementation only supports spawning tasks and
@@ -60,16 +58,16 @@ struct MiniTokio {
6058
// is ready to make progress. This usually happens when a resource the task
6159
// uses becomes ready to perform an operation. For example, a socket
6260
// received data and a `read` call will succeed.
63-
scheduled: channel::Receiver<Arc<Task>>,
61+
scheduled: mpsc::Receiver<Arc<Task>>,
6462

6563
// Send half of the scheduled channel.
66-
sender: channel::Sender<Arc<Task>>,
64+
sender: mpsc::Sender<Arc<Task>>,
6765
}
6866

6967
impl MiniTokio {
7068
/// Initialize a new mini-tokio instance.
7169
fn new() -> MiniTokio {
72-
let (sender, scheduled) = channel::unbounded();
70+
let (sender, scheduled) = mpsc::channel();
7371

7472
MiniTokio { scheduled, sender }
7573
}
@@ -232,7 +230,7 @@ async fn delay(dur: Duration) {
232230
// Used to track the current mini-tokio instance so that the `spawn` function is
233231
// able to schedule spawned tasks.
234232
thread_local! {
235-
static CURRENT: RefCell<Option<channel::Sender<Arc<Task>>>> =
233+
static CURRENT: RefCell<Option<mpsc::Sender<Arc<Task>>>> =
236234
RefCell::new(None);
237235
}
238236

@@ -250,7 +248,7 @@ struct Task {
250248

251249
// When a task is notified, it is queued into this channel. The executor
252250
// pops notified tasks and executes them.
253-
executor: channel::Sender<Arc<Task>>,
251+
executor: mpsc::Sender<Arc<Task>>,
254252
}
255253

256254
impl Task {
@@ -259,7 +257,7 @@ impl Task {
259257
// Initializes a new Task harness containing the given future and pushes it
260258
// onto `sender`. The receiver half of the channel will get the task and
261259
// execute it.
262-
fn spawn<F>(future: F, sender: &channel::Sender<Arc<Task>>)
260+
fn spawn<F>(future: F, sender: &mpsc::Sender<Arc<Task>>)
263261
where
264262
F: Future<Output = ()> + Send + 'static,
265263
{

0 commit comments

Comments
 (0)