OLD | NEW |
1 // Copyright 2014 the V8 project authors. All rights reserved. | 1 // Copyright 2014 the V8 project 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 "src/base/platform/condition-variable.h" | 5 #include "src/base/platform/condition-variable.h" |
6 | 6 |
7 #include "src/base/platform/platform.h" | 7 #include "src/base/platform/platform.h" |
8 #include "src/base/platform/time.h" | 8 #include "src/base/platform/time.h" |
9 #include "testing/gtest/include/gtest/gtest.h" | 9 #include "testing/gtest/include/gtest/gtest.h" |
10 | 10 |
(...skipping 11 matching lines...) Expand all Loading... |
22 EXPECT_FALSE(cv.WaitFor(&mutex, TimeDelta::FromMicroseconds(n))); | 22 EXPECT_FALSE(cv.WaitFor(&mutex, TimeDelta::FromMicroseconds(n))); |
23 | 23 |
24 cv.NotifyAll(); | 24 cv.NotifyAll(); |
25 EXPECT_FALSE(cv.WaitFor(&mutex, TimeDelta::FromMicroseconds(n))); | 25 EXPECT_FALSE(cv.WaitFor(&mutex, TimeDelta::FromMicroseconds(n))); |
26 } | 26 } |
27 } | 27 } |
28 | 28 |
29 | 29 |
30 namespace { | 30 namespace { |
31 | 31 |
32 class ThreadWithMutexAndConditionVariable V8_FINAL : public Thread { | 32 class ThreadWithMutexAndConditionVariable FINAL : public Thread { |
33 public: | 33 public: |
34 ThreadWithMutexAndConditionVariable() | 34 ThreadWithMutexAndConditionVariable() |
35 : Thread(Options("ThreadWithMutexAndConditionVariable")), | 35 : Thread(Options("ThreadWithMutexAndConditionVariable")), |
36 running_(false), | 36 running_(false), |
37 finished_(false) {} | 37 finished_(false) {} |
38 virtual ~ThreadWithMutexAndConditionVariable() {} | 38 virtual ~ThreadWithMutexAndConditionVariable() {} |
39 | 39 |
40 virtual void Run() V8_OVERRIDE { | 40 virtual void Run() OVERRIDE { |
41 LockGuard<Mutex> lock_guard(&mutex_); | 41 LockGuard<Mutex> lock_guard(&mutex_); |
42 running_ = true; | 42 running_ = true; |
43 cv_.NotifyOne(); | 43 cv_.NotifyOne(); |
44 while (running_) { | 44 while (running_) { |
45 cv_.Wait(&mutex_); | 45 cv_.Wait(&mutex_); |
46 } | 46 } |
47 finished_ = true; | 47 finished_ = true; |
48 cv_.NotifyAll(); | 48 cv_.NotifyAll(); |
49 } | 49 } |
50 | 50 |
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
101 threads[n].Join(); | 101 threads[n].Join(); |
102 LockGuard<Mutex> lock_guard(&threads[n].mutex_); | 102 LockGuard<Mutex> lock_guard(&threads[n].mutex_); |
103 EXPECT_FALSE(threads[n].running_); | 103 EXPECT_FALSE(threads[n].running_); |
104 EXPECT_TRUE(threads[n].finished_); | 104 EXPECT_TRUE(threads[n].finished_); |
105 } | 105 } |
106 } | 106 } |
107 | 107 |
108 | 108 |
109 namespace { | 109 namespace { |
110 | 110 |
111 class ThreadWithSharedMutexAndConditionVariable V8_FINAL : public Thread { | 111 class ThreadWithSharedMutexAndConditionVariable FINAL : public Thread { |
112 public: | 112 public: |
113 ThreadWithSharedMutexAndConditionVariable() | 113 ThreadWithSharedMutexAndConditionVariable() |
114 : Thread(Options("ThreadWithSharedMutexAndConditionVariable")), | 114 : Thread(Options("ThreadWithSharedMutexAndConditionVariable")), |
115 running_(false), | 115 running_(false), |
116 finished_(false), | 116 finished_(false), |
117 cv_(NULL), | 117 cv_(NULL), |
118 mutex_(NULL) {} | 118 mutex_(NULL) {} |
119 virtual ~ThreadWithSharedMutexAndConditionVariable() {} | 119 virtual ~ThreadWithSharedMutexAndConditionVariable() {} |
120 | 120 |
121 virtual void Run() V8_OVERRIDE { | 121 virtual void Run() OVERRIDE { |
122 LockGuard<Mutex> lock_guard(mutex_); | 122 LockGuard<Mutex> lock_guard(mutex_); |
123 running_ = true; | 123 running_ = true; |
124 cv_->NotifyAll(); | 124 cv_->NotifyAll(); |
125 while (running_) { | 125 while (running_) { |
126 cv_->Wait(mutex_); | 126 cv_->Wait(mutex_); |
127 } | 127 } |
128 finished_ = true; | 128 finished_ = true; |
129 cv_->NotifyAll(); | 129 cv_->NotifyAll(); |
130 } | 130 } |
131 | 131 |
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
211 | 211 |
212 // Join all threads. | 212 // Join all threads. |
213 for (int n = 0; n < kThreadCount; ++n) { | 213 for (int n = 0; n < kThreadCount; ++n) { |
214 threads[n].Join(); | 214 threads[n].Join(); |
215 } | 215 } |
216 } | 216 } |
217 | 217 |
218 | 218 |
219 namespace { | 219 namespace { |
220 | 220 |
221 class LoopIncrementThread V8_FINAL : public Thread { | 221 class LoopIncrementThread FINAL : public Thread { |
222 public: | 222 public: |
223 LoopIncrementThread(int rem, int* counter, int limit, int thread_count, | 223 LoopIncrementThread(int rem, int* counter, int limit, int thread_count, |
224 ConditionVariable* cv, Mutex* mutex) | 224 ConditionVariable* cv, Mutex* mutex) |
225 : Thread(Options("LoopIncrementThread")), | 225 : Thread(Options("LoopIncrementThread")), |
226 rem_(rem), | 226 rem_(rem), |
227 counter_(counter), | 227 counter_(counter), |
228 limit_(limit), | 228 limit_(limit), |
229 thread_count_(thread_count), | 229 thread_count_(thread_count), |
230 cv_(cv), | 230 cv_(cv), |
231 mutex_(mutex) { | 231 mutex_(mutex) { |
232 EXPECT_LT(rem, thread_count); | 232 EXPECT_LT(rem, thread_count); |
233 EXPECT_EQ(0, limit % thread_count); | 233 EXPECT_EQ(0, limit % thread_count); |
234 } | 234 } |
235 | 235 |
236 virtual void Run() V8_OVERRIDE { | 236 virtual void Run() OVERRIDE { |
237 int last_count = -1; | 237 int last_count = -1; |
238 while (true) { | 238 while (true) { |
239 LockGuard<Mutex> lock_guard(mutex_); | 239 LockGuard<Mutex> lock_guard(mutex_); |
240 int count = *counter_; | 240 int count = *counter_; |
241 while (count % thread_count_ != rem_ && count < limit_) { | 241 while (count % thread_count_ != rem_ && count < limit_) { |
242 cv_->Wait(mutex_); | 242 cv_->Wait(mutex_); |
243 count = *counter_; | 243 count = *counter_; |
244 } | 244 } |
245 if (count >= limit_) break; | 245 if (count >= limit_) break; |
246 EXPECT_EQ(*counter_, count); | 246 EXPECT_EQ(*counter_, count); |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
292 delete threads[n]; | 292 delete threads[n]; |
293 } | 293 } |
294 delete[] threads; | 294 delete[] threads; |
295 | 295 |
296 EXPECT_EQ(limit, counter); | 296 EXPECT_EQ(limit, counter); |
297 } | 297 } |
298 } | 298 } |
299 | 299 |
300 } // namespace base | 300 } // namespace base |
301 } // namespace v8 | 301 } // namespace v8 |
OLD | NEW |