OLD | NEW |
(Empty) | |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "base/task_scheduler/scheduler_lock.h" |
| 6 |
| 7 #include <stdlib.h> |
| 8 |
| 9 #include "base/compiler_specific.h" |
| 10 #include "base/macros.h" |
| 11 #include "base/synchronization/waitable_event.h" |
| 12 #include "base/threading/platform_thread.h" |
| 13 #include "testing/gtest/include/gtest/gtest.h" |
| 14 |
| 15 namespace base { |
| 16 namespace task_scheduler { |
| 17 |
| 18 class BasicLockTestThread : public PlatformThread::Delegate { |
| 19 public: |
| 20 explicit BasicLockTestThread(SchedulerLock* lock) |
| 21 : lock_(lock), |
| 22 acquired_(0) {} |
| 23 |
| 24 void ThreadMain() override { |
| 25 for (int i = 0; i < 10; i++) { |
| 26 lock_->Acquire(); |
| 27 acquired_++; |
| 28 lock_->Release(); |
| 29 } |
| 30 for (int i = 0; i < 10; i++) { |
| 31 lock_->Acquire(); |
| 32 acquired_++; |
| 33 PlatformThread::Sleep(TimeDelta::FromMilliseconds(rand() % 20)); |
| 34 lock_->Release(); |
| 35 } |
| 36 } |
| 37 |
| 38 int acquired() const { return acquired_; } |
| 39 |
| 40 private: |
| 41 SchedulerLock* const lock_; |
| 42 int acquired_; |
| 43 |
| 44 DISALLOW_COPY_AND_ASSIGN(BasicLockTestThread); |
| 45 }; |
| 46 |
| 47 class BasicLockAcquireAndWaitThread : public PlatformThread::Delegate { |
| 48 public: |
| 49 explicit BasicLockAcquireAndWaitThread(SchedulerLock* lock) |
| 50 : lock_(lock), |
| 51 lock_acquire_event_(false, false), |
| 52 main_thread_continue_event_(false, false) {} |
| 53 |
| 54 void ThreadMain() override { |
| 55 lock_->Acquire(); |
| 56 lock_acquire_event_.Signal(); |
| 57 main_thread_continue_event_.Wait(); |
| 58 lock_->Release(); |
| 59 } |
| 60 |
| 61 void WaitForLockAcquition() { |
| 62 lock_acquire_event_.Wait(); |
| 63 } |
| 64 |
| 65 void ContinueMain() { |
| 66 main_thread_continue_event_.Signal(); |
| 67 } |
| 68 |
| 69 private: |
| 70 SchedulerLock* const lock_; |
| 71 WaitableEvent lock_acquire_event_; |
| 72 WaitableEvent main_thread_continue_event_; |
| 73 }; |
| 74 |
| 75 TEST(SchedulerLock, Basic) { |
| 76 SchedulerLock lock; |
| 77 BasicLockTestThread thread(&lock); |
| 78 PlatformThreadHandle handle; |
| 79 |
| 80 ASSERT_TRUE(PlatformThread::Create(0, &thread, &handle)); |
| 81 |
| 82 int acquired = 0; |
| 83 for (int i = 0; i < 5; i++) { |
| 84 lock.Acquire(); |
| 85 acquired++; |
| 86 lock.Release(); |
| 87 } |
| 88 for (int i = 0; i < 10; i++) { |
| 89 lock.Acquire(); |
| 90 acquired++; |
| 91 PlatformThread::Sleep(TimeDelta::FromMilliseconds(rand() % 20)); |
| 92 lock.Release(); |
| 93 } |
| 94 for (int i = 0; i < 5; i++) { |
| 95 lock.Acquire(); |
| 96 acquired++; |
| 97 PlatformThread::Sleep(TimeDelta::FromMilliseconds(rand() % 20)); |
| 98 lock.Release(); |
| 99 } |
| 100 |
| 101 PlatformThread::Join(handle); |
| 102 |
| 103 EXPECT_GE(acquired, 20); |
| 104 EXPECT_GE(thread.acquired(), 20); |
| 105 } |
| 106 |
| 107 TEST(SchedulerLock, AcquirePredecessor) { |
| 108 SchedulerLock lock_predecessor; |
| 109 SchedulerLock lock(&lock_predecessor); |
| 110 lock_predecessor.Acquire(); |
| 111 lock.Acquire(); |
| 112 lock.Release(); |
| 113 lock_predecessor.Release(); |
| 114 } |
| 115 |
| 116 TEST(SchedulerLock, AcquireNonPredecessor) { |
| 117 SchedulerLock lock_predecessor; |
| 118 SchedulerLock lock; |
| 119 lock_predecessor.Acquire(); |
| 120 EXPECT_DEATH(lock.Acquire(), ""); |
| 121 lock_predecessor.Release(); |
| 122 } |
| 123 |
| 124 TEST(SchedulerLock, AcquireMultipleLocksInOrder) { |
| 125 SchedulerLock lock1; |
| 126 SchedulerLock lock2(&lock1); |
| 127 SchedulerLock lock3(&lock2); |
| 128 lock1.Acquire(); |
| 129 lock2.Acquire(); |
| 130 lock3.Acquire(); |
| 131 lock3.Release(); |
| 132 lock2.Release(); |
| 133 lock1.Release(); |
| 134 } |
| 135 |
| 136 TEST(SchedulerLock, AcquireMultipleLocksInTheMiddleOfAChain) { |
| 137 SchedulerLock lock1; |
| 138 SchedulerLock lock2(&lock1); |
| 139 SchedulerLock lock3(&lock2); |
| 140 lock2.Acquire(); |
| 141 lock3.Acquire(); |
| 142 lock3.Release(); |
| 143 lock2.Release(); |
| 144 } |
| 145 |
| 146 TEST(SchedulerLock, AcquireMultipleLocksOutOfOrder) { |
| 147 SchedulerLock lock1; |
| 148 SchedulerLock lock2(&lock1); |
| 149 SchedulerLock lock3(&lock2); |
| 150 lock1.Acquire(); |
| 151 EXPECT_DEATH(lock3.Acquire(), ""); |
| 152 lock1.Release(); |
| 153 } |
| 154 |
| 155 TEST(SchedulerLock, AcquireLocksDifferentThreadsSafely) { |
| 156 SchedulerLock lock1; |
| 157 SchedulerLock lock2; |
| 158 BasicLockAcquireAndWaitThread thread(&lock1); |
| 159 PlatformThreadHandle handle; |
| 160 ASSERT_TRUE(PlatformThread::Create(0, &thread, &handle)); |
| 161 |
| 162 lock2.Acquire(); |
| 163 thread.WaitForLockAcquition(); |
| 164 thread.ContinueMain(); |
| 165 lock2.Release(); |
| 166 PlatformThread::Join(handle); |
| 167 } |
| 168 |
| 169 TEST(SchedulerLock, AcquireLocksWithPredecessorDifferentThreadsSafely) { |
| 170 SchedulerLock lock1; |
| 171 SchedulerLock lock2(&lock1); |
| 172 BasicLockAcquireAndWaitThread thread(&lock1); |
| 173 PlatformThreadHandle handle; |
| 174 ASSERT_TRUE(PlatformThread::Create(0, &thread, &handle)); |
| 175 |
| 176 lock2.Acquire(); |
| 177 thread.WaitForLockAcquition(); |
| 178 thread.ContinueMain(); |
| 179 lock2.Release(); |
| 180 PlatformThread::Join(handle); |
| 181 } |
| 182 |
| 183 } // namespace task_scheduler |
| 184 } // base |
OLD | NEW |