Skip to content

Commit 6cbf4ef

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 6cbf4ef

File tree

1 file changed

+35
-2
lines changed

1 file changed

+35
-2
lines changed

glib/src/functions.rs

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -268,9 +268,24 @@ 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<
281+
R: Send + 'static,
282+
F: std::future::Future<Output = R> + Send + 'static,
283+
>(
284+
priority: crate::source::Priority,
285+
f: F,
271286
) -> crate::JoinHandle<R> {
272287
let ctx = crate::MainContext::ref_thread_default();
273-
ctx.spawn(f)
288+
ctx.spawn_with_priority(priority, f)
274289
}
275290

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

0 commit comments

Comments
 (0)