1 // Copyright 2013 the V8 project authors. All rights reserved.
2 // Redistribution and use in source and binary forms, with or without
3 // modification, are permitted provided that the following conditions are
6 // * Redistributions of source code must retain the above copyright
7 // notice, this list of conditions and the following disclaimer.
8 // * Redistributions in binary form must reproduce the above
9 // copyright notice, this list of conditions and the following
10 // disclaimer in the documentation and/or other materials provided
11 // with the distribution.
12 // * Neither the name of Google Inc. nor the names of its
13 // contributors may be used to endorse or promote products derived
14 // from this software without specific prior written permission.
16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 #include "src/base/platform/platform.h"
33 #include "test/cctest/cctest.h"
36 using namespace ::v8::internal;
39 class WaitAndSignalThread V8_FINAL : public v8::base::Thread {
41 explicit WaitAndSignalThread(v8::base::Semaphore* semaphore)
42 : Thread(Options("WaitAndSignalThread")), semaphore_(semaphore) {}
43 virtual ~WaitAndSignalThread() {}
45 virtual void Run() V8_OVERRIDE {
46 for (int n = 0; n < 1000; ++n) {
49 semaphore_->WaitFor(v8::base::TimeDelta::FromMicroseconds(1));
57 v8::base::Semaphore* semaphore_;
62 v8::base::Semaphore semaphore(0);
63 WaitAndSignalThread t1(&semaphore);
64 WaitAndSignalThread t2(&semaphore);
69 // Make something available.
77 bool result = semaphore.WaitFor(v8::base::TimeDelta::FromMicroseconds(1));
85 v8::base::Semaphore semaphore(0);
87 // Semaphore not signalled - timeout.
88 ok = semaphore.WaitFor(v8::base::TimeDelta::FromMicroseconds(0));
90 ok = semaphore.WaitFor(v8::base::TimeDelta::FromMicroseconds(100));
92 ok = semaphore.WaitFor(v8::base::TimeDelta::FromMicroseconds(1000));
95 // Semaphore signalled - no timeout.
97 ok = semaphore.WaitFor(v8::base::TimeDelta::FromMicroseconds(0));
100 ok = semaphore.WaitFor(v8::base::TimeDelta::FromMicroseconds(100));
103 ok = semaphore.WaitFor(v8::base::TimeDelta::FromMicroseconds(1000));
108 static const char alphabet[] = "XKOAD";
109 static const int kAlphabetSize = sizeof(alphabet) - 1;
110 static const int kBufferSize = 4096; // GCD(buffer size, alphabet size) = 1
111 static char buffer[kBufferSize];
112 static const int kDataSize = kBufferSize * kAlphabetSize * 10;
114 static v8::base::Semaphore free_space(kBufferSize);
115 static v8::base::Semaphore used_space(0);
118 class ProducerThread V8_FINAL : public v8::base::Thread {
120 ProducerThread() : Thread(Options("ProducerThread")) {}
121 virtual ~ProducerThread() {}
123 virtual void Run() V8_OVERRIDE {
124 for (int n = 0; n < kDataSize; ++n) {
126 buffer[n % kBufferSize] = alphabet[n % kAlphabetSize];
133 class ConsumerThread V8_FINAL : public v8::base::Thread {
135 ConsumerThread() : Thread(Options("ConsumerThread")) {}
136 virtual ~ConsumerThread() {}
138 virtual void Run() V8_OVERRIDE {
139 for (int n = 0; n < kDataSize; ++n) {
141 DCHECK_EQ(static_cast<int>(alphabet[n % kAlphabetSize]),
142 static_cast<int>(buffer[n % kBufferSize]));
149 TEST(ProducerConsumer) {
150 ProducerThread producer_thread;
151 ConsumerThread consumer_thread;
152 producer_thread.Start();
153 consumer_thread.Start();
154 producer_thread.Join();
155 consumer_thread.Join();