| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 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); |
| 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 |
#define TEST_TYPE_PROPERTY_OBJECT (test_property_object_get_type()) |
| 80 |
#define TEST_PROPERTY_OBJECT(obj) \ |
| 81 |
(G_TYPE_CHECK_INSTANCE_CAST(obj, TEST_TYPE_PROPERTY_OBJECT, TestPropertyObject)) |
| 82 |
|
| 83 |
GType test_property_object_get_type (void); |
| 84 |
|
| 85 |
typedef struct _TestPropertyObject |
| 86 |
{ |
| 87 |
GObject parent_instance; |
| 88 |
DexFuture *future; |
| 89 |
} TestPropertyObject; |
| 90 |
|
| 91 |
typedef struct _TestPropertyObjectClass |
| 92 |
{ |
| 93 |
GObjectClass parent_class; |
| 94 |
} TestPropertyObjectClass; |
| 95 |
|
| 96 |
enum { |
| 97 |
PROP_0, |
| 98 |
PROP_FUTURE, |
| 99 |
N_PROPS |
| 100 |
}; |
| 101 |
|
| 102 |
G_DEFINE_TYPE (TestPropertyObject, test_property_object, G_TYPE_OBJECT) |
| 103 |
|
| 104 |
static GParamSpec *test_property_object_props[N_PROPS]; |
| 105 |
|
| 106 |
static void |
| 107 |
test_property_object_finalize (GObject *object) |
| 108 |
{ |
| 109 |
TestPropertyObject *self = TEST_PROPERTY_OBJECT (object); |
| 110 |
|
| 111 |
dex_clear (&self->future); |
| 112 |
|
| 113 |
G_OBJECT_CLASS (test_property_object_parent_class)->finalize (object); |
| 114 |
} |
| 115 |
|
| 116 |
static void |
| 117 |
test_property_object_get_property (GObject *object, |
| 118 |
guint prop_id, |
| 119 |
GValue *value, |
| 120 |
GParamSpec *pspec) |
| 121 |
{ |
| 122 |
TestPropertyObject *self = TEST_PROPERTY_OBJECT (object); |
| 123 |
|
| 124 |
switch (prop_id) |
| 125 |
{ |
| 126 |
case PROP_FUTURE: |
| 127 |
dex_value_set_object (value, DEX_OBJECT (self->future)); |
| 128 |
break; |
| 129 |
|
| 130 |
default: |
| 131 |
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); |
| 132 |
} |
| 133 |
} |
| 134 |
|
| 135 |
static void |
| 136 |
test_property_object_set_property (GObject *object, |
| 137 |
guint prop_id, |
| 138 |
const GValue *value, |
| 139 |
GParamSpec *pspec) |
| 140 |
{ |
| 141 |
TestPropertyObject *self = TEST_PROPERTY_OBJECT (object); |
| 142 |
|
| 143 |
switch (prop_id) |
| 144 |
{ |
| 145 |
case PROP_FUTURE: |
| 146 |
dex_clear (&self->future); |
| 147 |
self->future = DEX_FUTURE (dex_value_dup_object (value)); |
| 148 |
break; |
| 149 |
|
| 150 |
default: |
| 151 |
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); |
| 152 |
} |
| 153 |
} |
| 154 |
|
| 155 |
static void |
| 156 |
test_property_object_class_init (TestPropertyObjectClass *klass) |
| 157 |
{ |
| 158 |
GObjectClass *object_class = G_OBJECT_CLASS (klass); |
| 159 |
|
| 160 |
object_class->finalize = test_property_object_finalize; |
| 161 |
object_class->get_property = test_property_object_get_property; |
| 162 |
object_class->set_property = test_property_object_set_property; |
| 163 |
|
| 164 |
test_property_object_props[PROP_FUTURE] = |
| 165 |
dex_param_spec_object ("future", NULL, NULL, |
| 166 |
DEX_TYPE_FUTURE, |
| 167 |
(G_PARAM_READWRITE | |
| 168 |
G_PARAM_STATIC_STRINGS)); |
| 169 |
|
| 170 |
g_object_class_install_properties (object_class, N_PROPS, test_property_object_props); |
| 171 |
} |
| 172 |
|
| 173 |
static void |
| 174 |
test_property_object_init (TestPropertyObject *self) |
| 175 |
{ |
| 176 |
} |
| 177 |
|
| 178 |
static guint |
| 179 |
test_object_count_weak_refs (TestObject *to) |
| 180 |
{ |
| 181 |
DexObject *object = DEX_OBJECT (to); |
| 182 |
guint count = 0; |
| 183 |
|
| 184 |
dex_object_lock (object); |
| 185 |
for (DexWeakRef *wr = object->weak_refs; wr != NULL; wr = wr->next) |
| 186 |
count++; |
| 187 |
dex_object_unlock (object); |
| 188 |
|
| 189 |
return count; |
| 190 |
} |
| 191 |
|
| 192 |
static void |
| 193 |
test_weak_ref_st (void) |
| 194 |
{ |
| 195 |
TestObject *to, *to2; |
| 196 |
DexWeakRef wr; |
| 197 |
DexWeakRef wr2; |
| 198 |
DexWeakRef wr3; |
| 199 |
|
| 200 |
finalize_count = 0; |
| 201 |
|
| 202 |
dex_weak_ref_init (&wr, NULL); |
| 203 |
g_assert_null (dex_weak_ref_get (&wr)); |
| 204 |
|
| 205 |
to = test_object_new (); |
| 206 |
dex_weak_ref_set (&wr, to); |
| 207 |
|
| 208 |
dex_ref (to); |
| 209 |
dex_unref (to); |
| 210 |
|
| 211 |
|
| 212 |
dex_weak_ref_init (&wr2, to); |
| 213 |
dex_weak_ref_set (&wr2, to); |
| 214 |
|
| 215 |
|
| 216 |
dex_weak_ref_init (&wr3, NULL); |
| 217 |
dex_weak_ref_set (&wr3, to); |
| 218 |
dex_weak_ref_set (&wr3, NULL); |
| 219 |
dex_weak_ref_set (&wr3, to); |
| 220 |
|
| 221 |
dex_weak_ref_clear (&wr2); |
| 222 |
dex_weak_ref_clear (&wr3); |
| 223 |
|
| 224 |
to2 = dex_weak_ref_get (&wr); |
| 225 |
g_assert_true (to == to2); |
| 226 |
dex_clear (&to2); |
| 227 |
dex_clear (&to); |
| 228 |
g_assert_cmpint (finalize_count, ==, 1); |
| 229 |
g_assert_null (dex_weak_ref_get (&wr)); |
| 230 |
|
| 231 |
dex_weak_ref_clear (&wr); |
| 232 |
} |
| 233 |
|
| 234 |
static void |
| 235 |
test_weak_ref_retarget (void) |
| 236 |
{ |
| 237 |
TestObject *to1; |
| 238 |
TestObject *to2; |
| 239 |
TestObject *borrowed; |
| 240 |
DexWeakRef wr; |
| 241 |
|
| 242 |
finalize_count = 0; |
| 243 |
|
| 244 |
to1 = test_object_new (); |
| 245 |
to2 = test_object_new (); |
| 246 |
|
| 247 |
dex_weak_ref_init (&wr, to1); |
| 248 |
|
| 249 |
g_assert_cmpuint (test_object_count_weak_refs (to1), ==, 1); |
| 250 |
g_assert_cmpuint (test_object_count_weak_refs (to2), ==, 0); |
| 251 |
|
| 252 |
dex_weak_ref_set (&wr, to1); |
| 253 |
g_assert_cmpuint (test_object_count_weak_refs (to1), ==, 1); |
| 254 |
g_assert_cmpuint (test_object_count_weak_refs (to2), ==, 0); |
| 255 |
|
| 256 |
dex_weak_ref_set (&wr, to2); |
| 257 |
g_assert_cmpuint (test_object_count_weak_refs (to1), ==, 0); |
| 258 |
g_assert_cmpuint (test_object_count_weak_refs (to2), ==, 1); |
| 259 |
|
| 260 |
dex_unref (to1); |
| 261 |
g_assert_cmpint (finalize_count, ==, 1); |
| 262 |
|
| 263 |
borrowed = dex_weak_ref_get (&wr); |
| 264 |
g_assert_true (borrowed == to2); |
| 265 |
dex_unref (borrowed); |
| 266 |
|
| 267 |
dex_weak_ref_clear (&wr); |
| 268 |
g_assert_cmpuint (test_object_count_weak_refs (to2), ==, 0); |
| 269 |
|
| 270 |
dex_unref (to2); |
| 271 |
g_assert_cmpint (finalize_count, ==, 2); |
| 272 |
} |
| 273 |
|
| 274 |
typedef struct |
| 275 |
{ |
| 276 |
TestObject *to; |
| 277 |
DexWeakRef wr; |
| 278 |
GMutex wait1; |
| 279 |
GMutex wait2; |
| 280 |
GCond cond1; |
| 281 |
GCond cond2; |
| 282 |
} TestDexWeakRefMt; |
| 283 |
|
| 284 |
static gpointer |
| 285 |
test_weak_ref_mt_worker (gpointer data) |
| 286 |
{ |
| 287 |
TestDexWeakRefMt *state = data; |
| 288 |
TestObject *to; |
| 289 |
|
| 290 |
g_assert_nonnull (state); |
| 291 |
g_assert_nonnull (state->to); |
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
g_mutex_lock (&state->wait1); |
| 297 |
to = dex_weak_ref_get (&state->wr); |
| 298 |
g_assert_nonnull (to); |
| 299 |
g_clear_pointer (&to, dex_unref); |
| 300 |
g_cond_signal (&state->cond1); |
| 301 |
g_mutex_unlock (&state->wait1); |
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
g_mutex_lock (&state->wait2); |
| 310 |
to = dex_weak_ref_get (&state->wr); |
| 311 |
g_assert_null (to); |
| 312 |
g_cond_signal (&state->cond2); |
| 313 |
g_mutex_unlock (&state->wait2); |
| 314 |
|
| 315 |
return NULL; |
| 316 |
} |
| 317 |
|
| 318 |
static void |
| 319 |
test_weak_ref_mt (void) |
| 320 |
{ |
| 321 |
TestDexWeakRefMt state; |
| 322 |
GThread *thread; |
| 323 |
|
| 324 |
finalize_count = 0; |
| 325 |
|
| 326 |
state.to = test_object_new (); |
| 327 |
dex_weak_ref_init (&state.wr, state.to); |
| 328 |
|
| 329 |
g_mutex_init (&state.wait1); |
| 330 |
g_mutex_init (&state.wait2); |
| 331 |
|
| 332 |
g_cond_init (&state.cond1); |
| 333 |
g_cond_init (&state.cond2); |
| 334 |
|
| 335 |
g_mutex_lock (&state.wait1); |
| 336 |
g_mutex_lock (&state.wait2); |
| 337 |
|
| 338 |
thread = g_thread_new ("test_weak_ref_mt_worker", test_weak_ref_mt_worker, &state); |
| 339 |
|
| 340 |
|
| 341 |
g_cond_wait (&state.cond1, &state.wait1); |
| 342 |
|
| 343 |
|
| 344 |
|
| 345 |
|
| 346 |
g_clear_pointer (&state.to, dex_unref); |
| 347 |
g_cond_wait (&state.cond2, &state.wait2); |
| 348 |
|
| 349 |
g_mutex_unlock (&state.wait1); |
| 350 |
g_mutex_unlock (&state.wait2); |
| 351 |
|
| 352 |
g_thread_join (thread); |
| 353 |
|
| 354 |
dex_weak_ref_clear (&state.wr); |
| 355 |
|
| 356 |
g_mutex_clear (&state.wait1); |
| 357 |
g_mutex_clear (&state.wait2); |
| 358 |
|
| 359 |
g_cond_clear (&state.cond1); |
| 360 |
g_cond_clear (&state.cond2); |
| 361 |
} |
| 362 |
|
| 363 |
typedef struct |
| 364 |
{ |
| 365 |
TestObject *to; |
| 366 |
DexWeakRef wr; |
| 367 |
GMutex mutex; |
| 368 |
GCond cond; |
| 369 |
} TestDexWeakRefExtendLiveness; |
| 370 |
|
| 371 |
static gpointer |
| 372 |
test_weak_ref_extend_liveness_worker (gpointer data) |
| 373 |
{ |
| 374 |
TestDexWeakRefExtendLiveness *state = data; |
| 375 |
|
| 376 |
|
| 377 |
|
| 378 |
|
| 379 |
|
| 380 |
g_mutex_lock (&state->wr.mutex); |
| 381 |
|
| 382 |
g_assert (state->to != NULL); |
| 383 |
|
| 384 |
|
| 385 |
|
| 386 |
|
| 387 |
g_mutex_lock (&state->mutex); |
| 388 |
g_cond_signal (&state->cond); |
| 389 |
g_mutex_unlock (&state->mutex); |
| 390 |
|
| 391 |
|
| 392 |
|
| 393 |
|
| 394 |
|
| 395 |
|
| 396 |
while (DEX_OBJECT (state->to)->ref_count > 0) |
| 397 |
g_usleep (G_USEC_PER_SEC / 1000); |
| 398 |
|
| 399 |
g_assert_cmpint (finalize_count, ==, 0); |
| 400 |
|
| 401 |
|
| 402 |
|
| 403 |
|
| 404 |
atomic_fetch_add_explicit (&DEX_OBJECT (state->to)->weak_refs_watermark, |
| 405 |
1, |
| 406 |
memory_order_acq_rel); |
| 407 |
atomic_fetch_add_explicit (&DEX_OBJECT (state->to)->ref_count, 1, memory_order_relaxed); |
| 408 |
|
| 409 |
|
| 410 |
|
| 411 |
|
| 412 |
g_mutex_unlock (&state->wr.mutex); |
| 413 |
|
| 414 |
dex_unref (state->to); |
| 415 |
|
| 416 |
return NULL; |
| 417 |
} |
| 418 |
|
| 419 |
static void |
| 420 |
test_weak_ref_extend_liveness (void) |
| 421 |
{ |
| 422 |
TestDexWeakRefExtendLiveness state; |
| 423 |
GThread *thread; |
| 424 |
|
| 425 |
finalize_count = 0; |
| 426 |
|
| 427 |
state.to = test_object_new (); |
| 428 |
dex_weak_ref_init (&state.wr, state.to); |
| 429 |
g_mutex_init (&state.mutex); |
| 430 |
g_cond_init (&state.cond); |
| 431 |
|
| 432 |
g_mutex_lock (&state.mutex); |
| 433 |
|
| 434 |
thread = g_thread_new ("test_weak_ref_extend_liveness_worker", |
| 435 |
test_weak_ref_extend_liveness_worker, |
| 436 |
&state); |
| 437 |
|
| 438 |
g_cond_wait (&state.cond, &state.mutex); |
| 439 |
dex_unref (state.to); |
| 440 |
|
| 441 |
g_mutex_unlock (&state.mutex); |
| 442 |
|
| 443 |
g_thread_join (thread); |
| 444 |
|
| 445 |
state.to = NULL; |
| 446 |
dex_weak_ref_clear (&state.wr); |
| 447 |
g_mutex_clear (&state.mutex); |
| 448 |
g_cond_clear (&state.cond); |
| 449 |
|
| 450 |
g_assert_cmpint (finalize_count, ==, 1); |
| 451 |
} |
| 452 |
|
| 453 |
typedef struct |
| 454 |
{ |
| 455 |
DexWeakRef *wr; |
| 456 |
TestObject *replacement; |
| 457 |
} TestDexWeakRefRetargetLockOrder; |
| 458 |
|
| 459 |
static gpointer |
| 460 |
test_weak_ref_retarget_lock_order_worker (gpointer data) |
| 461 |
{ |
| 462 |
TestDexWeakRefRetargetLockOrder *state = data; |
| 463 |
|
| 464 |
dex_weak_ref_set (state->wr, state->replacement); |
| 465 |
|
| 466 |
return NULL; |
| 467 |
} |
| 468 |
|
| 469 |
static void |
| 470 |
test_weak_ref_retarget_lock_order (void) |
| 471 |
{ |
| 472 |
TestDexWeakRefRetargetLockOrder state; |
| 473 |
TestObject *old_object; |
| 474 |
TestObject *new_object; |
| 475 |
DexObject *old_dex_object; |
| 476 |
DexWeakRef wr; |
| 477 |
GThread *thread; |
| 478 |
gint64 deadline; |
| 479 |
|
| 480 |
finalize_count = 0; |
| 481 |
|
| 482 |
old_object = test_object_new (); |
| 483 |
new_object = test_object_new (); |
| 484 |
old_dex_object = DEX_OBJECT (old_object); |
| 485 |
|
| 486 |
dex_weak_ref_init (&wr, old_object); |
| 487 |
|
| 488 |
state.wr = ≀ |
| 489 |
state.replacement = new_object; |
| 490 |
|
| 491 |
dex_object_lock (old_dex_object); |
| 492 |
thread = g_thread_new ("test_weak_ref_retarget_lock_order", |
| 493 |
test_weak_ref_retarget_lock_order_worker, |
| 494 |
&state); |
| 495 |
|
| 496 |
deadline = g_get_monotonic_time () + G_USEC_PER_SEC; |
| 497 |
while (atomic_load_explicit (&old_dex_object->ref_count, memory_order_acquire) == 1) |
| 498 |
{ |
| 499 |
g_assert_cmpint (g_get_monotonic_time (), <, deadline); |
| 500 |
g_usleep (G_USEC_PER_SEC / 1000); |
| 501 |
} |
| 502 |
|
| 503 |
g_assert_true (g_mutex_trylock (&wr.mutex)); |
| 504 |
g_mutex_unlock (&wr.mutex); |
| 505 |
|
| 506 |
dex_object_unlock (old_dex_object); |
| 507 |
g_thread_join (thread); |
| 508 |
|
| 509 |
g_assert_cmpuint (test_object_count_weak_refs (old_object), ==, 0); |
| 510 |
g_assert_cmpuint (test_object_count_weak_refs (new_object), ==, 1); |
| 511 |
|
| 512 |
dex_weak_ref_clear (&wr); |
| 513 |
dex_unref (old_object); |
| 514 |
dex_unref (new_object); |
| 515 |
|
| 516 |
g_assert_cmpint (finalize_count, ==, 2); |
| 517 |
} |
| 518 |
|
| 519 |
typedef struct |
| 520 |
{ |
| 521 |
TestObject *to; |
| 522 |
DexWeakRef wr; |
| 523 |
GMutex mutex; |
| 524 |
GCond cond; |
| 525 |
} TestDexWeakRefImmortal; |
| 526 |
|
| 527 |
static gpointer |
| 528 |
test_weak_ref_immortal_worker (gpointer data) |
| 529 |
{ |
| 530 |
TestDexWeakRefImmortal *state = data; |
| 531 |
DexObject *borrowed; |
| 532 |
TestObject *to; |
| 533 |
|
| 534 |
borrowed = DEX_OBJECT (state->to); |
| 535 |
|
| 536 |
g_mutex_lock (&state->mutex); |
| 537 |
|
| 538 |
|
| 539 |
dex_object_lock (borrowed); |
| 540 |
|
| 541 |
|
| 542 |
g_cond_signal (&state->cond); |
| 543 |
g_mutex_unlock (&state->mutex); |
| 544 |
|
| 545 |
|
| 546 |
|
| 547 |
|
| 548 |
while (g_atomic_int_get (&borrowed->ref_count) > 0) |
| 549 |
g_usleep (G_USEC_PER_SEC / 1000); |
| 550 |
|
| 551 |
to = dex_weak_ref_get (&state->wr); |
| 552 |
g_assert_nonnull (to); |
| 553 |
|
| 554 |
|
| 555 |
g_assert_cmpint (DEX_OBJECT (to)->weak_refs_watermark, ==, 0); |
| 556 |
|
| 557 |
|
| 558 |
dex_object_unlock (borrowed); |
| 559 |
|
| 560 |
g_mutex_lock (&state->mutex); |
| 561 |
dex_unref (to); |
| 562 |
g_mutex_unlock (&state->mutex); |
| 563 |
|
| 564 |
dex_weak_ref_clear (&state->wr); |
| 565 |
|
| 566 |
return NULL; |
| 567 |
} |
| 568 |
|
| 569 |
static void |
| 570 |
test_weak_ref_immortal (void) |
| 571 |
{ |
| 572 |
TestDexWeakRefImmortal state; |
| 573 |
DexObject *borrowed; |
| 574 |
GThread *thread; |
| 575 |
|
| 576 |
finalize_count = 0; |
| 577 |
|
| 578 |
g_mutex_init (&state.mutex); |
| 579 |
g_cond_init (&state.cond); |
| 580 |
|
| 581 |
state.to = test_object_new (); |
| 582 |
borrowed = DEX_OBJECT (state.to); |
| 583 |
dex_weak_ref_init (&state.wr, state.to); |
| 584 |
|
| 585 |
|
| 586 |
borrowed->weak_refs_watermark = G_MAXUINT32; |
| 587 |
|
| 588 |
|
| 589 |
g_mutex_lock (&state.mutex); |
| 590 |
thread = g_thread_new ("test_weak_ref_immortal", test_weak_ref_immortal_worker, &state); |
| 591 |
g_cond_wait (&state.cond, &state.mutex); |
| 592 |
|
| 593 |
|
| 594 |
|
| 595 |
|
| 596 |
dex_unref (state.to); |
| 597 |
g_assert_cmpint (finalize_count, ==, 0); |
| 598 |
|
| 599 |
|
| 600 |
g_mutex_unlock (&state.mutex); |
| 601 |
|
| 602 |
g_thread_join (thread); |
| 603 |
|
| 604 |
g_mutex_clear (&state.mutex); |
| 605 |
g_cond_clear (&state.cond); |
| 606 |
|
| 607 |
|
| 608 |
|
| 609 |
|
| 610 |
g_assert_cmpint (finalize_count, ==, 0); |
| 611 |
g_assert_cmpint (borrowed->ref_count, ==, 1); |
| 612 |
g_assert_cmpint (borrowed->weak_refs_watermark, ==, 1); |
| 613 |
borrowed->weak_refs_watermark = 1; |
| 614 |
borrowed->ref_count = 1; |
| 615 |
dex_unref (state.to); |
| 616 |
g_assert_cmpint (finalize_count, ==, 1); |
| 617 |
} |
| 618 |
|
| 619 |
static gboolean test_weak_ref_thread_guantlet_waiting; |
| 620 |
|
| 621 |
static gpointer |
| 622 |
test_weak_ref_thread_guantlet_worker (gpointer data) |
| 623 |
{ |
| 624 |
DexWeakRef *wr = data; |
| 625 |
TestObject *to; |
| 626 |
guint i = 0; |
| 627 |
|
| 628 |
while ((to = dex_weak_ref_get (wr))) |
| 629 |
{ |
| 630 |
dex_unref (to); |
| 631 |
g_usleep (G_USEC_PER_SEC / 10000 * g_random_int_range (0, 100)); |
| 632 |
|
| 633 |
|
| 634 |
|
| 635 |
|
| 636 |
|
| 637 |
|
| 638 |
|
| 639 |
i++; |
| 640 |
if ((i % 10) == 0 && g_atomic_int_get (&test_weak_ref_thread_guantlet_waiting)) |
| 641 |
break; |
| 642 |
} |
| 643 |
|
| 644 |
dex_weak_ref_clear (wr); |
| 645 |
g_free (wr); |
| 646 |
|
| 647 |
return NULL; |
| 648 |
} |
| 649 |
|
| 650 |
static void |
| 651 |
test_weak_ref_thread_guantlet (void) |
| 652 |
{ |
| 653 |
TestObject *to = test_object_new (); |
| 654 |
GThread *threads[8]; |
| 655 |
|
| 656 |
finalize_count = 0; |
| 657 |
|
| 658 |
for (guint i = 0; i < G_N_ELEMENTS (threads); i++) |
| 659 |
{ |
| 660 |
DexWeakRef *wr = g_new0 (DexWeakRef, 1); |
| 661 |
char *thread_name = g_strdup_printf ("test_weak_ref_thread_guantlet:%d", i); |
| 662 |
|
| 663 |
dex_weak_ref_init (wr, to); |
| 664 |
threads[i] = g_thread_new (thread_name, test_weak_ref_thread_guantlet_worker, wr); |
| 665 |
g_free (thread_name); |
| 666 |
} |
| 667 |
|
| 668 |
g_assert_cmpint (g_atomic_int_get (&finalize_count), ==, 0); |
| 669 |
|
| 670 |
g_usleep (G_USEC_PER_SEC / 3); |
| 671 |
g_assert_cmpint (g_atomic_int_get (&finalize_count), ==, 0); |
| 672 |
|
| 673 |
dex_unref (to); |
| 674 |
g_atomic_int_set (&test_weak_ref_thread_guantlet_waiting, TRUE); |
| 675 |
|
| 676 |
for (guint i = 0; i < G_N_ELEMENTS (threads); i++) |
| 677 |
g_thread_join (threads[i]); |
| 678 |
|
| 679 |
g_assert_cmpint (finalize_count, ==, 1); |
| 680 |
} |
| 681 |
|
| 682 |
static void |
| 683 |
test_object_basic (void) |
| 684 |
{ |
| 685 |
g_assert_true (dex_object_get_type() != G_TYPE_INVALID); |
| 686 |
g_assert_true (G_TYPE_IS_INSTANTIATABLE (dex_object_get_type())); |
| 687 |
g_assert_true (G_TYPE_IS_ABSTRACT (dex_object_get_type())); |
| 688 |
} |
| 689 |
|
| 690 |
static void |
| 691 |
test_object_set_object (void) |
| 692 |
{ |
| 693 |
TestObject *object1; |
| 694 |
TestObject *object2; |
| 695 |
TestObject *held = NULL; |
| 696 |
|
| 697 |
finalize_count = 0; |
| 698 |
|
| 699 |
g_assert_false (dex_set_object (&held, NULL)); |
| 700 |
|
| 701 |
object1 = test_object_new (); |
| 702 |
object2 = test_object_new (); |
| 703 |
|
| 704 |
g_assert_true (dex_set_object (&held, object1)); |
| 705 |
g_assert_true (held == object1); |
| 706 |
g_assert_cmpint (DEX_OBJECT (object1)->ref_count, ==, 2); |
| 707 |
|
| 708 |
g_assert_false (dex_set_object (&held, object1)); |
| 709 |
g_assert_true (held == object1); |
| 710 |
g_assert_cmpint (DEX_OBJECT (object1)->ref_count, ==, 2); |
| 711 |
|
| 712 |
g_assert_true (dex_set_object (&held, object2)); |
| 713 |
g_assert_true (held == object2); |
| 714 |
g_assert_cmpint (DEX_OBJECT (object1)->ref_count, ==, 1); |
| 715 |
g_assert_cmpint (DEX_OBJECT (object2)->ref_count, ==, 2); |
| 716 |
|
| 717 |
g_assert_true (dex_set_object (&held, NULL)); |
| 718 |
g_assert_null (held); |
| 719 |
g_assert_cmpint (DEX_OBJECT (object2)->ref_count, ==, 1); |
| 720 |
|
| 721 |
dex_unref (object1); |
| 722 |
dex_unref (object2); |
| 723 |
|
| 724 |
g_assert_cmpint (finalize_count, ==, 2); |
| 725 |
} |
| 726 |
|
| 727 |
static void |
| 728 |
test_object_property (void) |
| 729 |
{ |
| 730 |
GObject * object = NULL; |
| 731 |
DexPromise * promise = NULL; |
| 732 |
DexFuture * borrowed = NULL; |
| 733 |
GParamSpec *pspec; |
| 734 |
GValue value = G_VALUE_INIT; |
| 735 |
|
| 736 |
object = g_object_new (TEST_TYPE_PROPERTY_OBJECT, NULL); |
| 737 |
promise = dex_promise_new (); |
| 738 |
pspec = test_property_object_props[PROP_FUTURE]; |
| 739 |
|
| 740 |
g_assert_cmpuint (G_PARAM_SPEC_VALUE_TYPE (pspec), ==, DEX_TYPE_FUTURE); |
| 741 |
|
| 742 |
g_value_init (&value, DEX_TYPE_FUTURE); |
| 743 |
dex_value_set_object (&value, DEX_OBJECT (promise)); |
| 744 |
|
| 745 |
g_assert_true (g_param_value_is_valid (pspec, &value)); |
| 746 |
|
| 747 |
g_object_set_property (object, "future", &value); |
| 748 |
g_value_unset (&value); |
| 749 |
|
| 750 |
g_value_init (&value, DEX_TYPE_FUTURE); |
| 751 |
g_object_get_property (object, "future", &value); |
| 752 |
|
| 753 |
borrowed = DEX_FUTURE (dex_value_dup_object (&value)); |
| 754 |
g_assert_true (borrowed == DEX_FUTURE (promise)); |
| 755 |
|
| 756 |
g_value_unset (&value); |
| 757 |
dex_clear (&borrowed); |
| 758 |
dex_clear (&promise); |
| 759 |
g_clear_object (&object); |
| 760 |
} |
| 761 |
|
| 762 |
int |
| 763 |
main (int argc, |
| 764 |
char *argv[]) |
| 765 |
{ |
| 766 |
dex_init (); |
| 767 |
g_test_init (&argc, &argv, NULL); |
| 768 |
g_test_add_func ("/Dex/TestSuite/Object/basic", test_object_basic); |
| 769 |
g_test_add_func ("/Dex/TestSuite/Object/set-object", test_object_set_object); |
| 770 |
g_test_add_func ("/Dex/TestSuite/Object/property", test_object_property); |
| 771 |
g_test_add_func ("/Dex/TestSuite/WeakRef/single-threaded", test_weak_ref_st); |
| 772 |
g_test_add_func ("/Dex/TestSuite/WeakRef/retarget", test_weak_ref_retarget); |
| 773 |
g_test_add_func ("/Dex/TestSuite/WeakRef/multi-threaded", test_weak_ref_mt); |
| 774 |
g_test_add_func ("/Dex/TestSuite/WeakRef/extend-liveness", test_weak_ref_extend_liveness); |
| 775 |
g_test_add_func ("/Dex/TestSuite/WeakRef/retarget-lock-order", |
| 776 |
test_weak_ref_retarget_lock_order); |
| 777 |
g_test_add_func ("/Dex/TestSuite/WeakRef/immortal", test_weak_ref_immortal); |
| 778 |
g_test_add_func ("/Dex/TestSuite/WeakRef/thread-guantlet", test_weak_ref_thread_guantlet); |
| 779 |
return g_test_run (); |
| 780 |
} |