Skip to content

Commit

Permalink
Merge pull request #1312 from sdroege/gio-async-no-send
Browse files Browse the repository at this point in the history
Don't require `Send` closures for GIO-style async functions
  • Loading branch information
sdroege authored Jan 24, 2022
2 parents c174cf7 + 8be43af commit e0d8d8d
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 7 deletions.
6 changes: 2 additions & 4 deletions src/analysis/bounds.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,10 +133,8 @@ impl Bounds {
}
let parameters = format_out_parameters(&out_parameters);
let error_type = find_error_type(env, function);
type_string = format!(
"FnOnce(Result<{}, {}>) + Send + 'static",
parameters, error_type
);
type_string =
format!("FnOnce(Result<{}, {}>) + 'static", parameters, error_type);
let bound_name = *self.unused.front().unwrap();
callback_info = Some(CallbackInfo {
callback_type: type_string.clone(),
Expand Down
34 changes: 31 additions & 3 deletions src/codegen/function_body_chunk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -716,12 +716,30 @@ impl Builder {
chunks: &mut Vec<Chunk>,
trampoline: &AsyncTrampoline,
) {
chunks.push(Chunk::Custom(String::from(
r###"
let main_context = glib::MainContext::ref_thread_default();
let is_main_context_owner = main_context.is_owner();
let has_acquired_main_context = (!is_main_context_owner)
.then(|| main_context.acquire().ok())
.flatten();
assert!(
is_main_context_owner || has_acquired_main_context.is_some(),
"Async operations only allowed if the thread is owning the MainContext"
);
"###,
)));

chunks.push(Chunk::Let {
name: "user_data".to_string(),
is_mut: false,
value: Box::new(Chunk::Custom("Box_::new(callback)".into())),
value: Box::new(Chunk::Custom(format!(
"Box_::new({}::new(callback))",
use_glib_type(env, "thread_guard::ThreadGuard")
))),
type_: Some(Box::new(Chunk::Custom(format!(
"Box_<{}>",
"Box_<{}<{}>>",
use_glib_type(env, "thread_guard::ThreadGuard"),
trampoline.bound_name
)))),
});
Expand Down Expand Up @@ -874,7 +892,17 @@ impl Builder {
is_mut: false,
value: Box::new(Chunk::Custom("Box_::from_raw(user_data as *mut _)".into())),
type_: Some(Box::new(Chunk::Custom(format!(
"Box_<{}>",
"Box_<{}<{}>>",
use_glib_type(env, "thread_guard::ThreadGuard"),
trampoline.bound_name
)))),
});
body.push(Chunk::Let {
name: "callback".to_string(),
is_mut: false,
value: Box::new(Chunk::Custom("callback.into_inner()".into())),
type_: Some(Box::new(Chunk::Custom(format!(
"{}",
trampoline.bound_name
)))),
});
Expand Down

0 comments on commit e0d8d8d

Please sign in to comment.