OLD | NEW |
1 /* | 1 /* |
2 * Copyright (C) 2013 Google Inc. All rights reserved. | 2 * Copyright (C) 2013 Google Inc. All rights reserved. |
3 * | 3 * |
4 * Redistribution and use in source and binary forms, with or without | 4 * Redistribution and use in source and binary forms, with or without |
5 * modification, are permitted provided that the following conditions are | 5 * modification, are permitted provided that the following conditions are |
6 * met: | 6 * met: |
7 * | 7 * |
8 * * Redistributions of source code must retain the above copyright | 8 * * Redistributions of source code must retain the above copyright |
9 * notice, this list of conditions and the following disclaimer. | 9 * notice, this list of conditions and the following disclaimer. |
10 * * Redistributions in binary form must reproduce the above | 10 * * Redistributions in binary form must reproduce the above |
(...skipping 6343 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
6354 { | 6354 { |
6355 Persistent<IntWrapper> object = new IntWrapper(20); | 6355 Persistent<IntWrapper> object = new IntWrapper(20); |
6356 OwnPtr<WeakPersistentHolder> holder = adoptPtr(new WeakPersistentHolder(obje
ct)); | 6356 OwnPtr<WeakPersistentHolder> holder = adoptPtr(new WeakPersistentHolder(obje
ct)); |
6357 Heap::collectGarbage(ThreadState::NoHeapPointersOnStack, ThreadState::GCWith
Sweep, Heap::ForcedGC); | 6357 Heap::collectGarbage(ThreadState::NoHeapPointersOnStack, ThreadState::GCWith
Sweep, Heap::ForcedGC); |
6358 EXPECT_TRUE(holder->object()); | 6358 EXPECT_TRUE(holder->object()); |
6359 object = nullptr; | 6359 object = nullptr; |
6360 Heap::collectGarbage(ThreadState::NoHeapPointersOnStack, ThreadState::GCWith
Sweep, Heap::ForcedGC); | 6360 Heap::collectGarbage(ThreadState::NoHeapPointersOnStack, ThreadState::GCWith
Sweep, Heap::ForcedGC); |
6361 EXPECT_FALSE(holder->object()); | 6361 EXPECT_FALSE(holder->object()); |
6362 } | 6362 } |
6363 | 6363 |
| 6364 namespace { |
| 6365 |
| 6366 void workerThreadMainForCrossThreadWeakPersistentTest(DestructorLockingObject**
object) |
| 6367 { |
| 6368 // Step 2: Create an object and store the pointer. |
| 6369 MutexLocker locker(workerThreadMutex()); |
| 6370 ThreadState::attach(); |
| 6371 *object = DestructorLockingObject::create(); |
| 6372 wakeMainThread(); |
| 6373 parkWorkerThread(); |
| 6374 |
| 6375 // Step 4: Run a GC. |
| 6376 Heap::collectGarbage(ThreadState::NoHeapPointersOnStack, ThreadState::GCWith
Sweep, Heap::ForcedGC); |
| 6377 wakeMainThread(); |
| 6378 parkWorkerThread(); |
| 6379 |
| 6380 // Step 6: Finish. |
| 6381 ThreadState::detach(); |
| 6382 wakeMainThread(); |
| 6383 } |
| 6384 |
| 6385 } // anonymous namespace |
| 6386 |
| 6387 TEST(HeapTest, CrossThreadWeakPersistent) |
| 6388 { |
| 6389 // Create an object in the worker thread, have a CrossThreadWeakPersistent p
ointing to it on the main thread, |
| 6390 // clear the reference in the worker thread, run a GC in the worker thread,
and see if the |
| 6391 // CrossThreadWeakPersistent is cleared. |
| 6392 |
| 6393 DestructorLockingObject::s_destructorCalls = 0; |
| 6394 |
| 6395 // Step 1: Initiate a worker thread, and wait for |object| to get allocated
on the worker thread. |
| 6396 MutexLocker mainThreadMutexLocker(mainThreadMutex()); |
| 6397 OwnPtr<WebThread> workerThread = adoptPtr(Platform::current()->createThread(
"Test Worker Thread")); |
| 6398 DestructorLockingObject* object = nullptr; |
| 6399 workerThread->postTask(FROM_HERE, new Task(threadSafeBind(workerThreadMainFo
rCrossThreadWeakPersistentTest, AllowCrossThreadAccessWrapper<DestructorLockingO
bject**>(&object)))); |
| 6400 parkMainThread(); |
| 6401 |
| 6402 // Step 3: Set up a CrossThreadWeakPersistent. |
| 6403 ASSERT_TRUE(object); |
| 6404 CrossThreadWeakPersistent<DestructorLockingObject> crossThreadWeakPersistent
(object); |
| 6405 object = nullptr; |
| 6406 { |
| 6407 SafePointAwareMutexLocker recursiveMutexLocker(recursiveMutex()); |
| 6408 EXPECT_EQ(0, DestructorLockingObject::s_destructorCalls); |
| 6409 } |
| 6410 |
| 6411 { |
| 6412 // Pretend we have no pointers on stack during the step 4. |
| 6413 SafePointScope scope(ThreadState::NoHeapPointersOnStack); |
| 6414 wakeWorkerThread(); |
| 6415 parkMainThread(); |
| 6416 } |
| 6417 |
| 6418 // Step 5: Make sure the weak persistent is cleared. |
| 6419 EXPECT_FALSE(crossThreadWeakPersistent.get()); |
| 6420 { |
| 6421 SafePointAwareMutexLocker recursiveMutexLocker(recursiveMutex()); |
| 6422 EXPECT_EQ(1, DestructorLockingObject::s_destructorCalls); |
| 6423 } |
| 6424 |
| 6425 wakeWorkerThread(); |
| 6426 parkMainThread(); |
| 6427 } |
| 6428 |
6364 } // namespace blink | 6429 } // namespace blink |
OLD | NEW |