Debian Codegraph @

testsuite/test-object.c

raw
1 /* test-object.c
2 *
3 * Copyright 2022 Christian Hergert <[email protected]>
4 *
5 * This library is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU Lesser General Public License as
7 * published by the Free Software Foundation; either version 2.1 of the
8 * License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program. If not, see <http://www.gnu.org/licenses/>.
17 *
18 * SPDX-License-Identifier: LGPL-2.1-or-later
19 */
20
21 #include "config.h"
22
23 #include <stdatomic.h>
24
25 #include <libdex.h>
26
27 #include "dex-object-private.h"
28
29 static guint finalize_count;
30
31 #define TEST_TYPE_OBJECT (test_object_get_type())
32 #define TEST_IS_OBJECT(obj) (G_TYPE_CHECK_INSTANCE_TYPE(obj, TEST_TYPE_OBJECT))
33 #define TEST_OBJECT(obj) (G_TYPE_CHECK_INSTANCE_CAST(obj, TEST_TYPE_OBJECT, TestObject))
34
35 GType test_object_get_type (void) G_GNUC_CONST;
36
37 typedef struct _TestObject
38 {
39 DexObject parent_instance;
40 int field1;
41 guint field2;
42 double field3;
43 const char *field4;
44 } TestObject;
45
46 typedef struct _TestObjectClass
47 {
48 DexObjectClass parent_class;
49 } TestObjectClass;
50
51 DEX_DEFINE_FINAL_TYPE (TestObject, test_object, DEX_TYPE_OBJECT)
52
53 static void
54 test_object_finalize (DexObject *object)
55 {
56 g_atomic_int_inc (&finalize_count);
57 DEX_OBJECT_CLASS (test_object_parent_class)->finalize (object);
58 }
59
60 static void
61 test_object_class_init (TestObjectClass *test_object_class)
62 {
63 DexObjectClass *object_class = DEX_OBJECT_CLASS (test_object_class);
64
65 object_class->finalize = test_object_finalize;
66 }
67
68 static void
69 test_object_init (TestObject *test_object)
70 {
71 }
72
73 static TestObject *
74 test_object_new (void)
75 {
76 return (TestObject *)g_type_create_instance (TEST_TYPE_OBJECT);
77 }
78
79 static void
80 test_weak_ref_st (void)
81 {
82 TestObject *to, *to2;
83 DexWeakRef wr;
84 DexWeakRef wr2;
85 DexWeakRef wr3;
86
87 finalize_count = 0;
88
89 dex_weak_ref_init (&wr, NULL);
90 g_assert_null (dex_weak_ref_get (&wr));
91
92 to = test_object_new ();
93 dex_weak_ref_set (&wr, to);
94
95 dex_ref (to);
96 dex_unref (to);
97
98 /* Add second weak-ref to test removing */
99 dex_weak_ref_init (&wr2, to);
100 dex_weak_ref_set (&wr2, to);
101
102 /* And a third weak-ref to test set API */
103 dex_weak_ref_init (&wr3, NULL);
104 dex_weak_ref_set (&wr3, to);
105 dex_weak_ref_set (&wr3, NULL);
106 dex_weak_ref_set (&wr3, to);
107
108 dex_weak_ref_clear (&wr2);
109 dex_weak_ref_clear (&wr3);
110
111 to2 = dex_weak_ref_get (&wr);
112 g_assert_true (to == to2);
113 dex_clear (&to2);
114 dex_clear (&to);
115 g_assert_cmpint (finalize_count, ==, 1);
116 g_assert_null (dex_weak_ref_get (&wr));
117
118 dex_weak_ref_clear (&wr);
119 }
120
121 typedef struct
122 {
123 TestObject *to;
124 DexWeakRef wr;
125 GMutex wait1;
126 GMutex wait2;
127 GCond cond1;
128 GCond cond2;
129 } TestDexWeakRefMt;
130
131 static gpointer
132 test_weak_ref_mt_worker (gpointer data)
133 {
134 TestDexWeakRefMt *state = data;
135 TestObject *to;
136
137 g_assert_nonnull (state);
138 g_assert_nonnull (state->to);
139
140 /* Test that we can get a full ref from the weak, as we
141 * should be certain to is still alive.
142 */
143 g_mutex_lock (&state->wait1);
144 to = dex_weak_ref_get (&state->wr);
145 g_assert_nonnull (to);
146 g_clear_pointer (&to, dex_unref);
147 g_cond_signal (&state->cond1);
148 g_mutex_unlock (&state->wait1);
149
150 /* Acquire the second lock (which is held by thread-1 until
151 * we signaled above, at which point it's waiting for a
152 * second condition to signal. The first thread should have
153 * dropped the final ref, and therefore our attempt to get
154 * a ref from the weak ref should fail.
155 */
156 g_mutex_lock (&state->wait2);
157 to = dex_weak_ref_get (&state->wr);
158 g_assert_null (to);
159 g_cond_signal (&state->cond2);
160 g_mutex_unlock (&state->wait2);
161
162 return NULL;
163 }
164
165 static void
166 test_weak_ref_mt (void)
167 {
168 TestDexWeakRefMt state;
169 GThread *thread;
170
171 finalize_count = 0;
172
173 state.to = test_object_new ();
174 dex_weak_ref_init (&state.wr, state.to);
175
176 g_mutex_init (&state.wait1);
177 g_mutex_init (&state.wait2);
178
179 g_cond_init (&state.cond1);
180 g_cond_init (&state.cond2);
181
182 g_mutex_lock (&state.wait1);
183 g_mutex_lock (&state.wait2);
184
185 thread = g_thread_new ("test_weak_ref_mt_worker", test_weak_ref_mt_worker, &state);
186
187 /* Now wait for thread-2 to signal us */
188 g_cond_wait (&state.cond1, &state.wait1);
189
190 /* We were signaled, now clear our ref, then allow thread-2
191 * to acquire wait2 while we wait again to be signaled.
192 */
193 g_clear_pointer (&state.to, dex_unref);
194 g_cond_wait (&state.cond2, &state.wait2);
195
196 g_mutex_unlock (&state.wait1);
197 g_mutex_unlock (&state.wait2);
198
199 g_thread_join (thread);
200
201 dex_weak_ref_clear (&state.wr);
202
203 g_mutex_clear (&state.wait1);
204 g_mutex_clear (&state.wait2);
205
206 g_cond_clear (&state.cond1);
207 g_cond_clear (&state.cond2);
208 }
209
210 typedef struct
211 {
212 TestObject *to;
213 DexWeakRef wr;
214 GMutex mutex;
215 GCond cond;
216 } TestDexWeakRefExtendLiveness;
217
218 static gpointer
219 test_weak_ref_extend_liveness_worker (gpointer data)
220 {
221 TestDexWeakRefExtendLiveness *state = data;
222
223 /* Prelock the weak-ref mutex so that we can make the
224 * first thread block on acquiring it after it signals
225 * our thread to continue.
226 */
227 g_mutex_lock (&state->wr.mutex);
228
229 g_assert (state->to != NULL);
230
231 /* Now wait for thread-1 to release the mutex (while waiting
232 * for our signal) and we can signal it to continue.
233 */
234 g_mutex_lock (&state->mutex);
235 g_cond_signal (&state->cond);
236 g_mutex_unlock (&state->mutex);
237
238 /* At this point, it is likely that we will get the first
239 * thread to block on state->wr.mutex, but since we don't have
240 * a condition to be signaled by, we just have to ... um, wait
241 * and hope our CPU isn't running on hamster wheels.
242 */
243 while (DEX_OBJECT (state->to)->ref_count > 0)
244 g_usleep (G_USEC_PER_SEC / 1000);
245
246 g_assert_cmpint (finalize_count, ==, 0);
247
248 /* We still have state->wr.mutex, so pretend we've extended
249 * liveness while it's attempting to finalize.
250 */
251 g_atomic_int_inc (&DEX_OBJECT (state->to)->weak_refs_watermark);
252 atomic_fetch_add_explicit (&DEX_OBJECT (state->to)->ref_count, 1, memory_order_relaxed);
253
254 /* Now release our mutex so we can race against the
255 * main thread to do the final unref.
256 */
257 g_mutex_unlock (&state->wr.mutex);
258
259 dex_unref (state->to);
260
261 return NULL;
262 }
263
264 static void
265 test_weak_ref_extend_liveness (void)
266 {
267 TestDexWeakRefExtendLiveness state;
268 GThread *thread;
269
270 finalize_count = 0;
271
272 state.to = test_object_new ();
273 dex_weak_ref_init (&state.wr, state.to);
274 g_mutex_init (&state.mutex);
275 g_cond_init (&state.cond);
276
277 g_mutex_lock (&state.mutex);
278
279 thread = g_thread_new ("test_weak_ref_extend_liveness_worker",
280 test_weak_ref_extend_liveness_worker,
281 &state);
282
283 g_cond_wait (&state.cond, &state.mutex);
284 dex_unref (state.to);
285
286 g_mutex_unlock (&state.mutex);
287
288 g_thread_join (thread);
289
290 state.to = NULL;
291 dex_weak_ref_clear (&state.wr);
292 g_mutex_clear (&state.mutex);
293 g_cond_clear (&state.cond);
294
295 g_assert_cmpint (finalize_count, ==, 1);
296 }
297
298 typedef struct
299 {
300 TestObject *to;
301 DexWeakRef wr;
302 GMutex mutex;
303 GCond cond;
304 } TestDexWeakRefImmortal;
305
306 static gpointer
307 test_weak_ref_immortal_worker (gpointer data)
308 {
309 TestDexWeakRefImmortal *state = data;
310 DexObject *borrowed;
311 TestObject *to;
312
313 borrowed = DEX_OBJECT (state->to);
314
315 g_mutex_lock (&state->mutex);
316
317 /* Acquire weak ref lock to force main thread to block */
318 dex_object_lock (borrowed);
319
320 /* Now signal other thread to continue */
321 g_cond_signal (&state->cond);
322 g_mutex_unlock (&state->mutex);
323
324 /* Now wait until the ref count reaches zero, then bump the watermark
325 * by creating a new reference.
326 */
327 while (g_atomic_int_get (&borrowed->ref_count) > 0)
328 g_usleep (G_USEC_PER_SEC / 1000);
329
330 to = dex_weak_ref_get (&state->wr);
331 g_assert_nonnull (to);
332
333 /* UB here really, since it's overflow */
334 g_assert_cmpint (DEX_OBJECT (to)->weak_refs_watermark, ==, 0);
335
336 /* Now release the lock allowing the main thread to continue */
337 dex_object_unlock (borrowed);
338
339 g_mutex_lock (&state->mutex);
340 dex_unref (to);
341 g_mutex_unlock (&state->mutex);
342
343 dex_weak_ref_clear (&state->wr);
344
345 return NULL;
346 }
347
348 static void
349 test_weak_ref_immortal (void)
350 {
351 TestDexWeakRefImmortal state;
352 DexObject *borrowed;
353 GThread *thread;
354
355 finalize_count = 0;
356
357 g_mutex_init (&state.mutex);
358 g_cond_init (&state.cond);
359
360 state.to = test_object_new ();
361 borrowed = DEX_OBJECT (state.to);
362 dex_weak_ref_init (&state.wr, state.to);
363
364 /* Make object immortal */
365 borrowed->weak_refs_watermark = G_MAXUINT32;
366
367 /* Now force a race on liveness from thread */
368 g_mutex_lock (&state.mutex);
369 thread = g_thread_new ("test_weak_ref_immortal", test_weak_ref_immortal_worker, &state);
370 g_cond_wait (&state.cond, &state.mutex);
371
372 /* Try to unref forcing the race (blocking while acquiring the weak ref
373 * mutex until the thread allows us to continue).
374 */
375 dex_unref (state.to);
376 g_assert_cmpint (finalize_count, ==, 0);
377
378 /* Now release our lock allowing the thread to cleanup */
379 g_mutex_unlock (&state.mutex);
380
381 g_thread_join (thread);
382
383 g_mutex_clear (&state.mutex);
384 g_cond_clear (&state.cond);
385
386 /* Clear watermark and unref so we are valgrind clean. Consumers
387 * can't actually do this, but that is the point.
388 */
389 g_assert_cmpint (finalize_count, ==, 0);
390 g_assert_cmpint (borrowed->ref_count, ==, 1);
391 g_assert_cmpint (borrowed->weak_refs_watermark, ==, 1);
392 borrowed->weak_refs_watermark = 1;
393 borrowed->ref_count = 1;
394 dex_unref (state.to);
395 g_assert_cmpint (finalize_count, ==, 1);
396 }
397
398 static gboolean test_weak_ref_thread_guantlet_waiting;
399
400 static gpointer
401 test_weak_ref_thread_guantlet_worker (gpointer data)
402 {
403 DexWeakRef *wr = data;
404 TestObject *to;
405 guint i = 0;
406
407 while ((to = dex_weak_ref_get (wr)))
408 {
409 dex_unref (to);
410 g_usleep (G_USEC_PER_SEC / 10000 * g_random_int_range (0, 100));
411
412 /* It is possible, given a devilish enough of a thread scheduler, that
413 * this could infinitely cause us to run due to always having a ref
414 * greater than zero in one thread.
415 *
416 * So we read an atomic every now and again to break out.
417 */
418 i++;
419 if ((i % 10) == 0 && g_atomic_int_get (&test_weak_ref_thread_guantlet_waiting))
420 break;
421 }
422
423 dex_weak_ref_clear (wr);
424 g_free (wr);
425
426 return NULL;
427 }
428
429 static void
430 test_weak_ref_thread_guantlet (void)
431 {
432 TestObject *to = test_object_new ();
433 GThread *threads[8];
434
435 finalize_count = 0;
436
437 for (guint i = 0; i < G_N_ELEMENTS (threads); i++)
438 {
439 DexWeakRef *wr = g_new0 (DexWeakRef, 1);
440 char *thread_name = g_strdup_printf ("test_weak_ref_thread_guantlet:%d", i);
441
442 dex_weak_ref_init (wr, to);
443 threads[i] = g_thread_new (thread_name, test_weak_ref_thread_guantlet_worker, wr);
444 g_free (thread_name);
445 }
446
447 g_assert_cmpint (g_atomic_int_get (&finalize_count), ==, 0);
448
449 g_usleep (G_USEC_PER_SEC / 3);
450 g_assert_cmpint (g_atomic_int_get (&finalize_count), ==, 0);
451
452 dex_unref (to);
453 g_atomic_int_set (&test_weak_ref_thread_guantlet_waiting, TRUE);
454
455 for (guint i = 0; i < G_N_ELEMENTS (threads); i++)
456 g_thread_join (threads[i]);
457
458 g_assert_cmpint (finalize_count, ==, 1);
459 }
460
461 static void
462 test_object_basic (void)
463 {
464 g_assert_true (dex_object_get_type() != G_TYPE_INVALID);
465 g_assert_true (G_TYPE_IS_INSTANTIATABLE (dex_object_get_type()));
466 g_assert_true (G_TYPE_IS_ABSTRACT (dex_object_get_type()));
467 }
468
469 int
470 main (int argc,
471 char *argv[])
472 {
473 dex_init ();
474 g_test_init (&argc, &argv, NULL);
475 g_test_add_func ("/Dex/TestSuite/Object/basic", test_object_basic);
476 g_test_add_func ("/Dex/TestSuite/WeakRef/single-threaded", test_weak_ref_st);
477 g_test_add_func ("/Dex/TestSuite/WeakRef/multi-threaded", test_weak_ref_mt);
478 g_test_add_func ("/Dex/TestSuite/WeakRef/extend-liveness", test_weak_ref_extend_liveness);
479 g_test_add_func ("/Dex/TestSuite/WeakRef/immortal", test_weak_ref_immortal);
480 g_test_add_func ("/Dex/TestSuite/WeakRef/thread-guantlet", test_weak_ref_thread_guantlet);
481 return g_test_run ();
482 }