Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(577)

Side by Side Diff: base/task_scheduler/scheduler_lock_unittest.cc

Issue 1706123002: TaskScheduler [2/9] Scheduler Lock (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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 {
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(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);
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;
118 SchedulerLock lock;
119 EXPECT_DEBUG_DEATH({
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);
152 EXPECT_DEBUG_DEATH({
153 lock1.Acquire();
154 lock3.Acquire();
155 lock1.Release();
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
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
208 } // namespace internal
209 } // base
OLDNEW
« base/task_scheduler/scheduler_lock_impl.cc ('K') | « base/task_scheduler/scheduler_lock_impl.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698