File: test-object.c

package info (click to toggle)
libdex 1.0.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,136 kB
  • sloc: ansic: 13,114; asm: 226; makefile: 22; sh: 6; javascript: 5
file content (481 lines) | stat: -rw-r--r-- 12,275 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
/* test-object.c
 *
 * Copyright 2022 Christian Hergert <chergert@redhat.com>
 *
 * This library is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of the
 * License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * SPDX-License-Identifier: LGPL-2.1-or-later
 */

#include "config.h"

#include <stdatomic.h>

#include <libdex.h>

#include "dex-object-private.h"

static guint finalize_count;

#define TEST_TYPE_OBJECT (test_object_get_type())
#define TEST_IS_OBJECT(obj) (G_TYPE_CHECK_INSTANCE_TYPE(obj, TEST_TYPE_OBJECT))
#define TEST_OBJECT(obj) (G_TYPE_CHECK_INSTANCE_CAST(obj, TEST_TYPE_OBJECT, TestObject))

GType test_object_get_type (void) G_GNUC_CONST;

typedef struct _TestObject
{
  DexObject parent_instance;
  int field1;
  guint field2;
  double field3;
  const char *field4;
} TestObject;

typedef struct _TestObjectClass
{
  DexObjectClass parent_class;
} TestObjectClass;

DEX_DEFINE_FINAL_TYPE (TestObject, test_object, DEX_TYPE_OBJECT)

static void
test_object_finalize (DexObject *object)
{
  g_atomic_int_inc (&finalize_count);
  DEX_OBJECT_CLASS (test_object_parent_class)->finalize (object);
}

static void
test_object_class_init (TestObjectClass *test_object_class)
{
  DexObjectClass *object_class = DEX_OBJECT_CLASS (test_object_class);

  object_class->finalize = test_object_finalize;
}

static void
test_object_init (TestObject *test_object)
{
}

static TestObject *
test_object_new (void)
{
  return (TestObject *)g_type_create_instance (TEST_TYPE_OBJECT);
}

static void
test_weak_ref_st (void)
{
  TestObject *to, *to2;
  DexWeakRef wr;
  DexWeakRef wr2;
  DexWeakRef wr3;

  finalize_count = 0;

  dex_weak_ref_init (&wr, NULL);
  g_assert_null (dex_weak_ref_get (&wr));

  to = test_object_new ();
  dex_weak_ref_set (&wr, to);

  dex_ref (to);
  dex_unref (to);

  /* Add second weak-ref to test removing */
  dex_weak_ref_init (&wr2, to);
  dex_weak_ref_set (&wr2, to);

  /* And a third weak-ref to test set API */
  dex_weak_ref_init (&wr3, NULL);
  dex_weak_ref_set (&wr3, to);
  dex_weak_ref_set (&wr3, NULL);
  dex_weak_ref_set (&wr3, to);

  dex_weak_ref_clear (&wr2);
  dex_weak_ref_clear (&wr3);

  to2 = dex_weak_ref_get (&wr);
  g_assert_true (to == to2);
  dex_clear (&to2);
  dex_clear (&to);
  g_assert_cmpint (finalize_count, ==, 1);
  g_assert_null (dex_weak_ref_get (&wr));

  dex_weak_ref_clear (&wr);
}

typedef struct
{
  TestObject *to;
  DexWeakRef wr;
  GMutex wait1;
  GMutex wait2;
  GCond cond1;
  GCond cond2;
} TestDexWeakRefMt;

static gpointer
test_weak_ref_mt_worker (gpointer data)
{
  TestDexWeakRefMt *state = data;
  TestObject *to;

  g_assert_nonnull (state);
  g_assert_nonnull (state->to);

  /* Test that we can get a full ref from the weak, as we
   * should be certain to is still alive.
   */
  g_mutex_lock (&state->wait1);
  to = dex_weak_ref_get (&state->wr);
  g_assert_nonnull (to);
  g_clear_pointer (&to, dex_unref);
  g_cond_signal (&state->cond1);
  g_mutex_unlock (&state->wait1);

  /* Acquire the second lock (which is held by thread-1 until
   * we signaled above, at which point it's waiting for a
   * second condition to signal. The first thread should have
   * dropped the final ref, and therefore our attempt to get
   * a ref from the weak ref should fail.
   */
  g_mutex_lock (&state->wait2);
  to = dex_weak_ref_get (&state->wr);
  g_assert_null (to);
  g_cond_signal (&state->cond2);
  g_mutex_unlock (&state->wait2);

  return NULL;
}

