Chromium Code Reviews| 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 internal { | |
| 17 | |
| 18 class BasicLockTestThread : public PlatformThread::Delegate { | |
|
gab
2016/02/19 16:08:04
Meta comment on what this does.
robliao
2016/02/19 21:25:07
Done.
| |
| 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)); | |
|
gab
2016/02/19 16:08:04
Use base/rand_util.h instead of stdlib's rand()
robliao
2016/02/19 21:25:07
Done.
| |
| 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() { | |
|
gab
2016/02/19 16:08:03
WaitForLockAcquisition
^
robliao
2016/02/19 21:25:06
Done.
| |
| 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 }; | |
|
gab
2016/02/19 16:08:03
DISALLOW_COPY_AND_ASSIGN
robliao
2016/02/19 21:25:07
Done.
| |
| 74 | |
| 75 TEST(TaskSchedulerLock, 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); | |
|
gab
2016/02/19 16:08:03
Why GE and not EQ? (if EQ, put constant on LHS, pe
robliao
2016/02/19 21:25:07
The original lock test had a try, so there was a c
| |
| 105 } | |
| 106 | |
| 107 TEST(TaskSchedulerLock, 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(TaskSchedulerLock, AcquireNonPredecessor) { | |
| 117 SchedulerLock lock_predecessor; | |
|
gab
2016/02/19 16:08:03
s/lock_predecessor/other_lock/ (since it's not act
robliao
2016/02/19 21:25:06
Changed to lock1 and lock2 to match below.
| |
| 118 SchedulerLock lock; | |
| 119 EXPECT_DEBUG_DEATH({ | |
|
gab
2016/02/19 16:08:03
Cool :-)!! Didn't know about EXPECT_DEBUG_DEATH!
robliao
2016/02/19 21:25:06
Turns out Release builds for tests don't have DCHE
gab
2016/02/19 22:05:24
Hmmm okay (they probably should and I remember a d
robliao
2016/02/19 23:51:11
The right fix would be to update the EXPECT_DEBUG_
| |
| 120 lock_predecessor.Acquire(); | |
| 121 lock.Acquire(); | |
| 122 lock_predecessor.Release(); | |
| 123 }, ""); | |
| 124 } | |
| 125 | |
| 126 TEST(TaskSchedulerLock, AcquireMultipleLocksInOrder) { | |
| 127 SchedulerLock lock1; | |
| 128 SchedulerLock lock2(&lock1); | |
| 129 SchedulerLock lock3(&lock2); | |
| 130 lock1.Acquire(); | |
| 131 lock2.Acquire(); | |
| 132 lock3.Acquire(); | |
| 133 lock3.Release(); | |
| 134 lock2.Release(); | |
| 135 lock1.Release(); | |
| 136 } | |
| 137 | |
| 138 TEST(TaskSchedulerLock, AcquireMultipleLocksInTheMiddleOfAChain) { | |
| 139 SchedulerLock lock1; | |
| 140 SchedulerLock lock2(&lock1); | |
| 141 SchedulerLock lock3(&lock2); | |
| 142 lock2.Acquire(); | |
| 143 lock3.Acquire(); | |
| 144 lock3.Release(); | |
| 145 lock2.Release(); | |
| 146 } | |
| 147 | |
| 148 TEST(TaskSchedulerLock, AcquireMultipleLocksOutOfOrder) { | |
| 149 SchedulerLock lock1; | |
| 150 SchedulerLock lock2(&lock1); | |
| 151 SchedulerLock lock3(&lock2); | |
|
gab
2016/02/19 16:08:03
Looks like this test only needs two locks. Let's k
robliao
2016/02/19 21:25:06
This is actually a little different from AcquireNo
gab
2016/02/19 22:05:24
Ah I see then I think we also need an "out-of-orde
robliao
2016/02/19 23:51:11
Done.
| |
| 152 EXPECT_DEBUG_DEATH({ | |
| 153 lock1.Acquire(); | |
| 154 lock3.Acquire(); | |
|
gab
2016/02/19 16:08:04
I'd prefer if this was the last statement in the E
robliao
2016/02/19 21:25:06
Done.
| |
| 155 lock1.Release(); | |
|
gab
2016/02/19 16:08:03
In non-dcheck builds you have to Release lock3 or
robliao
2016/02/19 21:25:07
Turns out, this is okay due to forking on non-Wind
| |
| 156 }, ""); | |
| 157 } | |
| 158 | |
| 159 TEST(TaskSchedulerLock, AcquireLocksDifferentThreadsSafely) { | |
| 160 SchedulerLock lock1; | |
| 161 SchedulerLock lock2; | |
| 162 BasicLockAcquireAndWaitThread thread(&lock1); | |
| 163 PlatformThreadHandle handle; | |
| 164 ASSERT_TRUE(PlatformThread::Create(0, &thread, &handle)); | |
| 165 | |
| 166 lock2.Acquire(); | |
| 167 thread.WaitForLockAcquition(); | |
| 168 thread.ContinueMain(); | |
| 169 lock2.Release(); | |
| 170 PlatformThread::Join(handle); | |
| 171 } | |
| 172 | |
| 173 TEST(TaskSchedulerLock, AcquireLocksWithPredecessorDifferentThreadsSafely) { | |
| 174 SchedulerLock lock1; | |
| 175 SchedulerLock lock2(&lock1); | |
| 176 BasicLockAcquireAndWaitThread thread(&lock1); | |
| 177 PlatformThreadHandle handle; | |
| 178 ASSERT_TRUE(PlatformThread::Create(0, &thread, &handle)); | |
| 179 | |
|
gab
2016/02/19 16:08:03
Think we also need
thread.ContinueAcquireLock();
t
robliao
2016/02/19 21:25:06
Clarified the test and added explicit locking orde
| |
| 180 lock2.Acquire(); | |
| 181 thread.WaitForLockAcquition(); | |
| 182 thread.ContinueMain(); | |
| 183 lock2.Release(); | |
| 184 PlatformThread::Join(handle); | |
| 185 } | |
| 186 | |
| 187 TEST(TaskSchedulerLock, SelfReferentialLock) { | |
| 188 struct SelfReferentialLock { | |
| 189 SelfReferentialLock() : lock(&lock) {} | |
| 190 | |
| 191 SchedulerLock lock; | |
| 192 }; | |
| 193 | |
| 194 EXPECT_DEBUG_DEATH({ SelfReferentialLock lock; }, ""); | |
| 195 } | |
| 196 | |
| 197 TEST(TaskSchedulerLock, PredecessorCycle) { | |
| 198 struct LockCycle { | |
| 199 LockCycle() : lock1(&lock2), lock2(&lock1) {} | |
| 200 | |
| 201 SchedulerLock lock1; | |
| 202 SchedulerLock lock2; | |
| 203 }; | |
| 204 | |
| 205 EXPECT_DEBUG_DEATH({ LockCycle cycle; }, ""); | |
| 206 } | |
| 207 | |
|
gab
2016/02/19 16:08:04
Ah interesting :-), thought taking predecessor as
robliao
2016/02/19 21:25:07
Done.
| |
| 208 } // namespace internal | |
| 209 } // base | |
| OLD | NEW |