| 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 5317 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 5328 ThreadState::detachCurrentThread(); | 5328 ThreadState::detachCurrentThread(); |
| 5329 } | 5329 } |
| 5330 | 5330 |
| 5331 static volatile uintptr_t s_workerObjectPointer; | 5331 static volatile uintptr_t s_workerObjectPointer; |
| 5332 }; | 5332 }; |
| 5333 | 5333 |
| 5334 TEST(HeapTest, ThreadedStrongification) { | 5334 TEST(HeapTest, ThreadedStrongification) { |
| 5335 ThreadedStrongificationTester::test(); | 5335 ThreadedStrongificationTester::test(); |
| 5336 } | 5336 } |
| 5337 | 5337 |
| 5338 class MemberSameThreadCheckTester { |
| 5339 public: |
| 5340 void test() { |
| 5341 IntWrapper::s_destructorCalls = 0; |
| 5342 |
| 5343 MutexLocker locker(mainThreadMutex()); |
| 5344 std::unique_ptr<WebThread> workerThread = WTF::wrapUnique( |
| 5345 Platform::current()->createThread("Test Worker Thread")); |
| 5346 workerThread->getWebTaskRunner()->postTask( |
| 5347 BLINK_FROM_HERE, |
| 5348 crossThreadBind(&MemberSameThreadCheckTester::workerThreadMain, |
| 5349 crossThreadUnretained(this))); |
| 5350 |
| 5351 parkMainThread(); |
| 5352 } |
| 5353 |
| 5354 private: |
| 5355 Member<IntWrapper> m_wrapper; |
| 5356 |
| 5357 void workerThreadMain() { |
| 5358 MutexLocker locker(workerThreadMutex()); |
| 5359 |
| 5360 ThreadState::attachCurrentThread(); |
| 5361 |
| 5362 // Setting an object created on the worker thread to a Member allocated on |
| 5363 // the main thread is not allowed. |
| 5364 m_wrapper = IntWrapper::create(42); |
| 5365 |
| 5366 wakeMainThread(); |
| 5367 ThreadState::detachCurrentThread(); |
| 5368 } |
| 5369 }; |
| 5370 |
| 5371 #if DCHECK_IS_ON() |
| 5372 TEST(HeapTest, MemberSameThreadCheck) { |
| 5373 EXPECT_DEATH(MemberSameThreadCheckTester().test(), ""); |
| 5374 } |
| 5375 #endif |
| 5376 |
| 5377 class PersistentSameThreadCheckTester { |
| 5378 public: |
| 5379 void test() { |
| 5380 IntWrapper::s_destructorCalls = 0; |
| 5381 |
| 5382 MutexLocker locker(mainThreadMutex()); |
| 5383 std::unique_ptr<WebThread> workerThread = WTF::wrapUnique( |
| 5384 Platform::current()->createThread("Test Worker Thread")); |
| 5385 workerThread->getWebTaskRunner()->postTask( |
| 5386 BLINK_FROM_HERE, |
| 5387 crossThreadBind(&PersistentSameThreadCheckTester::workerThreadMain, |
| 5388 crossThreadUnretained(this))); |
| 5389 |
| 5390 parkMainThread(); |
| 5391 } |
| 5392 |
| 5393 private: |
| 5394 Persistent<IntWrapper> m_wrapper; |
| 5395 |
| 5396 void workerThreadMain() { |
| 5397 MutexLocker locker(workerThreadMutex()); |
| 5398 |
| 5399 ThreadState::attachCurrentThread(); |
| 5400 |
| 5401 // Setting an object created on the worker thread to a Persistent allocated |
| 5402 // on the main thread is not allowed. |
| 5403 m_wrapper = IntWrapper::create(42); |
| 5404 |
| 5405 wakeMainThread(); |
| 5406 ThreadState::detachCurrentThread(); |
| 5407 } |
| 5408 }; |
| 5409 |
| 5410 #if DCHECK_IS_ON() |
| 5411 TEST(HeapTest, PersistentSameThreadCheck) { |
| 5412 EXPECT_DEATH(PersistentSameThreadCheckTester().test(), ""); |
| 5413 } |
| 5414 #endif |
| 5415 |
| 5416 class MarkingSameThreadCheckTester { |
| 5417 public: |
| 5418 void test() { |
| 5419 IntWrapper::s_destructorCalls = 0; |
| 5420 |
| 5421 MutexLocker locker(mainThreadMutex()); |
| 5422 std::unique_ptr<WebThread> workerThread = WTF::wrapUnique( |
| 5423 Platform::current()->createThread("Test Worker Thread")); |
| 5424 Persistent<MainThreadObject> m_mainThreadObject = new MainThreadObject(); |
| 5425 workerThread->getWebTaskRunner()->postTask( |
| 5426 BLINK_FROM_HERE, |
| 5427 crossThreadBind(&MarkingSameThreadCheckTester::workerThreadMain, |
| 5428 crossThreadUnretained(this), |
| 5429 wrapCrossThreadPersistent(m_mainThreadObject.get()))); |
| 5430 parkMainThread(); |
| 5431 // This will try to mark MainThreadObject when it tries to mark IntWrapper |
| 5432 // it should crash. |
| 5433 preciselyCollectGarbage(); |
| 5434 } |
| 5435 |
| 5436 private: |
| 5437 class MainThreadObject : public GarbageCollectedFinalized<MainThreadObject> { |
| 5438 public: |
| 5439 DEFINE_INLINE_TRACE() { visitor->trace(m_wrapperSet); } |
| 5440 void addToSet(IntWrapper* wrapper) { m_wrapperSet.insert(42, wrapper); } |
| 5441 |
| 5442 private: |
| 5443 HeapHashMap<int, Member<IntWrapper>> m_wrapperSet; |
| 5444 }; |
| 5445 |
| 5446 void workerThreadMain(MainThreadObject* mainThreadObject) { |
| 5447 MutexLocker locker(workerThreadMutex()); |
| 5448 |
| 5449 ThreadState::attachCurrentThread(); |
| 5450 |
| 5451 // Adding a reference to an object created on the worker thread to a |
| 5452 // HeapHashMap created on the main thread is not allowed. |
| 5453 mainThreadObject->addToSet(IntWrapper::create(42)); |
| 5454 |
| 5455 wakeMainThread(); |
| 5456 ThreadState::detachCurrentThread(); |
| 5457 } |
| 5458 }; |
| 5459 |
| 5460 #if DCHECK_IS_ON() |
| 5461 TEST(HeapTest, MarkingSameThreadCheck) { |
| 5462 // This will crash during marking, at the DCHECK in Visitor::markHeader() or |
| 5463 // earlier. |
| 5464 EXPECT_DEATH(MarkingSameThreadCheckTester().test(), ""); |
| 5465 } |
| 5466 #endif |
| 5467 |
| 5338 static bool allocateAndReturnBool() { | 5468 static bool allocateAndReturnBool() { |
| 5339 conservativelyCollectGarbage(); | 5469 conservativelyCollectGarbage(); |
| 5340 return true; | 5470 return true; |
| 5341 } | 5471 } |
| 5342 | 5472 |
| 5343 static bool checkGCForbidden() { | 5473 static bool checkGCForbidden() { |
| 5344 ASSERT(ThreadState::current()->isGCForbidden()); | 5474 ASSERT(ThreadState::current()->isGCForbidden()); |
| 5345 return true; | 5475 return true; |
| 5346 } | 5476 } |
| 5347 | 5477 |
| (...skipping 997 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 6345 "HeapVector"); | 6475 "HeapVector"); |
| 6346 static_assert( | 6476 static_assert( |
| 6347 WTF::IsGarbageCollectedType<HeapDeque<Member<IntWrapper>>>::value, | 6477 WTF::IsGarbageCollectedType<HeapDeque<Member<IntWrapper>>>::value, |
| 6348 "HeapDeque"); | 6478 "HeapDeque"); |
| 6349 static_assert(WTF::IsGarbageCollectedType< | 6479 static_assert(WTF::IsGarbageCollectedType< |
| 6350 HeapTerminatedArray<Member<IntWrapper>>>::value, | 6480 HeapTerminatedArray<Member<IntWrapper>>>::value, |
| 6351 "HeapTerminatedArray"); | 6481 "HeapTerminatedArray"); |
| 6352 } | 6482 } |
| 6353 | 6483 |
| 6354 } // namespace blink | 6484 } // namespace blink |
| OLD | NEW |