OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "base/synchronization/lock.h" | 5 #include "base/synchronization/lock.h" |
6 | 6 |
7 #include <stdlib.h> | 7 #include <stdlib.h> |
8 | 8 |
9 #include "base/compiler_specific.h" | 9 #include "base/compiler_specific.h" |
10 #include "base/threading/platform_thread.h" | 10 #include "base/threading/platform_thread.h" |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
44 private: | 44 private: |
45 Lock* lock_; | 45 Lock* lock_; |
46 int acquired_; | 46 int acquired_; |
47 | 47 |
48 DISALLOW_COPY_AND_ASSIGN(BasicLockTestThread); | 48 DISALLOW_COPY_AND_ASSIGN(BasicLockTestThread); |
49 }; | 49 }; |
50 | 50 |
51 TEST(LockTest, Basic) { | 51 TEST(LockTest, Basic) { |
52 Lock lock; | 52 Lock lock; |
53 BasicLockTestThread thread(&lock); | 53 BasicLockTestThread thread(&lock); |
54 PlatformThreadHandle handle = kNullThreadHandle; | 54 PlatformThreadHandle handle; |
55 | 55 |
56 ASSERT_TRUE(PlatformThread::Create(0, &thread, &handle)); | 56 ASSERT_TRUE(PlatformThread::Create(0, &thread, &handle)); |
57 | 57 |
58 int acquired = 0; | 58 int acquired = 0; |
59 for (int i = 0; i < 5; i++) { | 59 for (int i = 0; i < 5; i++) { |
60 lock.Acquire(); | 60 lock.Acquire(); |
61 acquired++; | 61 acquired++; |
62 lock.Release(); | 62 lock.Release(); |
63 } | 63 } |
64 for (int i = 0; i < 10; i++) { | 64 for (int i = 0; i < 10; i++) { |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
110 | 110 |
111 TEST(LockTest, TryLock) { | 111 TEST(LockTest, TryLock) { |
112 Lock lock; | 112 Lock lock; |
113 | 113 |
114 ASSERT_TRUE(lock.Try()); | 114 ASSERT_TRUE(lock.Try()); |
115 // We now have the lock.... | 115 // We now have the lock.... |
116 | 116 |
117 // This thread will not be able to get the lock. | 117 // This thread will not be able to get the lock. |
118 { | 118 { |
119 TryLockTestThread thread(&lock); | 119 TryLockTestThread thread(&lock); |
120 PlatformThreadHandle handle = kNullThreadHandle; | 120 PlatformThreadHandle handle; |
121 | 121 |
122 ASSERT_TRUE(PlatformThread::Create(0, &thread, &handle)); | 122 ASSERT_TRUE(PlatformThread::Create(0, &thread, &handle)); |
123 | 123 |
124 PlatformThread::Join(handle); | 124 PlatformThread::Join(handle); |
125 | 125 |
126 ASSERT_FALSE(thread.got_lock()); | 126 ASSERT_FALSE(thread.got_lock()); |
127 } | 127 } |
128 | 128 |
129 lock.Release(); | 129 lock.Release(); |
130 | 130 |
131 // This thread will.... | 131 // This thread will.... |
132 { | 132 { |
133 TryLockTestThread thread(&lock); | 133 TryLockTestThread thread(&lock); |
134 PlatformThreadHandle handle = kNullThreadHandle; | 134 PlatformThreadHandle handle; |
135 | 135 |
136 ASSERT_TRUE(PlatformThread::Create(0, &thread, &handle)); | 136 ASSERT_TRUE(PlatformThread::Create(0, &thread, &handle)); |
137 | 137 |
138 PlatformThread::Join(handle); | 138 PlatformThread::Join(handle); |
139 | 139 |
140 ASSERT_TRUE(thread.got_lock()); | 140 ASSERT_TRUE(thread.got_lock()); |
141 // But it released it.... | 141 // But it released it.... |
142 ASSERT_TRUE(lock.Try()); | 142 ASSERT_TRUE(lock.Try()); |
143 } | 143 } |
144 | 144 |
(...skipping 26 matching lines...) Expand all Loading... |
171 int* value_; | 171 int* value_; |
172 | 172 |
173 DISALLOW_COPY_AND_ASSIGN(MutexLockTestThread); | 173 DISALLOW_COPY_AND_ASSIGN(MutexLockTestThread); |
174 }; | 174 }; |
175 | 175 |
176 TEST(LockTest, MutexTwoThreads) { | 176 TEST(LockTest, MutexTwoThreads) { |
177 Lock lock; | 177 Lock lock; |
178 int value = 0; | 178 int value = 0; |
179 | 179 |
180 MutexLockTestThread thread(&lock, &value); | 180 MutexLockTestThread thread(&lock, &value); |
181 PlatformThreadHandle handle = kNullThreadHandle; | 181 PlatformThreadHandle handle; |
182 | 182 |
183 ASSERT_TRUE(PlatformThread::Create(0, &thread, &handle)); | 183 ASSERT_TRUE(PlatformThread::Create(0, &thread, &handle)); |
184 | 184 |
185 MutexLockTestThread::DoStuff(&lock, &value); | 185 MutexLockTestThread::DoStuff(&lock, &value); |
186 | 186 |
187 PlatformThread::Join(handle); | 187 PlatformThread::Join(handle); |
188 | 188 |
189 EXPECT_EQ(2 * 40, value); | 189 EXPECT_EQ(2 * 40, value); |
190 } | 190 } |
191 | 191 |
192 TEST(LockTest, MutexFourThreads) { | 192 TEST(LockTest, MutexFourThreads) { |
193 Lock lock; | 193 Lock lock; |
194 int value = 0; | 194 int value = 0; |
195 | 195 |
196 MutexLockTestThread thread1(&lock, &value); | 196 MutexLockTestThread thread1(&lock, &value); |
197 MutexLockTestThread thread2(&lock, &value); | 197 MutexLockTestThread thread2(&lock, &value); |
198 MutexLockTestThread thread3(&lock, &value); | 198 MutexLockTestThread thread3(&lock, &value); |
199 PlatformThreadHandle handle1 = kNullThreadHandle; | 199 PlatformThreadHandle handle1; |
200 PlatformThreadHandle handle2 = kNullThreadHandle; | 200 PlatformThreadHandle handle2; |
201 PlatformThreadHandle handle3 = kNullThreadHandle; | 201 PlatformThreadHandle handle3; |
202 | 202 |
203 ASSERT_TRUE(PlatformThread::Create(0, &thread1, &handle1)); | 203 ASSERT_TRUE(PlatformThread::Create(0, &thread1, &handle1)); |
204 ASSERT_TRUE(PlatformThread::Create(0, &thread2, &handle2)); | 204 ASSERT_TRUE(PlatformThread::Create(0, &thread2, &handle2)); |
205 ASSERT_TRUE(PlatformThread::Create(0, &thread3, &handle3)); | 205 ASSERT_TRUE(PlatformThread::Create(0, &thread3, &handle3)); |
206 | 206 |
207 MutexLockTestThread::DoStuff(&lock, &value); | 207 MutexLockTestThread::DoStuff(&lock, &value); |
208 | 208 |
209 PlatformThread::Join(handle1); | 209 PlatformThread::Join(handle1); |
210 PlatformThread::Join(handle2); | 210 PlatformThread::Join(handle2); |
211 PlatformThread::Join(handle3); | 211 PlatformThread::Join(handle3); |
212 | 212 |
213 EXPECT_EQ(4 * 40, value); | 213 EXPECT_EQ(4 * 40, value); |
214 } | 214 } |
215 | 215 |
216 } // namespace base | 216 } // namespace base |
OLD | NEW |