1 #include "../shm/dconf-shm.h"
3 #include "dconf-mock.h"
11 static GHashTable
*dconf_mock_shm_table
;
12 static GMutex dconf_mock_shm_lock
;
13 static GString
*dconf_mock_shm_log
;
16 dconf_mock_shm_unref (gpointer data
)
18 DConfMockShm
*shm
= data
;
20 if (g_atomic_int_dec_and_test (&shm
->ref_count
))
21 g_slice_free (DConfMockShm
, shm
);
25 dconf_mock_shm_ref (DConfMockShm
*shm
)
27 g_atomic_int_inc (&shm
->ref_count
);
33 dconf_shm_open (const gchar
*name
)
37 g_mutex_lock (&dconf_mock_shm_lock
);
39 if G_UNLIKELY (dconf_mock_shm_table
== NULL
)
41 dconf_mock_shm_table
= g_hash_table_new_full (g_str_hash
, g_str_equal
, g_free
, dconf_mock_shm_unref
);
42 dconf_mock_shm_log
= g_string_new (NULL
);
45 shm
= g_hash_table_lookup (dconf_mock_shm_table
, name
);
48 shm
= g_slice_new0 (DConfMockShm
);
49 g_hash_table_insert (dconf_mock_shm_table
, g_strdup (name
), dconf_mock_shm_ref (shm
));
52 /* before unlocking... */
53 dconf_mock_shm_ref (shm
);
55 g_string_append_printf (dconf_mock_shm_log
, "open %s;", name
);
57 g_mutex_unlock (&dconf_mock_shm_lock
);
63 dconf_shm_close (guint8
*shm
)
67 g_mutex_lock (&dconf_mock_shm_lock
);
68 g_string_append (dconf_mock_shm_log
, "close;");
69 g_mutex_unlock (&dconf_mock_shm_lock
);
71 dconf_mock_shm_unref (shm
);
76 dconf_mock_shm_flag (const gchar
*name
)
81 g_mutex_lock (&dconf_mock_shm_lock
);
82 shm
= g_hash_table_lookup (dconf_mock_shm_table
, name
);
86 count
= shm
->ref_count
;
87 g_hash_table_remove (dconf_mock_shm_table
, name
);
89 g_mutex_unlock (&dconf_mock_shm_lock
);
95 dconf_mock_shm_reset (void)
97 g_mutex_lock (&dconf_mock_shm_lock
);
98 if (dconf_mock_shm_table
!= NULL
)
103 g_hash_table_iter_init (&iter
, dconf_mock_shm_table
);
104 while (g_hash_table_iter_next (&iter
, NULL
, &value
))
106 DConfMockShm
*shm
= value
;
108 g_assert_cmpint (shm
->ref_count
, ==, 1);
109 g_hash_table_iter_remove (&iter
);
112 g_string_truncate (dconf_mock_shm_log
, 0);
114 g_mutex_unlock (&dconf_mock_shm_lock
);
118 dconf_mock_shm_assert_log (const gchar
*expected_log
)
120 g_mutex_lock (&dconf_mock_shm_lock
);
121 g_assert_cmpstr (dconf_mock_shm_log
->str
, ==, expected_log
);
122 g_string_truncate (dconf_mock_shm_log
, 0);
123 g_mutex_unlock (&dconf_mock_shm_lock
);