libnm: use G_PARAM_EXPLICIT_NOTIFY for direct boolean properties

Now that we require glib 2.42, we can use G_PARAM_EXPLICIT_NOTIFY flag.
The benefit is that this flag saves a notification, when the property
value does not change.

The downside is, that implementations of set_property() must remember to
emit _notify() when required. This is somewhat alleviated by using
_nm_setting_property_set_property_direct(), which does this
automatically.

Se the flag for G_PARAM_EXPLICIT_NOTIFY for direct boolean properties.
For now, only do it for boolean properties, because of the danger of
getting this wrong. We must review all callers to make sure that they
don't implement set_properties() and don't forget to notify.
This commit is contained in:
Thomas Haller 2023-12-11 12:09:13 +01:00
parent 13179a62d3
commit 4c31c73bf6
No known key found for this signature in database
GPG key ID: 29C2366E4DFC5728
4 changed files with 35 additions and 18 deletions

View file

@ -6555,7 +6555,7 @@ nm_setting_ip_config_class_init(NMSettingIPConfigClass *klass)
"",
"",
FALSE,
G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
G_PARAM_READWRITE | G_PARAM_EXPLICIT_NOTIFY | G_PARAM_STATIC_STRINGS);
/**
* NMSettingIPConfig:ignore-auto-dns:
@ -6571,7 +6571,7 @@ nm_setting_ip_config_class_init(NMSettingIPConfigClass *klass)
"",
"",
FALSE,
G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
G_PARAM_READWRITE | G_PARAM_EXPLICIT_NOTIFY | G_PARAM_STATIC_STRINGS);
/**
* NMSettingIPConfig:dhcp-hostname:
@ -6602,7 +6602,7 @@ nm_setting_ip_config_class_init(NMSettingIPConfigClass *klass)
"",
"",
TRUE,
G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
G_PARAM_READWRITE | G_PARAM_EXPLICIT_NOTIFY | G_PARAM_STATIC_STRINGS);
/**
* NMSettingIPConfig:never-default:
@ -6616,7 +6616,7 @@ nm_setting_ip_config_class_init(NMSettingIPConfigClass *klass)
"",
"",
FALSE,
G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
G_PARAM_READWRITE | G_PARAM_EXPLICIT_NOTIFY | G_PARAM_STATIC_STRINGS);
/**
* NMSettingIPConfig:may-fail:
@ -6634,7 +6634,7 @@ nm_setting_ip_config_class_init(NMSettingIPConfigClass *klass)
"",
"",
TRUE,
G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
G_PARAM_READWRITE | G_PARAM_EXPLICIT_NOTIFY | G_PARAM_STATIC_STRINGS);
/**
* NMSettingIPConfig:dad-timeout:

View file

@ -542,12 +542,12 @@ _nm_properties_override(GArray *properties_override, const NMSettInfoProperty *p
\
nm_assert(NM_IN_SET(_default_value, 0, 1)); \
\
_param_spec = \
g_param_spec_boolean("" prop_name "", \
"", \
"", \
_default_value, \
G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | (param_flags)); \
_param_spec = g_param_spec_boolean("" prop_name "", \
"", \
"", \
_default_value, \
G_PARAM_READWRITE | G_PARAM_EXPLICIT_NOTIFY \
| G_PARAM_STATIC_STRINGS | (param_flags)); \
\
(obj_properties)[(prop_id)] = _param_spec; \
\

View file

@ -989,13 +989,10 @@ _nm_setting_property_set_property_direct(GObject *object,
nm_assert_not_reached();
out_notify:
/* If explicit-notify would be set, we would need to emit g_object_notify_by_pspec().
*
* Currently we never set that, also because we still support glib 2.40. */
nm_assert(!NM_FLAGS_HAS(pspec->flags, 1 << 30 /* G_PARAM_EXPLICIT_NOTIFY */));
/* We only notify "direct_also_notify". The other property is automatically notified. */
nm_gobject_notify_together_by_pspec(object, property_info->direct_also_notify);
nm_gobject_notify_together_by_pspec(
object,
NM_FLAGS_HAS(pspec->flags, G_PARAM_EXPLICIT_NOTIFY) ? property_info->param_spec : NULL,
property_info->direct_also_notify);
return;

View file

@ -4902,6 +4902,26 @@ check_done:;
prop_idx_val = _PROP_IDX_PACK(meta_type, prop_idx);
g_array_append_val(property_types_data, prop_idx_val);
if (sip->param_spec) {
gboolean expected;
/* TODO: we should move all "direct" properties to use G_PARAM_EXPLICIT_NOTIFY.
*
* Currently only certain direct properties are as such. This should change.
*
* Warning: this is potentially dangerous, because implementations MUST remember
* to notify the property change in set_property(). Optimally, the property uses
* _nm_setting_property_set_property_direct(), which takes care of that.
*/
expected = NM_IN_SET(sip->property_type->direct_type, NM_VALUE_TYPE_BOOL);
if (NM_FLAGS_HAS(sip->param_spec->flags, G_PARAM_EXPLICIT_NOTIFY)) {
g_assert(expected);
} else {
g_assert(!expected);
}
}
if (sip->param_spec) {
nm_auto_unset_gvalue GValue val = G_VALUE_INIT;