| 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 <cstring> | 5 #include <cstring> |
| 6 | 6 |
| 7 #include "src/base/platform/platform.h" | 7 #include "src/base/platform/platform.h" |
| 8 #include "src/base/platform/semaphore.h" | 8 #include "src/base/platform/semaphore.h" |
| 9 #include "src/base/platform/time.h" | 9 #include "src/base/platform/time.h" |
| 10 #include "testing/gtest/include/gtest/gtest.h" | 10 #include "testing/gtest/include/gtest/gtest.h" |
| 11 | 11 |
| 12 namespace v8 { | 12 namespace v8 { |
| 13 namespace base { | 13 namespace base { |
| 14 | 14 |
| 15 namespace { | 15 namespace { |
| 16 | 16 |
| 17 static const char kAlphabet[] = "XKOAD"; | 17 static const char kAlphabet[] = "XKOAD"; |
| 18 static const size_t kAlphabetSize = sizeof(kAlphabet) - 1; | 18 static const size_t kAlphabetSize = sizeof(kAlphabet) - 1; |
| 19 static const size_t kBufferSize = 987; // GCD(buffer size, alphabet size) = 1 | 19 static const size_t kBufferSize = 987; // GCD(buffer size, alphabet size) = 1 |
| 20 static const size_t kDataSize = kBufferSize * kAlphabetSize * 10; | 20 static const size_t kDataSize = kBufferSize * kAlphabetSize * 10; |
| 21 | 21 |
| 22 | 22 |
| 23 class ProducerThread V8_FINAL : public Thread { | 23 class ProducerThread FINAL : public Thread { |
| 24 public: | 24 public: |
| 25 ProducerThread(char* buffer, Semaphore* free_space, Semaphore* used_space) | 25 ProducerThread(char* buffer, Semaphore* free_space, Semaphore* used_space) |
| 26 : Thread(Options("ProducerThread")), | 26 : Thread(Options("ProducerThread")), |
| 27 buffer_(buffer), | 27 buffer_(buffer), |
| 28 free_space_(free_space), | 28 free_space_(free_space), |
| 29 used_space_(used_space) {} | 29 used_space_(used_space) {} |
| 30 virtual ~ProducerThread() {} | 30 virtual ~ProducerThread() {} |
| 31 | 31 |
| 32 virtual void Run() V8_OVERRIDE { | 32 virtual void Run() OVERRIDE { |
| 33 for (size_t n = 0; n < kDataSize; ++n) { | 33 for (size_t n = 0; n < kDataSize; ++n) { |
| 34 free_space_->Wait(); | 34 free_space_->Wait(); |
| 35 buffer_[n % kBufferSize] = kAlphabet[n % kAlphabetSize]; | 35 buffer_[n % kBufferSize] = kAlphabet[n % kAlphabetSize]; |
| 36 used_space_->Signal(); | 36 used_space_->Signal(); |
| 37 } | 37 } |
| 38 } | 38 } |
| 39 | 39 |
| 40 private: | 40 private: |
| 41 char* buffer_; | 41 char* buffer_; |
| 42 Semaphore* const free_space_; | 42 Semaphore* const free_space_; |
| 43 Semaphore* const used_space_; | 43 Semaphore* const used_space_; |
| 44 }; | 44 }; |
| 45 | 45 |
| 46 | 46 |
| 47 class ConsumerThread V8_FINAL : public Thread { | 47 class ConsumerThread FINAL : public Thread { |
| 48 public: | 48 public: |
| 49 ConsumerThread(const char* buffer, Semaphore* free_space, | 49 ConsumerThread(const char* buffer, Semaphore* free_space, |
| 50 Semaphore* used_space) | 50 Semaphore* used_space) |
| 51 : Thread(Options("ConsumerThread")), | 51 : Thread(Options("ConsumerThread")), |
| 52 buffer_(buffer), | 52 buffer_(buffer), |
| 53 free_space_(free_space), | 53 free_space_(free_space), |
| 54 used_space_(used_space) {} | 54 used_space_(used_space) {} |
| 55 virtual ~ConsumerThread() {} | 55 virtual ~ConsumerThread() {} |
| 56 | 56 |
| 57 virtual void Run() V8_OVERRIDE { | 57 virtual void Run() OVERRIDE { |
| 58 for (size_t n = 0; n < kDataSize; ++n) { | 58 for (size_t n = 0; n < kDataSize; ++n) { |
| 59 used_space_->Wait(); | 59 used_space_->Wait(); |
| 60 EXPECT_EQ(kAlphabet[n % kAlphabetSize], buffer_[n % kBufferSize]); | 60 EXPECT_EQ(kAlphabet[n % kAlphabetSize], buffer_[n % kBufferSize]); |
| 61 free_space_->Signal(); | 61 free_space_->Signal(); |
| 62 } | 62 } |
| 63 } | 63 } |
| 64 | 64 |
| 65 private: | 65 private: |
| 66 const char* buffer_; | 66 const char* buffer_; |
| 67 Semaphore* const free_space_; | 67 Semaphore* const free_space_; |
| 68 Semaphore* const used_space_; | 68 Semaphore* const used_space_; |
| 69 }; | 69 }; |
| 70 | 70 |
| 71 | 71 |
| 72 class WaitAndSignalThread V8_FINAL : public Thread { | 72 class WaitAndSignalThread FINAL : public Thread { |
| 73 public: | 73 public: |
| 74 explicit WaitAndSignalThread(Semaphore* semaphore) | 74 explicit WaitAndSignalThread(Semaphore* semaphore) |
| 75 : Thread(Options("WaitAndSignalThread")), semaphore_(semaphore) {} | 75 : Thread(Options("WaitAndSignalThread")), semaphore_(semaphore) {} |
| 76 virtual ~WaitAndSignalThread() {} | 76 virtual ~WaitAndSignalThread() {} |
| 77 | 77 |
| 78 virtual void Run() V8_OVERRIDE { | 78 virtual void Run() OVERRIDE { |
| 79 for (int n = 0; n < 100; ++n) { | 79 for (int n = 0; n < 100; ++n) { |
| 80 semaphore_->Wait(); | 80 semaphore_->Wait(); |
| 81 ASSERT_FALSE(semaphore_->WaitFor(TimeDelta::FromMicroseconds(1))); | 81 ASSERT_FALSE(semaphore_->WaitFor(TimeDelta::FromMicroseconds(1))); |
| 82 semaphore_->Signal(); | 82 semaphore_->Signal(); |
| 83 } | 83 } |
| 84 } | 84 } |
| 85 | 85 |
| 86 private: | 86 private: |
| 87 Semaphore* const semaphore_; | 87 Semaphore* const semaphore_; |
| 88 }; | 88 }; |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 136 semaphore.Signal(); | 136 semaphore.Signal(); |
| 137 ASSERT_TRUE(semaphore.WaitFor(TimeDelta::FromMicroseconds(0))); | 137 ASSERT_TRUE(semaphore.WaitFor(TimeDelta::FromMicroseconds(0))); |
| 138 semaphore.Signal(); | 138 semaphore.Signal(); |
| 139 ASSERT_TRUE(semaphore.WaitFor(TimeDelta::FromMicroseconds(100))); | 139 ASSERT_TRUE(semaphore.WaitFor(TimeDelta::FromMicroseconds(100))); |
| 140 semaphore.Signal(); | 140 semaphore.Signal(); |
| 141 ASSERT_TRUE(semaphore.WaitFor(TimeDelta::FromMicroseconds(1000))); | 141 ASSERT_TRUE(semaphore.WaitFor(TimeDelta::FromMicroseconds(1000))); |
| 142 } | 142 } |
| 143 | 143 |
| 144 } // namespace base | 144 } // namespace base |
| 145 } // namespace v8 | 145 } // namespace v8 |
| OLD | NEW |