static void
test_weak_ref_mt (void)
{
  TestDexWeakRefMt state;
  GThread *thread;

  finalize_count = 0;

  state.to = test_object_new ();
  dex_weak_ref_init (&state.wr, state.to);

  g_mutex_init (&state.wait1);
  g_mutex_init (&state.wait2);

  g_cond_init (&state.cond1);
  g_cond_init (&state.cond2);

  g_mutex_lock (&state.wait1);
  g_mutex_lock (&state.wait2);

  thread = g_thread_new ("test_weak_ref_mt_worker", test_weak_ref_mt_worker, &state);

  /* Now wait for thread-2 to signal us */
  g_cond_wait (&state.cond1, &state.wait1);

  /* We were signaled, now clear our ref, then allow thread-2
   * to acquire wait2 while we wait again to be signaled.
   */
  g_clear_pointer (&state.to, dex_unref);
  g_cond_wait (&state.cond2, &state.wait2);

  g_mutex_unlock (&state.wait1);
  g_mutex_unlock (&state.wait2);

  g_thread_join (thread);

  dex_weak_ref_clear (&state.wr);

  g_mutex_clear (&state.wait1);
  g_mutex_clear (&state.wait2);

  g_cond_clear (&state.cond1);
  g_cond_clear (&state.cond2);
}

typedef struct
{
  TestObject *to;
  DexWeakRef wr;
  GMutex mutex;
  GCond cond;
} TestDexWeakRefExtendLiveness;

static gpointer
test_weak_ref_extend_liveness_worker (gpointer data)
{
  TestDexWeakRefExtendLiveness *state = data;

  /* Prelock the weak-ref mutex so that we can make the
   * first thread block on acquiring it after it signals
   * our thread to continue.
   */
  g_mutex_lock (&state->wr.mutex);

  g_assert (state->to != NULL);

  /* Now wait for thread-1 to release the mutex (while waiting
   * for our signal) and we can signal it to continue.
   */
  g_mutex_lock (&state->mutex);
  g_cond_signal (&state->cond);
  g_mutex_unlock (&state->mutex);

  /* At this point, it is likely that we will get the first
   * thread to block on state->wr.mutex, but since we don't have
   * a condition to be signaled by, we just have to ... um, wait
   * and hope our CPU isn't running on hamster wheels.
   */
  while (DEX_OBJECT (state->to)->ref_count > 0)
    g_usleep (G_USEC_PER_SEC / 1000);

  g_assert_cmpint (finalize_count, ==, 0);

  /* We still have state->wr.mutex, so pretend we've extended
   * liveness while it's attempting to finalize.
   */
  g_atomic_int_inc (&DEX_OBJECT (state->to)->weak_refs_watermark);
  atomic_fetch_add_explicit (&DEX_OBJECT (state->to)->ref_count, 1, memory_order_relaxed);

  /* Now release our mutex so we can race against the
   * main thread to do the final unref.
   */
  g_mutex_unlock (&state->wr.mutex);

  dex_unref (state->to);

  return NULL;
}

static void
test_weak_ref_extend_liveness (void)
{
  TestDexWeakRefExtendLiveness state;
  GThread *thread;

  finalize_count = 0;

  state.to = test_object_new ();
  dex_weak_ref_init (&state.wr, state.to);
  g_mutex_init (&state.mutex);
  g_cond_init (&state.cond);

  g_mutex_lock (&state.mutex);

  thread = g_thread_new ("test_weak_ref_extend_liveness_worker",
                         test_weak_ref_extend_liveness_worker,
                         &state);

  g_cond_wait (&state.cond, &state.mutex);
  dex_unref (state.to);

  g_mutex_unlock (&state.mutex);

  g_thread_join (thread);

  state.to = NULL;
  dex_weak_ref_clear (&state.wr);
  g_mutex_clear (&state.mutex);
  g_cond_clear (&state.cond);

  g_assert_cmpint (finalize_count, ==, 1);
}

typedef struct
{
  TestObject *to;
  DexWeakRef wr;
  GMutex mutex;
  GCond cond;
} TestDexWeakRefImmortal;

static gpointer
test_weak_ref_immortal_worker (gpointer data)
{
  TestDexWeakRefImmortal *state = data;
  DexObject *borrowed;
  TestObject *to;

  borrowed = DEX_OBJECT (state->to);

  g_mutex_lock (&state->mutex);

  /* Acquire weak ref lock to force main thread to block */
  dex_object_lock (borrowed);

  /* Now signal other thread to continue */
  g_cond_signal (&state->cond);
  g_mutex_unlock (&state->mutex);

  /* Now wait until the ref count reaches zero, then bump the watermark
   * by creating a new reference.
   */
  while (g_atomic_int_get (&borrowed->ref_count) > 0)
    g_usleep (G_USEC_PER_SEC / 1000);

  to = dex_weak_ref_get (&state->wr);
  g_assert_nonnull (to);

  /* UB here really, since it's overflow */
  g_assert_cmpint (DEX_OBJECT (to)->weak_refs_watermark, ==, 0);

  /* Now release the lock allowing the main thread to continue */
  dex_object_unlock (borrowed);

  g_mutex_lock (&state->mutex);
  dex_unref (to);
  g_mutex_unlock (&state->mutex);

  dex_weak_ref_clear (&state->wr);

  return NULL;
}

