| 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 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 47 finished_ = true; | 47 finished_ = true; |
| 48 cv_.NotifyAll(); | 48 cv_.NotifyAll(); |
| 49 } | 49 } |
| 50 | 50 |
| 51 bool running_; | 51 bool running_; |
| 52 bool finished_; | 52 bool finished_; |
| 53 ConditionVariable cv_; | 53 ConditionVariable cv_; |
| 54 Mutex mutex_; | 54 Mutex mutex_; |
| 55 }; | 55 }; |
| 56 | 56 |
| 57 } | 57 } // namespace |
| 58 | 58 |
| 59 | 59 |
| 60 TEST(ConditionVariable, MultipleThreadsWithSeparateConditionVariables) { | 60 TEST(ConditionVariable, MultipleThreadsWithSeparateConditionVariables) { |
| 61 static const int kThreadCount = 128; | 61 static const int kThreadCount = 128; |
| 62 ThreadWithMutexAndConditionVariable threads[kThreadCount]; | 62 ThreadWithMutexAndConditionVariable threads[kThreadCount]; |
| 63 | 63 |
| 64 for (int n = 0; n < kThreadCount; ++n) { | 64 for (int n = 0; n < kThreadCount; ++n) { |
| 65 LockGuard<Mutex> lock_guard(&threads[n].mutex_); | 65 LockGuard<Mutex> lock_guard(&threads[n].mutex_); |
| 66 EXPECT_FALSE(threads[n].running_); | 66 EXPECT_FALSE(threads[n].running_); |
| 67 EXPECT_FALSE(threads[n].finished_); | 67 EXPECT_FALSE(threads[n].finished_); |
| (...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 128 finished_ = true; | 128 finished_ = true; |
| 129 cv_->NotifyAll(); | 129 cv_->NotifyAll(); |
| 130 } | 130 } |
| 131 | 131 |
| 132 bool running_; | 132 bool running_; |
| 133 bool finished_; | 133 bool finished_; |
| 134 ConditionVariable* cv_; | 134 ConditionVariable* cv_; |
| 135 Mutex* mutex_; | 135 Mutex* mutex_; |
| 136 }; | 136 }; |
| 137 | 137 |
| 138 } | 138 } // namespace |
| 139 | 139 |
| 140 | 140 |
| 141 TEST(ConditionVariable, MultipleThreadsWithSharedSeparateConditionVariables) { | 141 TEST(ConditionVariable, MultipleThreadsWithSharedSeparateConditionVariables) { |
| 142 static const int kThreadCount = 128; | 142 static const int kThreadCount = 128; |
| 143 ThreadWithSharedMutexAndConditionVariable threads[kThreadCount]; | 143 ThreadWithSharedMutexAndConditionVariable threads[kThreadCount]; |
| 144 ConditionVariable cv; | 144 ConditionVariable cv; |
| 145 Mutex mutex; | 145 Mutex mutex; |
| 146 | 146 |
| 147 for (int n = 0; n < kThreadCount; ++n) { | 147 for (int n = 0; n < kThreadCount; ++n) { |
| 148 threads[n].mutex_ = &mutex; | 148 threads[n].mutex_ = &mutex; |
| (...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 256 | 256 |
| 257 private: | 257 private: |
| 258 const int rem_; | 258 const int rem_; |
| 259 int* counter_; | 259 int* counter_; |
| 260 const int limit_; | 260 const int limit_; |
| 261 const int thread_count_; | 261 const int thread_count_; |
| 262 ConditionVariable* cv_; | 262 ConditionVariable* cv_; |
| 263 Mutex* mutex_; | 263 Mutex* mutex_; |
| 264 }; | 264 }; |
| 265 | 265 |
| 266 } | 266 } // namespace |
| 267 | 267 |
| 268 | 268 |
| 269 TEST(ConditionVariable, LoopIncrement) { | 269 TEST(ConditionVariable, LoopIncrement) { |
| 270 static const int kMaxThreadCount = 16; | 270 static const int kMaxThreadCount = 16; |
| 271 Mutex mutex; | 271 Mutex mutex; |
| 272 ConditionVariable cv; | 272 ConditionVariable cv; |
| 273 for (int thread_count = 1; thread_count < kMaxThreadCount; ++thread_count) { | 273 for (int thread_count = 1; thread_count < kMaxThreadCount; ++thread_count) { |
| 274 int limit = thread_count * 10; | 274 int limit = thread_count * 10; |
| 275 int counter = 0; | 275 int counter = 0; |
| 276 | 276 |
| 277 // Setup the threads. | 277 // Setup the threads. |
| 278 Thread** threads = new Thread*[thread_count]; | 278 Thread** threads = new Thread* [thread_count]; |
| 279 for (int n = 0; n < thread_count; ++n) { | 279 for (int n = 0; n < thread_count; ++n) { |
| 280 threads[n] = new LoopIncrementThread( | 280 threads[n] = new LoopIncrementThread(n, &counter, limit, thread_count, |
| 281 n, &counter, limit, thread_count, &cv, &mutex); | 281 &cv, &mutex); |
| 282 } | 282 } |
| 283 | 283 |
| 284 // Start all threads. | 284 // Start all threads. |
| 285 for (int n = thread_count - 1; n >= 0; --n) { | 285 for (int n = thread_count - 1; n >= 0; --n) { |
| 286 threads[n]->Start(); | 286 threads[n]->Start(); |
| 287 } | 287 } |
| 288 | 288 |
| 289 // Join and cleanup all threads. | 289 // Join and cleanup all threads. |
| 290 for (int n = 0; n < thread_count; ++n) { | 290 for (int n = 0; n < thread_count; ++n) { |
| 291 threads[n]->Join(); | 291 threads[n]->Join(); |
| 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 |