summaryrefslogtreecommitdiffstats
path: root/third_party/rust/async-task/examples
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-19 00:47:55 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-19 00:47:55 +0000
commit26a029d407be480d791972afb5975cf62c9360a6 (patch)
treef435a8308119effd964b339f76abb83a57c29483 /third_party/rust/async-task/examples
parentInitial commit. (diff)
downloadfirefox-26a029d407be480d791972afb5975cf62c9360a6.tar.xz
firefox-26a029d407be480d791972afb5975cf62c9360a6.zip
Adding upstream version 124.0.1.upstream/124.0.1
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'third_party/rust/async-task/examples')
-rw-r--r--third_party/rust/async-task/examples/spawn-local.rs73
-rw-r--r--third_party/rust/async-task/examples/spawn-on-thread.rs53
-rw-r--r--third_party/rust/async-task/examples/spawn.rs48
3 files changed, 174 insertions, 0 deletions
diff --git a/third_party/rust/async-task/examples/spawn-local.rs b/third_party/rust/async-task/examples/spawn-local.rs
new file mode 100644
index 0000000000..a9da1b4de9
--- /dev/null
+++ b/third_party/rust/async-task/examples/spawn-local.rs
@@ -0,0 +1,73 @@
+//! A simple single-threaded executor that can spawn non-`Send` futures.
+
+use std::cell::Cell;
+use std::future::Future;
+use std::rc::Rc;
+
+use async_task::{Runnable, Task};
+
+thread_local! {
+ // A queue that holds scheduled tasks.
+ static QUEUE: (flume::Sender<Runnable>, flume::Receiver<Runnable>) = flume::unbounded();
+}
+
+/// Spawns a future on the executor.
+fn spawn<F, T>(future: F) -> Task<T>
+where
+ F: Future<Output = T> + 'static,
+ T: 'static,
+{
+ // Create a task that is scheduled by pushing itself into the queue.
+ let schedule = |runnable| QUEUE.with(|(s, _)| s.send(runnable).unwrap());
+ let (runnable, task) = async_task::spawn_local(future, schedule);
+
+ // Schedule the task by pushing it into the queue.
+ runnable.schedule();
+
+ task
+}
+
+/// Runs a future to completion.
+fn run<F, T>(future: F) -> T
+where
+ F: Future<Output = T> + 'static,
+ T: 'static,
+{
+ // Spawn a task that sends its result through a channel.
+ let (s, r) = flume::unbounded();
+ spawn(async move { drop(s.send(future.await)) }).detach();
+
+ loop {
+ // If the original task has completed, return its result.
+ if let Ok(val) = r.try_recv() {
+ return val;
+ }
+
+ // Otherwise, take a task from the queue and run it.
+ QUEUE.with(|(_, r)| r.recv().unwrap().run());
+ }
+}
+
+fn main() {
+ let val = Rc::new(Cell::new(0));
+
+ // Run a future that increments a non-`Send` value.
+ run({
+ let val = val.clone();
+ async move {
+ // Spawn a future that increments the value.
+ let task = spawn({
+ let val = val.clone();
+ async move {
+ val.set(dbg!(val.get()) + 1);
+ }
+ });
+
+ val.set(dbg!(val.get()) + 1);
+ task.await;
+ }
+ });
+
+ // The value should be 2 at the end of the program.
+ dbg!(val.get());
+}
diff --git a/third_party/rust/async-task/examples/spawn-on-thread.rs b/third_party/rust/async-task/examples/spawn-on-thread.rs
new file mode 100644
index 0000000000..b0ec2f20a7
--- /dev/null
+++ b/third_party/rust/async-task/examples/spawn-on-thread.rs
@@ -0,0 +1,53 @@
+//! A function that runs a future to completion on a dedicated thread.
+
+use std::future::Future;
+use std::sync::Arc;
+use std::thread;
+
+use async_task::Task;
+use smol::future;
+
+/// Spawns a future on a new dedicated thread.
+///
+/// The returned task can be used to await the output of the future.
+fn spawn_on_thread<F, T>(future: F) -> Task<T>
+where
+ F: Future<Output = T> + Send + 'static,
+ T: Send + 'static,
+{
+ // Create a channel that holds the task when it is scheduled for running.
+ let (sender, receiver) = flume::unbounded();
+ let sender = Arc::new(sender);
+ let s = Arc::downgrade(&sender);
+
+ // Wrap the future into one that disconnects the channel on completion.
+ let future = async move {
+ // When the inner future completes, the sender gets dropped and disconnects the channel.
+ let _sender = sender;
+ future.await
+ };
+
+ // Create a task that is scheduled by sending it into the channel.
+ let schedule = move |runnable| s.upgrade().unwrap().send(runnable).unwrap();
+ let (runnable, task) = async_task::spawn(future, schedule);
+
+ // Schedule the task by sending it into the channel.
+ runnable.schedule();
+
+ // Spawn a thread running the task to completion.
+ thread::spawn(move || {
+ // Keep taking the task from the channel and running it until completion.
+ for runnable in receiver {
+ runnable.run();
+ }
+ });
+
+ task
+}
+
+fn main() {
+ // Spawn a future on a dedicated thread.
+ future::block_on(spawn_on_thread(async {
+ println!("Hello, world!");
+ }));
+}
diff --git a/third_party/rust/async-task/examples/spawn.rs b/third_party/rust/async-task/examples/spawn.rs
new file mode 100644
index 0000000000..3a648114c9
--- /dev/null
+++ b/third_party/rust/async-task/examples/spawn.rs
@@ -0,0 +1,48 @@
+//! A simple single-threaded executor.
+
+use std::future::Future;
+use std::panic::catch_unwind;
+use std::thread;
+
+use async_task::{Runnable, Task};
+use once_cell::sync::Lazy;
+use smol::future;
+
+/// Spawns a future on the executor.
+fn spawn<F, T>(future: F) -> Task<T>
+where
+ F: Future<Output = T> + Send + 'static,
+ T: Send + 'static,
+{
+ // A queue that holds scheduled tasks.
+ static QUEUE: Lazy<flume::Sender<Runnable>> = Lazy::new(|| {
+ let (sender, receiver) = flume::unbounded::<Runnable>();
+
+ // Start the executor thread.
+ thread::spawn(|| {
+ for runnable in receiver {
+ // Ignore panics inside futures.
+ let _ignore_panic = catch_unwind(|| runnable.run());
+ }
+ });
+
+ sender
+ });
+
+ // Create a task that is scheduled by pushing it into the queue.
+ let schedule = |runnable| QUEUE.send(runnable).unwrap();
+ let (runnable, task) = async_task::spawn(future, schedule);
+
+ // Schedule the task by pushing it into the queue.
+ runnable.schedule();
+
+ task
+}
+
+fn main() {
+ // Spawn a future and await its result.
+ let task = spawn(async {
+ println!("Hello, world!");
+ });
+ future::block_on(task);
+}