static void
test_weak_ref_immortal (void)
{
  TestDexWeakRefImmortal state;
  DexObject *borrowed;
  GThread *thread;

  finalize_count = 0;

  g_mutex_init (&state.mutex);
  g_cond_init (&state.cond);

  state.to = test_object_new ();
  borrowed = DEX_OBJECT (state.to);
  dex_weak_ref_init (&state.wr, state.to);

  /* Make object immortal */
  borrowed->weak_refs_watermark = G_MAXUINT32;

  /* Now force a race on liveness from thread */
  g_mutex_lock (&state.mutex);
  thread = g_thread_new ("test_weak_ref_immortal", test_weak_ref_immortal_worker, &state);
  g_cond_wait (&state.cond, &state.mutex);

  /* Try to unref forcing the race (blocking while acquiring the weak ref
   * mutex until the thread allows us to continue).
   */
  dex_unref (state.to);
  g_assert_cmpint (finalize_count, ==, 0);

  /* Now release our lock allowing the thread to cleanup */
  g_mutex_unlock (&state.mutex);

  g_thread_join (thread);

  g_mutex_clear (&state.mutex);
  g_cond_clear (&state.cond);

  /* Clear watermark and unref so we are valgrind clean. Consumers
   * can't actually do this, but that is the point.
   */
  g_assert_cmpint (finalize_count, ==, 0);
  g_assert_cmpint (borrowed->ref_count, ==, 1);
  g_assert_cmpint (borrowed->weak_refs_watermark, ==, 1);
  borrowed->weak_refs_watermark = 1;
  borrowed->ref_count = 1;
  dex_unref (state.to);
  g_assert_cmpint (finalize_count, ==, 1);
}

static gboolean test_weak_ref_thread_guantlet_waiting;

static gpointer
test_weak_ref_thread_guantlet_worker (gpointer data)
{
  DexWeakRef *wr = data;
  TestObject *to;
  guint i = 0;

  while ((to = dex_weak_ref_get (wr)))
    {
      dex_unref (to);
      g_usleep (G_USEC_PER_SEC / 10000 * g_random_int_range (0, 100));

      /* It is possible, given a devilish enough of a thread scheduler, that
       * this could infinitely cause us to run due to always having a ref
       * greater than zero in one thread.
       *
       * So we read an atomic every now and again to break out.
       */
      i++;
      if ((i % 10) == 0 && g_atomic_int_get (&test_weak_ref_thread_guantlet_waiting))
        break;
    }

  dex_weak_ref_clear (wr);
  g_free (wr);

  return NULL;
}

static void
test_weak_ref_thread_guantlet (void)
{
  TestObject *to = test_object_new ();
  GThread *threads[8];

  finalize_count = 0;

  for (guint i = 0; i < G_N_ELEMENTS (threads); i++)
    {
      DexWeakRef *wr = g_new0 (DexWeakRef, 1);
      g_autofree char *thread_name = g_strdup_printf ("test_weak_ref_thread_guantlet:%d", i);

      dex_weak_ref_init (wr, to);
      threads[i] = g_thread_new (thread_name, test_weak_ref_thread_guantlet_worker, wr);
    }

  g_assert_cmpint (g_atomic_int_get (&finalize_count), ==, 0);

  g_usleep (G_USEC_PER_SEC / 3);
  g_assert_cmpint (g_atomic_int_get (&finalize_count), ==, 0);

  dex_unref (to);
  g_atomic_int_set (&test_weak_ref_thread_guantlet_waiting, TRUE);

  for (guint i = 0; i < G_N_ELEMENTS (threads); i++)
    g_thread_join (threads[i]);

  g_assert_cmpint (finalize_count, ==, 1);
}

static void
test_object_basic (void)
{
  g_assert_true (dex_object_get_type() != G_TYPE_INVALID);
  g_assert_true (G_TYPE_IS_INSTANTIATABLE (dex_object_get_type()));
  g_assert_true (G_TYPE_IS_ABSTRACT (dex_object_get_type()));
}

int
main (int   argc,
      char *argv[])
{
  dex_init ();
  g_test_init (&argc, &argv, NULL);
  g_test_add_func ("/Dex/TestSuite/Object/basic", test_object_basic);
  g_test_add_func ("/Dex/TestSuite/WeakRef/single-threaded", test_weak_ref_st);
  g_test_add_func ("/Dex/TestSuite/WeakRef/multi-threaded", test_weak_ref_mt);
  g_test_add_func ("/Dex/TestSuite/WeakRef/extend-liveness", test_weak_ref_extend_liveness);
  g_test_add_func ("/Dex/TestSuite/WeakRef/immortal", test_weak_ref_immortal);
  g_test_add_func ("/Dex/TestSuite/WeakRef/thread-guantlet", test_weak_ref_thread_guantlet);
  return g_test_run ();
}