Skip to content

glib: make spawn*() return the SourceId #400

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 3, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 9 additions & 8 deletions glib/src/main_context_futures.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use crate::MainContext;
use crate::MainLoop;
use crate::Priority;
use crate::Source;
use crate::SourceId;

// Wrapper around Send Futures and non-Send Futures that will panic
// if the non-Send Future is polled/dropped from a different thread
Expand Down Expand Up @@ -240,8 +241,8 @@ impl MainContext {
///
/// 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<F: Future<Output = ()> + Send + 'static>(&self, f: F) {
self.spawn_with_priority(crate::PRIORITY_DEFAULT, f);
pub fn spawn<F: Future<Output = ()> + Send + 'static>(&self, f: F) -> SourceId {
self.spawn_with_priority(crate::PRIORITY_DEFAULT, f)
}

/// Spawn a new infallible `Future` on the main context.
Expand All @@ -251,8 +252,8 @@ impl MainContext {
/// 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_local<F: Future<Output = ()> + 'static>(&self, f: F) {
self.spawn_local_with_priority(crate::PRIORITY_DEFAULT, f);
pub fn spawn_local<F: Future<Output = ()> + 'static>(&self, f: F) -> SourceId {
self.spawn_local_with_priority(crate::PRIORITY_DEFAULT, f)
}

/// Spawn a new infallible `Future` on the main context, with a non-default priority.
Expand All @@ -263,10 +264,10 @@ impl MainContext {
&self,
priority: Priority,
f: F,
) {
) -> SourceId {
let f = FutureObj::new(Box::new(f));
let source = TaskSource::new(priority, FutureWrapper::Send(f));
source.attach(Some(&*self));
source.attach(Some(&*self))
}

/// Spawn a new infallible `Future` on the main context, with a non-default priority.
Expand All @@ -280,13 +281,13 @@ impl MainContext {
&self,
priority: Priority,
f: F,
) {
) -> SourceId {
let _acquire = self
.acquire()
.expect("Spawning local futures only allowed on the thread owning the MainContext");
let f = LocalFutureObj::new(Box::new(f));
let source = TaskSource::new(priority, FutureWrapper::NonSend(ThreadGuard::new(f)));
source.attach(Some(&*self));
source.attach(Some(&*self))
}

/// Runs a new, infallible `Future` on the main context and block until it finished, returning
Expand Down