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 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() {} | |
31 | 30 |
32 virtual void Run() override { | 31 void Run() override { |
33 for (size_t n = 0; n < kDataSize; ++n) { | 32 for (size_t n = 0; n < kDataSize; ++n) { |
34 free_space_->Wait(); | 33 free_space_->Wait(); |
35 buffer_[n % kBufferSize] = kAlphabet[n % kAlphabetSize]; | 34 buffer_[n % kBufferSize] = kAlphabet[n % kAlphabetSize]; |
36 used_space_->Signal(); | 35 used_space_->Signal(); |
37 } | 36 } |
38 } | 37 } |
39 | 38 |
40 private: | 39 private: |
41 char* buffer_; | 40 char* buffer_; |
42 Semaphore* const free_space_; | 41 Semaphore* const free_space_; |
43 Semaphore* const used_space_; | 42 Semaphore* const used_space_; |
44 }; | 43 }; |
45 | 44 |
46 | 45 |
47 class ConsumerThread final : public Thread { | 46 class ConsumerThread final : public Thread { |
48 public: | 47 public: |
49 ConsumerThread(const char* buffer, Semaphore* free_space, | 48 ConsumerThread(const char* buffer, Semaphore* free_space, |
50 Semaphore* used_space) | 49 Semaphore* used_space) |
51 : Thread(Options("ConsumerThread")), | 50 : Thread(Options("ConsumerThread")), |
52 buffer_(buffer), | 51 buffer_(buffer), |
53 free_space_(free_space), | 52 free_space_(free_space), |
54 used_space_(used_space) {} | 53 used_space_(used_space) {} |
55 virtual ~ConsumerThread() {} | |
56 | 54 |
57 virtual void Run() override { | 55 void Run() override { |
58 for (size_t n = 0; n < kDataSize; ++n) { | 56 for (size_t n = 0; n < kDataSize; ++n) { |
59 used_space_->Wait(); | 57 used_space_->Wait(); |
60 EXPECT_EQ(kAlphabet[n % kAlphabetSize], buffer_[n % kBufferSize]); | 58 EXPECT_EQ(kAlphabet[n % kAlphabetSize], buffer_[n % kBufferSize]); |
61 free_space_->Signal(); | 59 free_space_->Signal(); |
62 } | 60 } |
63 } | 61 } |
64 | 62 |
65 private: | 63 private: |
66 const char* buffer_; | 64 const char* buffer_; |
67 Semaphore* const free_space_; | 65 Semaphore* const free_space_; |
68 Semaphore* const used_space_; | 66 Semaphore* const used_space_; |
69 }; | 67 }; |
70 | 68 |
71 | 69 |
72 class WaitAndSignalThread final : public Thread { | 70 class WaitAndSignalThread final : public Thread { |
73 public: | 71 public: |
74 explicit WaitAndSignalThread(Semaphore* semaphore) | 72 explicit WaitAndSignalThread(Semaphore* semaphore) |
75 : Thread(Options("WaitAndSignalThread")), semaphore_(semaphore) {} | 73 : Thread(Options("WaitAndSignalThread")), semaphore_(semaphore) {} |
76 virtual ~WaitAndSignalThread() {} | |
77 | 74 |
78 virtual void Run() override { | 75 void Run() override { |
79 for (int n = 0; n < 100; ++n) { | 76 for (int n = 0; n < 100; ++n) { |
80 semaphore_->Wait(); | 77 semaphore_->Wait(); |
81 ASSERT_FALSE(semaphore_->WaitFor(TimeDelta::FromMicroseconds(1))); | 78 ASSERT_FALSE(semaphore_->WaitFor(TimeDelta::FromMicroseconds(1))); |
82 semaphore_->Signal(); | 79 semaphore_->Signal(); |
83 } | 80 } |
84 } | 81 } |
85 | 82 |
86 private: | 83 private: |
87 Semaphore* const semaphore_; | 84 Semaphore* const semaphore_; |
88 }; | 85 }; |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
136 semaphore.Signal(); | 133 semaphore.Signal(); |
137 ASSERT_TRUE(semaphore.WaitFor(TimeDelta::FromMicroseconds(0))); | 134 ASSERT_TRUE(semaphore.WaitFor(TimeDelta::FromMicroseconds(0))); |
138 semaphore.Signal(); | 135 semaphore.Signal(); |
139 ASSERT_TRUE(semaphore.WaitFor(TimeDelta::FromMicroseconds(100))); | 136 ASSERT_TRUE(semaphore.WaitFor(TimeDelta::FromMicroseconds(100))); |
140 semaphore.Signal(); | 137 semaphore.Signal(); |
141 ASSERT_TRUE(semaphore.WaitFor(TimeDelta::FromMicroseconds(1000))); | 138 ASSERT_TRUE(semaphore.WaitFor(TimeDelta::FromMicroseconds(1000))); |
142 } | 139 } |
143 | 140 |
144 } // namespace base | 141 } // namespace base |
145 } // namespace v8 | 142 } // namespace v8 |
OLD | NEW |