From dce1b09c874eb87c913a08cc1b012843f8c13a11 Mon Sep 17 00:00:00 2001 From: Jordan Petridis Date: Mon, 30 Oct 2023 01:41:31 +0200 Subject: [PATCH] Add spawn_future_with_priority and spawn_future_local_with_priority Followup to #1201 Allow to set custom priority for the spawned futures. --- glib/src/functions.rs | 31 +++++++++++++++++++++++++++++-- 1 file changed, 29 insertions(+), 2 deletions(-) diff --git a/glib/src/functions.rs b/glib/src/functions.rs index d8fcb1b325f3..28ff07f56587 100644 --- a/glib/src/functions.rs +++ b/glib/src/functions.rs @@ -268,9 +268,21 @@ pub fn file_open_tmp( /// where main context is running, e.g. via a `MainLoop`. pub fn spawn_future + Send + 'static>( f: F, +) -> crate::JoinHandle { + spawn_future_with_priority(crate::source::Priority::DEFAULT, f) +} + +// rustdoc-stripper-ignore-next +/// Spawn a new infallible `Future` on the main context, with a non-default priority. +/// +/// This can be called from any thread and will execute the future from the thread +/// where main context is running, e.g. via a `MainLoop`. +pub fn spawn_future_with_priority + Send + 'static>( + priority: crate::source::Priority, + f: F, ) -> crate::JoinHandle { let ctx = crate::MainContext::ref_thread_default(); - ctx.spawn(f) + ctx.spawn_with_priority(priority, f) } // rustdoc-stripper-ignore-next @@ -283,7 +295,22 @@ pub fn spawn_future + Send /// `with_thread_default` or `acquire` on the main context. pub fn spawn_future_local + 'static>( f: F, +) -> crate::JoinHandle { + spawn_future_local_with_priority(crate::source::Priority::DEFAULT, f) +} + +// rustdoc-stripper-ignore-next +/// Spawn a new infallible `Future` on the main context, with a non-default priority. +/// +/// The given `Future` does not have to be `Send`. +/// +/// This can be called only from the thread where the main context is running, e.g. +/// from any other `Future` that is executed on this main context, or after calling +/// `with_thread_default` or `acquire` on the main context. +pub fn spawn_future_local_with_priority + 'static>( + priority: crate::source::Priority, + f: F, ) -> crate::JoinHandle { let ctx = crate::MainContext::ref_thread_default(); - ctx.spawn_local(f) + ctx.spawn_local_with_priority(priority, f) }