Skip to content

Commit df9d809

Browse files
authored
Merge pull request #1256 from fbrouille/fix_concurrency_issues
Fix concurrency issues
2 parents d8f78d7 + 64c305c commit df9d809

File tree

3 files changed

+112
-71
lines changed

3 files changed

+112
-71
lines changed

glib-macros/src/enum_derive.rs

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,6 @@ use heck::{ToKebabCase, ToShoutySnakeCase, ToUpperCamelCase};
44
use proc_macro2::TokenStream;
55
use proc_macro_error::abort_call_site;
66
use quote::{format_ident, quote, quote_spanned, ToTokens};
7-
use syn::{
8-
parse_quote, punctuated::Punctuated, spanned::Spanned, token::Comma, Data, ExprArray, Ident,
9-
Variant,
10-
};
117

128
use crate::utils::{
139
crate_ident_new, gen_enum_from_glib, parse_nested_meta_items, parse_optional_nested_meta_items,
@@ -21,8 +17,8 @@ use crate::utils::{
2117
// value_nick: "goat\0" as *const _ as *const _,
2218
// },
2319
fn gen_enum_values(
24-
enum_name: &Ident,
25-
enum_variants: &Punctuated<Variant, Comma>,
20+
enum_name: &syn::Ident,
21+
enum_variants: &syn::punctuated::Punctuated<syn::Variant, syn::token::Comma>,
2622
) -> (TokenStream, usize) {
2723
let crate_ident = crate_ident_new();
2824

@@ -50,7 +46,7 @@ fn gen_enum_values(
5046

5147
n += 1;
5248
// generates a glib::gobject_ffi::GEnumValue.
53-
quote_spanned! {v.span()=>
49+
quote_spanned! {syn::spanned::Spanned::span(&v)=>
5450
#crate_ident::gobject_ffi::GEnumValue {
5551
value: #enum_name::#name as i32,
5652
value_name: #value_name as *const _ as *const _,
@@ -70,7 +66,7 @@ pub fn impl_enum(input: &syn::DeriveInput) -> TokenStream {
7066
let name = &input.ident;
7167

7268
let enum_variants = match input.data {
73-
Data::Enum(ref e) => &e.variants,
69+
syn::Data::Enum(ref e) => &e.variants,
7470
_ => abort_call_site!("#[derive(glib::Enum)] only supports enums"),
7571
};
7672
let (g_enum_values, nb_enum_values) = gen_enum_values(name, enum_variants);
@@ -227,7 +223,7 @@ pub fn impl_enum(input: &syn::DeriveInput) -> TokenStream {
227223
// Registers the enum as a static type.
228224
fn register_enum_as_static(
229225
crate_ident: &TokenStream,
230-
name: &Ident,
226+
name: &syn::Ident,
231227
gtype_name: syn::LitStr,
232228
g_enum_values: TokenStream,
233229
nb_enum_values: usize,
@@ -273,15 +269,15 @@ fn register_enum_as_dynamic(
273269
crate_ident: &TokenStream,
274270
plugin_ty: TokenStream,
275271
lazy_registration: bool,
276-
name: &Ident,
272+
name: &syn::Ident,
277273
gtype_name: syn::LitStr,
278274
g_enum_values: TokenStream,
279275
nb_enum_values: usize,
280276
) -> TokenStream {
281277
// Wrap each GEnumValue to EnumValue
282-
let g_enum_values_expr: ExprArray = parse_quote! { [#g_enum_values] };
278+
let g_enum_values_expr: syn::ExprArray = syn::parse_quote! { [#g_enum_values] };
283279
let enum_values_iter = g_enum_values_expr.elems.iter().map(|v| {
284-
quote_spanned! {v.span()=>
280+
quote_spanned! {syn::spanned::Spanned::span(&v)=>
285281
#crate_ident::EnumValue::unsafe_from(#v),
286282
}
287283
});
@@ -310,6 +306,7 @@ fn register_enum_as_dynamic(
310306
);
311307
// name of the static array to store the enumeration values.
312308
let enum_values_array = format_ident!("{}_VALUES", name.to_string().to_shouty_snake_case());
309+
313310
quote! {
314311
/// The registration status type: a tuple of the weak reference on the plugin and of the GLib type.
315312
struct #registration_status_type(<#plugin_ty as #crate_ident::clone::Downgrade>::Weak, #crate_ident::Type);
@@ -392,6 +389,7 @@ fn register_enum_as_dynamic(
392389

393390
// name of the static variable to store the GLib type.
394391
let gtype_status = format_ident!("{}_G_TYPE", name.to_string().to_shouty_snake_case());
392+
395393
quote! {
396394
/// The GLib type which can be safely shared between threads.
397395
static #gtype_status: ::std::sync::atomic::AtomicUsize = ::std::sync::atomic::AtomicUsize::new(#crate_ident::gobject_ffi::G_TYPE_INVALID);
@@ -400,7 +398,7 @@ fn register_enum_as_dynamic(
400398
/// Do nothing as the enum has been registered on implementation load.
401399
#[inline]
402400
fn register_enum() -> #crate_ident::Type {
403-
let gtype = #gtype_status.load(::std::sync::atomic::Ordering::Relaxed);
401+
let gtype = #gtype_status.load(::std::sync::atomic::Ordering::Acquire);
404402
unsafe { <#crate_ident::Type as #crate_ident::translate::FromGlib<#crate_ident::ffi::GType>>::from_glib(gtype) }
405403
}
406404

@@ -410,7 +408,7 @@ fn register_enum_as_dynamic(
410408
pub fn on_implementation_load(type_plugin: &#plugin_ty) -> bool {
411409
static VALUES: #enum_values;
412410
let gtype = #crate_ident::translate::IntoGlib::into_glib(<#plugin_ty as glib::prelude::DynamicObjectRegisterExt>::register_dynamic_enum(type_plugin, #gtype_name, VALUES.as_ref()));
413-
#gtype_status.store(gtype, ::std::sync::atomic::Ordering::Relaxed);
411+
#gtype_status.store(gtype, ::std::sync::atomic::Ordering::Release);
414412
gtype != #crate_ident::gobject_ffi::G_TYPE_INVALID
415413
}
416414

glib-macros/src/object_interface_attribute.rs

Lines changed: 56 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
// Take a look at the license at the top of the repository in the LICENSE file.
22

3+
use heck::ToShoutySnakeCase;
34
use proc_macro2::TokenStream;
45
use proc_macro_error::abort_call_site;
5-
use quote::{quote, ToTokens};
6+
use quote::{format_ident, quote, ToTokens};
67

78
use crate::utils::{parse_optional_nested_meta_items, NestedMetaItem};
89

@@ -21,6 +22,18 @@ pub fn impl_object_interface(input: &mut syn::ItemImpl) -> TokenStream {
2122
..
2223
} = input;
2324

25+
let self_ty_as_ident = match &**self_ty {
26+
syn::Type::Path(syn::TypePath { path, .. }) => path.require_ident(),
27+
_ => Err(syn::Error::new(
28+
syn::spanned::Spanned::span(self_ty),
29+
"expected this path to be an identifier",
30+
)),
31+
};
32+
let self_ty = match self_ty_as_ident {
33+
Ok(ident) => ident,
34+
Err(e) => return e.to_compile_error(),
35+
};
36+
2437
let mut plugin_type = NestedMetaItem::<syn::Path>::new("plugin_type").value_required();
2538
let mut lazy_registration =
2639
NestedMetaItem::<syn::LitBool>::new("lazy_registration").value_required();
@@ -95,7 +108,7 @@ pub fn impl_object_interface(input: &mut syn::ItemImpl) -> TokenStream {
95108
// Registers the object interface as a static type.
96109
fn register_object_interface_as_static(
97110
crate_ident: &TokenStream,
98-
self_ty: &syn::Type,
111+
self_ty: &syn::Ident,
99112
) -> TokenStream {
100113
// registers the interface on first use (lazy registration).
101114
quote! {
@@ -122,7 +135,7 @@ fn register_object_interface_as_static(
122135
// An object interface can be reregistered as a dynamic type.
123136
fn register_object_interface_as_dynamic(
124137
crate_ident: &TokenStream,
125-
self_ty: &syn::Type,
138+
self_ty: &syn::Ident,
126139
plugin_ty: TokenStream,
127140
lazy_registration: bool,
128141
) -> TokenStream {
@@ -132,32 +145,40 @@ fn register_object_interface_as_dynamic(
132145
// registers the object interface as a dynamic type on the first use (lazy registration).
133146
// a weak reference on the plugin is stored and will be used later on the first use of the object interface.
134147
// this implementation relies on a static storage of a weak reference on the plugin and of the GLib type to know if the object interface has been registered.
148+
149+
// the registration status type.
150+
let registration_status_type = format_ident!("{}RegistrationStatus", self_ty);
151+
// name of the static variable to store the registration status.
152+
let registration_status = format_ident!(
153+
"{}",
154+
registration_status_type.to_string().to_shouty_snake_case()
155+
);
156+
135157
quote! {
136-
impl #self_ty {
137-
/// Returns a mutable reference to the registration status: a tuple of the weak reference on the plugin and of the GLib type.
138-
/// This is safe because the mutable reference guarantees that no other threads are concurrently accessing the data.
139-
#[inline]
140-
fn get_registration_status_ref_mut() -> &'static mut Option<(<#plugin_ty as #crate_ident::clone::Downgrade>::Weak, #crate_ident::Type)> {
141-
static mut REGISTRATION_STATUS: ::std::sync::Mutex<Option<(<#plugin_ty as #crate_ident::clone::Downgrade>::Weak, #crate_ident::Type)>> = ::std::sync::Mutex::new(None);
142-
unsafe { REGISTRATION_STATUS.get_mut().unwrap() }
143-
}
158+
/// The registration status type: a tuple of the weak reference on the plugin and of the GLib type.
159+
struct #registration_status_type(<#plugin_ty as #crate_ident::clone::Downgrade>::Weak, #crate_ident::Type);
160+
unsafe impl Send for #registration_status_type {}
144161

162+
/// The registration status protected by a mutex guarantees so that no other threads are concurrently accessing the data.
163+
static #registration_status: ::std::sync::Mutex<Option<#registration_status_type>> = ::std::sync::Mutex::new(None);
164+
165+
impl #self_ty {
145166
/// Registers the object interface as a dynamic type within the plugin only once.
146167
/// Plugin must have been used at least once.
147168
/// Do nothing if plugin has never been used or if the object interface is already registered as a dynamic type.
148169
#[inline]
149170
fn register_interface() -> #crate_ident::Type {
150-
let registration_status_ref_mut = Self::get_registration_status_ref_mut();
151-
match registration_status_ref_mut {
171+
let mut registration_status = #registration_status.lock().unwrap();
172+
match ::std::ops::DerefMut::deref_mut(&mut registration_status) {
152173
// plugin has never been used, so the object interface cannot be registered as a dynamic type.
153174
None => #crate_ident::Type::INVALID,
154175
// plugin has been used and the object interface has not been registered yet, so registers it as a dynamic type.
155-
Some((type_plugin, type_)) if !type_.is_valid() => {
176+
Some(#registration_status_type(type_plugin, type_)) if !type_.is_valid() => {
156177
*type_ = #crate_ident::subclass::register_dynamic_interface::<#plugin_ty, Self>(&(type_plugin.upgrade().unwrap()));
157178
*type_
158179
},
159180
// plugin has been used and the object interface has already been registered as a dynamic type.
160-
Some((_, type_)) => *type_
181+
Some(#registration_status_type(_, type_)) => *type_
161182
}
162183
}
163184

@@ -168,15 +189,15 @@ fn register_object_interface_as_dynamic(
168189
/// If plugin is reused (and has reloaded the implementation) and the object interface has not been registered yet as a dynamic type, do nothing.
169190
#[inline]
170191
pub fn on_implementation_load(type_plugin: &#plugin_ty) -> bool {
171-
let registration_status_ref_mut = Self::get_registration_status_ref_mut();
172-
match registration_status_ref_mut {
192+
let mut registration_status = #registration_status.lock().unwrap();
193+
match ::std::ops::DerefMut::deref_mut(&mut registration_status) {
173194
// plugin has never been used (this is the first time), so postpones registration of the object interface as a dynamic type on the first use.
174195
None => {
175-
*registration_status_ref_mut = Some((#crate_ident::clone::Downgrade::downgrade(type_plugin), #crate_ident::Type::INVALID));
196+
*registration_status = Some(#registration_status_type(#crate_ident::clone::Downgrade::downgrade(type_plugin), #crate_ident::Type::INVALID));
176197
true
177198
},
178199
// plugin has been used at least one time and the object interface has been registered as a dynamic type at least one time, so re-registers it.
179-
Some((_, type_)) if type_.is_valid() => {
200+
Some(#registration_status_type(_, type_)) if type_.is_valid() => {
180201
*type_ = #crate_ident::subclass::register_dynamic_interface::<#plugin_ty, Self>(type_plugin);
181202
type_.is_valid()
182203
},
@@ -192,15 +213,15 @@ fn register_object_interface_as_dynamic(
192213
/// Else do nothing.
193214
#[inline]
194215
pub fn on_implementation_unload(type_plugin_: &#plugin_ty) -> bool {
195-
let registration_status_ref_mut = Self::get_registration_status_ref_mut();
196-
match registration_status_ref_mut {
216+
let mut registration_status = #registration_status.lock().unwrap();
217+
match ::std::ops::DerefMut::deref_mut(&mut registration_status) {
197218
// plugin has never been used, so unload implementation is unexpected.
198219
None => false,
199220
// plugin has been used at least one time and the object interface has been registered as a dynamic type at least one time.
200-
Some((_, type_)) if type_.is_valid() => true,
221+
Some(#registration_status_type(_, type_)) if type_.is_valid() => true,
201222
// plugin has been used at least one time but the object interface has not been registered yet as a dynamic type, so cancels the postponed registration.
202223
Some(_) => {
203-
*registration_status_ref_mut = None;
224+
*registration_status = None;
204225
true
205226
}
206227
}
@@ -209,29 +230,29 @@ fn register_object_interface_as_dynamic(
209230
}
210231
} else {
211232
// registers immediately the object interface as a dynamic type.
233+
234+
// name of the static variable to store the GLib type.
235+
let gtype_status = format_ident!("{}_G_TYPE", self_ty.to_string().to_shouty_snake_case());
236+
212237
quote! {
213-
impl #self_ty {
214-
/// Returns a mutable reference to the GLib type.
215-
/// This is safe because the mutable reference guarantees that no other threads are concurrently accessing the atomic data.
216-
#[inline]
217-
fn get_type_mut() -> &'static mut #crate_ident::ffi::GType {
218-
static mut TYPE: ::std::sync::atomic::AtomicUsize = ::std::sync::atomic::AtomicUsize::new(#crate_ident::gobject_ffi::G_TYPE_INVALID);
219-
unsafe { TYPE.get_mut() }
220-
}
238+
/// The GLib type which can be safely shared between threads.
239+
static #gtype_status: ::std::sync::atomic::AtomicUsize = ::std::sync::atomic::AtomicUsize::new(#crate_ident::gobject_ffi::G_TYPE_INVALID);
221240

241+
impl #self_ty {
222242
/// Do nothing as the object interface has been registered on implementation load.
223243
#[inline]
224244
fn register_interface() -> #crate_ident::Type {
225-
unsafe { <#crate_ident::Type as #crate_ident::translate::FromGlib<#crate_ident::ffi::GType>>::from_glib(*Self::get_type_mut()) }
245+
let gtype = #gtype_status.load(::std::sync::atomic::Ordering::Acquire);
246+
unsafe { <#crate_ident::Type as #crate_ident::translate::FromGlib<#crate_ident::ffi::GType>>::from_glib(gtype) }
226247
}
227248

228249
/// Registers the object interface as a dynamic type within the plugin.
229250
/// The object interface can be registered several times as a dynamic type.
230251
#[inline]
231252
pub fn on_implementation_load(type_plugin: &#plugin_ty) -> bool {
232-
let type_mut = Self::get_type_mut();
233-
*type_mut = #crate_ident::translate::IntoGlib::into_glib(#crate_ident::subclass::register_dynamic_interface::<#plugin_ty, Self>(type_plugin));
234-
*type_mut != #crate_ident::gobject_ffi::G_TYPE_INVALID
253+
let gtype = #crate_ident::translate::IntoGlib::into_glib(#crate_ident::subclass::register_dynamic_interface::<#plugin_ty, Self>(type_plugin));
254+
#gtype_status.store(gtype, ::std::sync::atomic::Ordering::Release);
255+
gtype != #crate_ident::gobject_ffi::G_TYPE_INVALID
235256
}
236257

237258
/// Do nothing as object interfaces registered as dynamic types are never unregistered.

0 commit comments

Comments
 (0)