Upstream version 10.38.222.0
[platform/framework/web/crosswalk.git] / src / v8 / test / cctest / test-semaphore.cc
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
4 // met:
5 //
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.
15 //
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.
27
28 #include <stdlib.h>
29
30 #include "src/v8.h"
31
32 #include "src/base/platform/platform.h"
33 #include "test/cctest/cctest.h"
34
35
36 using namespace ::v8::internal;
37
38
39 class WaitAndSignalThread V8_FINAL : public v8::base::Thread {
40  public:
41   explicit WaitAndSignalThread(v8::base::Semaphore* semaphore)
42       : Thread(Options("WaitAndSignalThread")), semaphore_(semaphore) {}
43   virtual ~WaitAndSignalThread() {}
44
45   virtual void Run() V8_OVERRIDE {
46     for (int n = 0; n < 1000; ++n) {
47       semaphore_->Wait();
48       bool result =
49           semaphore_->WaitFor(v8::base::TimeDelta::FromMicroseconds(1));
50       DCHECK(!result);
51       USE(result);
52       semaphore_->Signal();
53     }
54   }
55
56  private:
57   v8::base::Semaphore* semaphore_;
58 };
59
60
61 TEST(WaitAndSignal) {
62   v8::base::Semaphore semaphore(0);
63   WaitAndSignalThread t1(&semaphore);
64   WaitAndSignalThread t2(&semaphore);
65
66   t1.Start();
67   t2.Start();
68
69   // Make something available.
70   semaphore.Signal();
71
72   t1.Join();
73   t2.Join();
74
75   semaphore.Wait();
76
77   bool result = semaphore.WaitFor(v8::base::TimeDelta::FromMicroseconds(1));
78   DCHECK(!result);
79   USE(result);
80 }
81
82
83 TEST(WaitFor) {
84   bool ok;
85   v8::base::Semaphore semaphore(0);
86
87   // Semaphore not signalled - timeout.
88   ok = semaphore.WaitFor(v8::base::TimeDelta::FromMicroseconds(0));
89   CHECK(!ok);
90   ok = semaphore.WaitFor(v8::base::TimeDelta::FromMicroseconds(100));
91   CHECK(!ok);
92   ok = semaphore.WaitFor(v8::base::TimeDelta::FromMicroseconds(1000));
93   CHECK(!ok);
94
95   // Semaphore signalled - no timeout.
96   semaphore.Signal();
97   ok = semaphore.WaitFor(v8::base::TimeDelta::FromMicroseconds(0));
98   CHECK(ok);
99   semaphore.Signal();
100   ok = semaphore.WaitFor(v8::base::TimeDelta::FromMicroseconds(100));
101   CHECK(ok);
102   semaphore.Signal();
103   ok = semaphore.WaitFor(v8::base::TimeDelta::FromMicroseconds(1000));
104   CHECK(ok);
105 }
106
107
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;
113
114 static v8::base::Semaphore free_space(kBufferSize);
115 static v8::base::Semaphore used_space(0);
116
117
118 class ProducerThread V8_FINAL : public v8::base::Thread {
119  public:
120   ProducerThread() : Thread(Options("ProducerThread")) {}
121   virtual ~ProducerThread() {}
122
123   virtual void Run() V8_OVERRIDE {
124     for (int n = 0; n < kDataSize; ++n) {
125       free_space.Wait();
126       buffer[n % kBufferSize] = alphabet[n % kAlphabetSize];
127       used_space.Signal();
128     }
129   }
130 };
131
132
133 class ConsumerThread V8_FINAL : public v8::base::Thread {
134  public:
135   ConsumerThread() : Thread(Options("ConsumerThread")) {}
136   virtual ~ConsumerThread() {}
137
138   virtual void Run() V8_OVERRIDE {
139     for (int n = 0; n < kDataSize; ++n) {
140       used_space.Wait();
141       DCHECK_EQ(static_cast<int>(alphabet[n % kAlphabetSize]),
142                 static_cast<int>(buffer[n % kBufferSize]));
143       free_space.Signal();
144     }
145   }
146 };
147
148
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();
156 }