OLD | NEW |
1 // Copyright 2013 the V8 project authors. All rights reserved. | 1 // Copyright 2013 the V8 project authors. All rights reserved. |
2 // Redistribution and use in source and binary forms, with or without | 2 // Redistribution and use in source and binary forms, with or without |
3 // modification, are permitted provided that the following conditions are | 3 // modification, are permitted provided that the following conditions are |
4 // met: | 4 // met: |
5 // | 5 // |
6 // * Redistributions of source code must retain the above copyright | 6 // * Redistributions of source code must retain the above copyright |
7 // notice, this list of conditions and the following disclaimer. | 7 // notice, this list of conditions and the following disclaimer. |
8 // * Redistributions in binary form must reproduce the above | 8 // * Redistributions in binary form must reproduce the above |
9 // copyright notice, this list of conditions and the following | 9 // copyright notice, this list of conditions and the following |
10 // disclaimer in the documentation and/or other materials provided | 10 // disclaimer in the documentation and/or other materials provided |
(...skipping 21 matching lines...) Expand all Loading... |
32 #include "src/base/platform/platform.h" | 32 #include "src/base/platform/platform.h" |
33 #include "test/cctest/cctest.h" | 33 #include "test/cctest/cctest.h" |
34 | 34 |
35 | 35 |
36 using namespace ::v8::internal; | 36 using namespace ::v8::internal; |
37 | 37 |
38 | 38 |
39 class WaitAndSignalThread V8_FINAL : public v8::base::Thread { | 39 class WaitAndSignalThread V8_FINAL : public v8::base::Thread { |
40 public: | 40 public: |
41 explicit WaitAndSignalThread(v8::base::Semaphore* semaphore) | 41 explicit WaitAndSignalThread(v8::base::Semaphore* semaphore) |
42 : Thread("WaitAndSignalThread"), semaphore_(semaphore) {} | 42 : Thread(Options("WaitAndSignalThread")), semaphore_(semaphore) {} |
43 virtual ~WaitAndSignalThread() {} | 43 virtual ~WaitAndSignalThread() {} |
44 | 44 |
45 virtual void Run() V8_OVERRIDE { | 45 virtual void Run() V8_OVERRIDE { |
46 for (int n = 0; n < 1000; ++n) { | 46 for (int n = 0; n < 1000; ++n) { |
47 semaphore_->Wait(); | 47 semaphore_->Wait(); |
48 bool result = | 48 bool result = |
49 semaphore_->WaitFor(v8::base::TimeDelta::FromMicroseconds(1)); | 49 semaphore_->WaitFor(v8::base::TimeDelta::FromMicroseconds(1)); |
50 DCHECK(!result); | 50 DCHECK(!result); |
51 USE(result); | 51 USE(result); |
52 semaphore_->Signal(); | 52 semaphore_->Signal(); |
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
110 static const int kBufferSize = 4096; // GCD(buffer size, alphabet size) = 1 | 110 static const int kBufferSize = 4096; // GCD(buffer size, alphabet size) = 1 |
111 static char buffer[kBufferSize]; | 111 static char buffer[kBufferSize]; |
112 static const int kDataSize = kBufferSize * kAlphabetSize * 10; | 112 static const int kDataSize = kBufferSize * kAlphabetSize * 10; |
113 | 113 |
114 static v8::base::Semaphore free_space(kBufferSize); | 114 static v8::base::Semaphore free_space(kBufferSize); |
115 static v8::base::Semaphore used_space(0); | 115 static v8::base::Semaphore used_space(0); |
116 | 116 |
117 | 117 |
118 class ProducerThread V8_FINAL : public v8::base::Thread { | 118 class ProducerThread V8_FINAL : public v8::base::Thread { |
119 public: | 119 public: |
120 ProducerThread() : Thread("ProducerThread") {} | 120 ProducerThread() : Thread(Options("ProducerThread")) {} |
121 virtual ~ProducerThread() {} | 121 virtual ~ProducerThread() {} |
122 | 122 |
123 virtual void Run() V8_OVERRIDE { | 123 virtual void Run() V8_OVERRIDE { |
124 for (int n = 0; n < kDataSize; ++n) { | 124 for (int n = 0; n < kDataSize; ++n) { |
125 free_space.Wait(); | 125 free_space.Wait(); |
126 buffer[n % kBufferSize] = alphabet[n % kAlphabetSize]; | 126 buffer[n % kBufferSize] = alphabet[n % kAlphabetSize]; |
127 used_space.Signal(); | 127 used_space.Signal(); |
128 } | 128 } |
129 } | 129 } |
130 }; | 130 }; |
131 | 131 |
132 | 132 |
133 class ConsumerThread V8_FINAL : public v8::base::Thread { | 133 class ConsumerThread V8_FINAL : public v8::base::Thread { |
134 public: | 134 public: |
135 ConsumerThread() : Thread("ConsumerThread") {} | 135 ConsumerThread() : Thread(Options("ConsumerThread")) {} |
136 virtual ~ConsumerThread() {} | 136 virtual ~ConsumerThread() {} |
137 | 137 |
138 virtual void Run() V8_OVERRIDE { | 138 virtual void Run() V8_OVERRIDE { |
139 for (int n = 0; n < kDataSize; ++n) { | 139 for (int n = 0; n < kDataSize; ++n) { |
140 used_space.Wait(); | 140 used_space.Wait(); |
141 DCHECK_EQ(static_cast<int>(alphabet[n % kAlphabetSize]), | 141 DCHECK_EQ(static_cast<int>(alphabet[n % kAlphabetSize]), |
142 static_cast<int>(buffer[n % kBufferSize])); | 142 static_cast<int>(buffer[n % kBufferSize])); |
143 free_space.Signal(); | 143 free_space.Signal(); |
144 } | 144 } |
145 } | 145 } |
146 }; | 146 }; |
147 | 147 |
148 | 148 |
149 TEST(ProducerConsumer) { | 149 TEST(ProducerConsumer) { |
150 ProducerThread producer_thread; | 150 ProducerThread producer_thread; |
151 ConsumerThread consumer_thread; | 151 ConsumerThread consumer_thread; |
152 producer_thread.Start(); | 152 producer_thread.Start(); |
153 consumer_thread.Start(); | 153 consumer_thread.Start(); |
154 producer_thread.Join(); | 154 producer_thread.Join(); |
155 consumer_thread.Join(); | 155 consumer_thread.Join(); |
156 } | 156 } |
OLD | NEW |