Skip to content

Commit

Permalink
Add spawn_future_with_priority and spawn_future_local_with_priority
Browse files Browse the repository at this point in the history
Followup to gtk-rs#1201

Allow to set custom priority for the spawned futures.
  • Loading branch information
alatiera committed Oct 29, 2023
1 parent e93e899 commit dce1b09
Showing 1 changed file with 29 additions and 2 deletions.
31 changes: 29 additions & 2 deletions glib/src/functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -268,9 +268,21 @@ pub fn file_open_tmp(
/// where main context is running, e.g. via a `MainLoop`.
pub fn spawn_future<R: Send + 'static, F: std::future::Future<Output = R> + Send + 'static>(
f: F,
) -> crate::JoinHandle<R> {
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<R: Send + 'static, F: std::future::Future<Output = R> + Send + 'static>(
priority: crate::source::Priority,
f: F,
) -> crate::JoinHandle<R> {
let ctx = crate::MainContext::ref_thread_default();
ctx.spawn(f)
ctx.spawn_with_priority(priority, f)
}

// rustdoc-stripper-ignore-next
Expand All @@ -283,7 +295,22 @@ pub fn spawn_future<R: Send + 'static, F: std::future::Future<Output = R> + Send
/// `with_thread_default` or `acquire` on the main context.
pub fn spawn_future_local<R: 'static, F: std::future::Future<Output = R> + 'static>(
f: F,
) -> crate::JoinHandle<R> {
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<R: 'static, F: std::future::Future<Output = R> + 'static>(
priority: crate::source::Priority,
f: F,
) -> crate::JoinHandle<R> {
let ctx = crate::MainContext::ref_thread_default();
ctx.spawn_local(f)
ctx.spawn_local_with_priority(priority, f)
}

0 comments on commit dce1b09

Please sign in to comment.