Skip to content

Commit dce1b09

Browse files
committed
Add spawn_future_with_priority and spawn_future_local_with_priority
Followup to #1201 Allow to set custom priority for the spawned futures.
1 parent e93e899 commit dce1b09

File tree

1 file changed

+29
-2
lines changed

1 file changed

+29
-2
lines changed

glib/src/functions.rs

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -268,9 +268,21 @@ pub fn file_open_tmp(
268268
/// where main context is running, e.g. via a `MainLoop`.
269269
pub fn spawn_future<R: Send + 'static, F: std::future::Future<Output = R> + Send + 'static>(
270270
f: F,
271+
) -> crate::JoinHandle<R> {
272+
spawn_future_with_priority(crate::source::Priority::DEFAULT, f)
273+
}
274+
275+
// rustdoc-stripper-ignore-next
276+
/// Spawn a new infallible `Future` on the main context, with a non-default priority.
277+
///
278+
/// This can be called from any thread and will execute the future from the thread
279+
/// where main context is running, e.g. via a `MainLoop`.
280+
pub fn spawn_future_with_priority<R: Send + 'static, F: std::future::Future<Output = R> + Send + 'static>(
281+
priority: crate::source::Priority,
282+
f: F,
271283
) -> crate::JoinHandle<R> {
272284
let ctx = crate::MainContext::ref_thread_default();
273-
ctx.spawn(f)
285+
ctx.spawn_with_priority(priority, f)
274286
}
275287

276288
// rustdoc-stripper-ignore-next
@@ -283,7 +295,22 @@ pub fn spawn_future<R: Send + 'static, F: std::future::Future<Output = R> + Send
283295
/// `with_thread_default` or `acquire` on the main context.
284296
pub fn spawn_future_local<R: 'static, F: std::future::Future<Output = R> + 'static>(
285297
f: F,
298+
) -> crate::JoinHandle<R> {
299+
spawn_future_local_with_priority(crate::source::Priority::DEFAULT, f)
300+
}
301+
302+
// rustdoc-stripper-ignore-next
303+
/// Spawn a new infallible `Future` on the main context, with a non-default priority.
304+
///
305+
/// The given `Future` does not have to be `Send`.
306+
///
307+
/// This can be called only from the thread where the main context is running, e.g.
308+
/// from any other `Future` that is executed on this main context, or after calling
309+
/// `with_thread_default` or `acquire` on the main context.
310+
pub fn spawn_future_local_with_priority<R: 'static, F: std::future::Future<Output = R> + 'static>(
311+
priority: crate::source::Priority,
312+
f: F,
286313
) -> crate::JoinHandle<R> {
287314
let ctx = crate::MainContext::ref_thread_default();
288-
ctx.spawn_local(f)
315+
ctx.spawn_local_with_priority(priority, f)
289316
}

0 commit comments

Comments
 (0)