@@ -75,7 +75,7 @@ pub trait ContentProviderExt: 'static {
75
75
fn storable_formats ( & self ) -> ContentFormats ;
76
76
77
77
#[ doc( alias = "gdk_content_provider_write_mime_type_async" ) ]
78
- fn write_mime_type_async < P : FnOnce ( Result < ( ) , glib:: Error > ) + Send + ' static > (
78
+ fn write_mime_type_async < P : FnOnce ( Result < ( ) , glib:: Error > ) + ' static > (
79
79
& self ,
80
80
mime_type : & str ,
81
81
stream : & impl IsA < gio:: OutputStream > ,
@@ -124,17 +124,28 @@ impl<O: IsA<ContentProvider>> ContentProviderExt for O {
124
124
}
125
125
}
126
126
127
- fn write_mime_type_async < P : FnOnce ( Result < ( ) , glib:: Error > ) + Send + ' static > (
127
+ fn write_mime_type_async < P : FnOnce ( Result < ( ) , glib:: Error > ) + ' static > (
128
128
& self ,
129
129
mime_type : & str ,
130
130
stream : & impl IsA < gio:: OutputStream > ,
131
131
io_priority : glib:: Priority ,
132
132
cancellable : Option < & impl IsA < gio:: Cancellable > > ,
133
133
callback : P ,
134
134
) {
135
- let user_data: Box_ < P > = Box_ :: new ( callback) ;
135
+ let main_context = glib:: MainContext :: ref_thread_default ( ) ;
136
+ let is_main_context_owner = main_context. is_owner ( ) ;
137
+ let has_acquired_main_context = ( !is_main_context_owner)
138
+ . then ( || main_context. acquire ( ) . ok ( ) )
139
+ . flatten ( ) ;
140
+ assert ! (
141
+ is_main_context_owner || has_acquired_main_context. is_some( ) ,
142
+ "Async operations only allowed if the thread is owning the MainContext"
143
+ ) ;
144
+
145
+ let user_data: Box_ < glib:: thread_guard:: ThreadGuard < P > > =
146
+ Box_ :: new ( glib:: thread_guard:: ThreadGuard :: new ( callback) ) ;
136
147
unsafe extern "C" fn write_mime_type_async_trampoline <
137
- P : FnOnce ( Result < ( ) , glib:: Error > ) + Send + ' static ,
148
+ P : FnOnce ( Result < ( ) , glib:: Error > ) + ' static ,
138
149
> (
139
150
_source_object : * mut glib:: gobject_ffi:: GObject ,
140
151
res : * mut gio:: ffi:: GAsyncResult ,
@@ -151,7 +162,9 @@ impl<O: IsA<ContentProvider>> ContentProviderExt for O {
151
162
} else {
152
163
Err ( from_glib_full ( error) )
153
164
} ;
154
- let callback: Box_ < P > = Box_ :: from_raw ( user_data as * mut _ ) ;
165
+ let callback: Box_ < glib:: thread_guard:: ThreadGuard < P > > =
166
+ Box_ :: from_raw ( user_data as * mut _ ) ;
167
+ let callback: P = callback. into_inner ( ) ;
155
168
callback ( result) ;
156
169
}
157
170
let callback = write_mime_type_async_trampoline :: < P > ;
0 commit comments