@@ -95,28 +95,19 @@ G_LOCK_DEFINE_STATIC (properties_lock);
95
95
96
96
/* ---------------------------------------------------------------------------------------------------- */
97
97
98
- G_LOCK_DEFINE_STATIC (signal_subscription_lock );
99
-
100
- typedef struct
101
- {
102
- volatile gint ref_count ;
103
- GDBusProxy * proxy ;
104
- } SignalSubscriptionData ;
105
-
106
- static SignalSubscriptionData *
107
- signal_subscription_ref (SignalSubscriptionData * data )
98
+ static GWeakRef *
99
+ weak_ref_new (GObject * object )
108
100
{
109
- g_atomic_int_inc (& data -> ref_count );
110
- return data ;
101
+ GWeakRef * weak_ref = g_new0 (GWeakRef , 1 );
102
+ g_weak_ref_init (weak_ref , object );
103
+ return g_steal_pointer (& weak_ref );
111
104
}
112
105
113
106
static void
114
- signal_subscription_unref ( SignalSubscriptionData * data )
107
+ weak_ref_free ( GWeakRef * weak_ref )
115
108
{
116
- if (g_atomic_int_dec_and_test (& data -> ref_count ))
117
- {
118
- g_slice_free (SignalSubscriptionData , data );
119
- }
109
+ g_weak_ref_clear (weak_ref );
110
+ g_free (weak_ref );
120
111
}
121
112
122
113
/* ---------------------------------------------------------------------------------------------------- */
@@ -152,8 +143,6 @@ struct _GDBusProxyPrivate
152
143
153
144
/* mutable, protected by properties_lock */
154
145
GDBusObject * object ;
155
-
156
- SignalSubscriptionData * signal_subscription_data ;
157
146
};
158
147
159
148
enum
@@ -189,22 +178,6 @@ G_DEFINE_TYPE_WITH_CODE (GDBusProxy, g_dbus_proxy, G_TYPE_OBJECT,
189
178
G_IMPLEMENT_INTERFACE (G_TYPE_INITABLE , initable_iface_init )
190
179
G_IMPLEMENT_INTERFACE (G_TYPE_ASYNC_INITABLE , async_initable_iface_init ))
191
180
192
- static void
193
- g_dbus_proxy_dispose (GObject * object )
194
- {
195
- GDBusProxy * proxy = G_DBUS_PROXY (object );
196
- G_LOCK (signal_subscription_lock );
197
- if (proxy -> priv -> signal_subscription_data != NULL )
198
- {
199
- proxy -> priv -> signal_subscription_data -> proxy = NULL ;
200
- signal_subscription_unref (proxy -> priv -> signal_subscription_data );
201
- proxy -> priv -> signal_subscription_data = NULL ;
202
- }
203
- G_UNLOCK (signal_subscription_lock );
204
-
205
- G_OBJECT_CLASS (g_dbus_proxy_parent_class )-> dispose (object );
206
- }
207
-
208
181
static void
209
182
g_dbus_proxy_finalize (GObject * object )
210
183
{
@@ -346,7 +319,6 @@ g_dbus_proxy_class_init (GDBusProxyClass *klass)
346
319
{
347
320
GObjectClass * gobject_class = G_OBJECT_CLASS (klass );
348
321
349
- gobject_class -> dispose = g_dbus_proxy_dispose ;
350
322
gobject_class -> finalize = g_dbus_proxy_finalize ;
351
323
gobject_class -> set_property = g_dbus_proxy_set_property ;
352
324
gobject_class -> get_property = g_dbus_proxy_get_property ;
@@ -638,9 +610,6 @@ static void
638
610
g_dbus_proxy_init (GDBusProxy * proxy )
639
611
{
640
612
proxy -> priv = g_dbus_proxy_get_instance_private (proxy );
641
- proxy -> priv -> signal_subscription_data = g_slice_new0 (SignalSubscriptionData );
642
- proxy -> priv -> signal_subscription_data -> ref_count = 1 ;
643
- proxy -> priv -> signal_subscription_data -> proxy = proxy ;
644
613
proxy -> priv -> properties = g_hash_table_new_full (g_str_hash ,
645
614
g_str_equal ,
646
615
g_free ,
@@ -868,21 +837,12 @@ on_signal_received (GDBusConnection *connection,
868
837
GVariant * parameters ,
869
838
gpointer user_data )
870
839
{
871
- SignalSubscriptionData * data = user_data ;
840
+ GWeakRef * proxy_weak = user_data ;
872
841
GDBusProxy * proxy ;
873
842
874
- G_LOCK (signal_subscription_lock );
875
- proxy = data -> proxy ;
843
+ proxy = G_DBUS_PROXY (g_weak_ref_get (proxy_weak ));
876
844
if (proxy == NULL )
877
- {
878
- G_UNLOCK (signal_subscription_lock );
879
- return ;
880
- }
881
- else
882
- {
883
- g_object_ref (proxy );
884
- G_UNLOCK (signal_subscription_lock );
885
- }
845
+ return ;
886
846
887
847
if (!proxy -> priv -> initialized )
888
848
goto out ;
@@ -929,8 +889,7 @@ on_signal_received (GDBusConnection *connection,
929
889
parameters );
930
890
931
891
out :
932
- if (proxy != NULL )
933
- g_object_unref (proxy );
892
+ g_clear_object (& proxy );
934
893
}
935
894
936
895
/* ---------------------------------------------------------------------------------------------------- */
@@ -1038,7 +997,7 @@ on_properties_changed (GDBusConnection *connection,
1038
997
GVariant * parameters ,
1039
998
gpointer user_data )
1040
999
{
1041
- SignalSubscriptionData * data = user_data ;
1000
+ GWeakRef * proxy_weak = user_data ;
1042
1001
gboolean emit_g_signal = FALSE;
1043
1002
GDBusProxy * proxy ;
1044
1003
const gchar * interface_name_for_signal ;
@@ -1052,18 +1011,9 @@ on_properties_changed (GDBusConnection *connection,
1052
1011
changed_properties = NULL ;
1053
1012
invalidated_properties = NULL ;
1054
1013
1055
- G_LOCK (signal_subscription_lock );
1056
- proxy = data -> proxy ;
1014
+ proxy = G_DBUS_PROXY (g_weak_ref_get (proxy_weak ));
1057
1015
if (proxy == NULL )
1058
- {
1059
- G_UNLOCK (signal_subscription_lock );
1060
- goto out ;
1061
- }
1062
- else
1063
- {
1064
- g_object_ref (proxy );
1065
- G_UNLOCK (signal_subscription_lock );
1066
- }
1016
+ return ;
1067
1017
1068
1018
if (!proxy -> priv -> initialized )
1069
1019
goto out ;
@@ -1150,11 +1100,9 @@ on_properties_changed (GDBusConnection *connection,
1150
1100
}
1151
1101
1152
1102
out :
1153
- if (changed_properties != NULL )
1154
- g_variant_unref (changed_properties );
1103
+ g_clear_pointer (& changed_properties , g_variant_unref );
1155
1104
g_free (invalidated_properties );
1156
- if (proxy != NULL )
1157
- g_object_unref (proxy );
1105
+ g_clear_object (& proxy );
1158
1106
}
1159
1107
1160
1108
/* ---------------------------------------------------------------------------------------------------- */
@@ -1258,8 +1206,7 @@ on_name_owner_changed_get_all_cb (GDBusConnection *connection,
1258
1206
{
1259
1207
G_LOCK (properties_lock );
1260
1208
g_free (data -> proxy -> priv -> name_owner );
1261
- data -> proxy -> priv -> name_owner = data -> name_owner ;
1262
- data -> name_owner = NULL ; /* to avoid an extra copy, we steal the string */
1209
+ data -> proxy -> priv -> name_owner = g_steal_pointer (& data -> name_owner );
1263
1210
g_hash_table_remove_all (data -> proxy -> priv -> properties );
1264
1211
G_UNLOCK (properties_lock );
1265
1212
if (result != NULL )
@@ -1289,23 +1236,14 @@ on_name_owner_changed (GDBusConnection *connection,
1289
1236
GVariant * parameters ,
1290
1237
gpointer user_data )
1291
1238
{
1292
- SignalSubscriptionData * data = user_data ;
1239
+ GWeakRef * proxy_weak = user_data ;
1293
1240
GDBusProxy * proxy ;
1294
1241
const gchar * old_owner ;
1295
1242
const gchar * new_owner ;
1296
1243
1297
- G_LOCK (signal_subscription_lock );
1298
- proxy = data -> proxy ;
1244
+ proxy = G_DBUS_PROXY (g_weak_ref_get (proxy_weak ));
1299
1245
if (proxy == NULL )
1300
- {
1301
- G_UNLOCK (signal_subscription_lock );
1302
- goto out ;
1303
- }
1304
- else
1305
- {
1306
- g_object_ref (proxy );
1307
- G_UNLOCK (signal_subscription_lock );
1308
- }
1246
+ return ;
1309
1247
1310
1248
/* if we are already trying to load properties, cancel that */
1311
1249
if (proxy -> priv -> get_all_cancellable != NULL )
@@ -1415,8 +1353,7 @@ on_name_owner_changed (GDBusConnection *connection,
1415
1353
}
1416
1354
1417
1355
out :
1418
- if (proxy != NULL )
1419
- g_object_unref (proxy );
1356
+ g_clear_object (& proxy );
1420
1357
}
1421
1358
1422
1359
/* ---------------------------------------------------------------------------------------------------- */
@@ -1762,8 +1699,8 @@ async_initable_init_first (GAsyncInitable *initable)
1762
1699
proxy -> priv -> interface_name ,
1763
1700
G_DBUS_SIGNAL_FLAGS_NONE ,
1764
1701
on_properties_changed ,
1765
- signal_subscription_ref ( proxy -> priv -> signal_subscription_data ),
1766
- (GDestroyNotify ) signal_subscription_unref );
1702
+ weak_ref_new ( G_OBJECT ( proxy ) ),
1703
+ (GDestroyNotify ) weak_ref_free );
1767
1704
}
1768
1705
1769
1706
if (!(proxy -> priv -> flags & G_DBUS_PROXY_FLAGS_DO_NOT_CONNECT_SIGNALS ))
@@ -1778,8 +1715,8 @@ async_initable_init_first (GAsyncInitable *initable)
1778
1715
NULL , /* arg0 */
1779
1716
G_DBUS_SIGNAL_FLAGS_NONE ,
1780
1717
on_signal_received ,
1781
- signal_subscription_ref ( proxy -> priv -> signal_subscription_data ),
1782
- (GDestroyNotify ) signal_subscription_unref );
1718
+ weak_ref_new ( G_OBJECT ( proxy ) ),
1719
+ (GDestroyNotify ) weak_ref_free );
1783
1720
}
1784
1721
1785
1722
if (proxy -> priv -> name != NULL &&
@@ -1794,8 +1731,8 @@ async_initable_init_first (GAsyncInitable *initable)
1794
1731
proxy -> priv -> name , /* arg0 */
1795
1732
G_DBUS_SIGNAL_FLAGS_NONE ,
1796
1733
on_name_owner_changed ,
1797
- signal_subscription_ref ( proxy -> priv -> signal_subscription_data ),
1798
- (GDestroyNotify ) signal_subscription_unref );
1734
+ weak_ref_new ( G_OBJECT ( proxy ) ),
1735
+ (GDestroyNotify ) weak_ref_free );
1799
1736
}
1800
1737
}
1801
1738
0 commit comments