Rename scheduler methods to be consistent with tokio, more obvious

This commit is contained in:
Filip Tibell 2023-08-21 09:52:38 -05:00
parent dbf5c989c2
commit 888f00dd8b
6 changed files with 19 additions and 20 deletions

View file

@ -170,7 +170,7 @@ where
// Start up our web server
// TODO: Spawn a scheduler background task here,
// and communicate using an mpsc channel instead
sched.schedule_future_background_local(async move {
sched.spawn_local(async move {
bound
.http1_only(true) // Web sockets can only use http1
.http1_keepalive(true) // Web sockets must be kept alive

View file

@ -53,7 +53,7 @@ impl Service<Request<Body>> for NetServiceInner {
let sched = lua
.app_data_ref::<&Scheduler>()
.expect("Lua struct is missing scheduler");
sched.schedule_future_background_local(async move {
sched.spawn_local(async move {
// Create our new full websocket object, then
// schedule our handler to get called asap
let res = async move {

View file

@ -175,10 +175,8 @@ async fn process_spawn(
.app_data_ref::<&Scheduler>()
.expect("Lua struct is missing scheduler");
let fut = spawn_command(program, args, options);
let recv = sched.schedule_future_background(fut);
let (status, stdout, stderr) = recv
let (status, stdout, stderr) = sched
.spawn(spawn_command(program, args, options))
.await
.expect("Failed to receive result of spawned process")?;

View file

@ -105,7 +105,7 @@ where
.expect("Lua struct is missing scheduler");
let thread2 = thread.clone();
sched.schedule_future_thread(thread.clone(), async move {
sched.spawn_thread(thread.clone(), async move {
let duration = Duration::from_secs_f64(secs);
time::sleep(duration).await;
sched.push_back(thread2, args)?;

View file

@ -33,9 +33,8 @@ where
/**
Schedules a plain future to run in the background.
This will spawn the future both on the scheduler and
potentially on a different thread using [`task::spawn`],
meaning the provided future must implement [`Send`].
This will potentially spawn the future on a different thread, using
[`task::spawn`], meaning the provided future must implement [`Send`].
Returns a [`Receiver`] which may be `await`-ed
to retrieve the result of the spawned future.
@ -43,10 +42,10 @@ where
This [`Receiver`] may be safely ignored if the result of the
spawned future is not needed, the future will run either way.
*/
pub fn schedule_future_background<F, FR>(&self, fut: F) -> Receiver<FR>
pub fn spawn<F>(&self, fut: F) -> Receiver<F::Output>
where
F: Future<Output = FR> + Send + 'static,
FR: Send + 'static,
F: Future + Send + 'static,
F::Output: Send + 'static,
{
let (tx, rx) = oneshot::channel();
@ -55,6 +54,8 @@ where
tx.send(res).ok();
});
// NOTE: We must spawn a future on our scheduler which awaits
// the handle from tokio to start driving our future properly
let futs = self
.futures_background
.try_lock()
@ -67,13 +68,13 @@ where
}
/**
Equivalent to [`schedule_future_background`], except the
future is only spawned on the scheduler, on the main thread.
Equivalent to [`spawn`], except the future is only
spawned on the Lune scheduler, and on the main thread.
*/
pub fn schedule_future_background_local<F, FR>(&self, fut: F) -> Receiver<FR>
pub fn spawn_local<F>(&self, fut: F) -> Receiver<F::Output>
where
F: Future<Output = FR> + 'static,
FR: 'static,
F: Future + 'static,
F::Output: 'static,
{
let (tx, rx) = oneshot::channel();
@ -94,7 +95,7 @@ where
If the given future returns a [`LuaError`], that error will be passed to the given `thread`.
*/
pub fn schedule_future_thread<F, FR>(
pub fn spawn_thread<F, FR>(
&'fut self,
thread: impl IntoLuaThread<'fut>,
fut: F,

View file

@ -57,7 +57,7 @@ where
let sched = lua
.app_data_ref::<&Scheduler>()
.expect("Lua struct is missing scheduler");
sched.schedule_future_thread(thread, future)?;
sched.spawn_thread(thread, future)?;
Ok(())
}),
)?;