Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1)

Side by Side Diff: test/cctest/test-semaphore.cc

Issue 23748003: Cleanup Semaphore class. (Closed) Base URL: https://v8.googlecode.com/svn/branches/bleeding_edge
Patch Set: Damn broken Rietveld! Created 7 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « test/cctest/test-log.cc ('k') | test/cctest/test-sockets.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
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 // Tests of the TokenLock class from lock.h
Michael Starzinger 2013/09/02 11:45:31 nit: This comment seems outdated, let's drop it.
Benedikt Meurer 2013/09/02 12:04:31 Done.
29
30 #include <stdlib.h>
31
32 #include "v8.h"
33
34 #include "platform.h"
35 #include "cctest.h"
36
37
38 using namespace ::v8::internal;
39
40
41 TEST(WaitAndSignal) {
42 class WaitAndSignalThread V8_FINAL : public Thread {
43 public:
44 explicit WaitAndSignalThread(Semaphore* semaphore)
45 : Thread("WaitAndSignalThread"), semaphore_(semaphore) {}
46 virtual ~WaitAndSignalThread() {}
47
48 virtual void Run() V8_OVERRIDE {
49 for (int n = 0; n < 1000; ++n) {
50 semaphore_->Wait();
51 bool result = semaphore_->WaitFor(TimeDelta::FromMicroseconds(1));
52 ASSERT(!result);
53 USE(result);
54 semaphore_->Signal();
55 }
56 }
57
58 private:
59 Semaphore* semaphore_;
60 };
61
62 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(TimeDelta::FromMicroseconds(1));
78 ASSERT(!result);
79 USE(result);
80 }
81
82
83 TEST(WaitFor) {
84 bool ok;
85 Semaphore semaphore(0);
86
87 // Semaphore not signalled - timeout.
88 ok = semaphore.WaitFor(TimeDelta::FromMicroseconds(0));
89 CHECK(!ok);
90 ok = semaphore.WaitFor(TimeDelta::FromMicroseconds(100));
91 CHECK(!ok);
92 ok = semaphore.WaitFor(TimeDelta::FromMicroseconds(1000));
93 CHECK(!ok);
94
95 // Semaphore signalled - no timeout.
96 semaphore.Signal();
97 ok = semaphore.WaitFor(TimeDelta::FromMicroseconds(0));
98 semaphore.Signal();
99 ok = semaphore.WaitFor(TimeDelta::FromMicroseconds(100));
100 semaphore.Signal();
101 ok = semaphore.WaitFor(TimeDelta::FromMicroseconds(1000));
102 CHECK(ok);
103 }
104
105
106 static const char alphabet[] = "XKOAD";
107 static const int kAlphabetSize = sizeof(alphabet) - 1;
108 static const int kBufferSize = 4096; // GCD(buffer size, alphabet size) = 1
109 static char buffer[kBufferSize];
110 static const int kDataSize = kBufferSize * kAlphabetSize * 10;
111
112 static Semaphore free_space(kBufferSize);
113 static Semaphore used_space(0);
114
115
116 class ProducerThread V8_FINAL : public Thread {
117 public:
118 ProducerThread() : Thread("ProducerThread") {}
119 virtual ~ProducerThread() {}
120
121 virtual void Run() V8_OVERRIDE {
122 for (int n = 0; n < kDataSize; ++n) {
123 free_space.Wait();
124 buffer[n % kBufferSize] = alphabet[n % kAlphabetSize];
125 used_space.Signal();
126 }
127 }
128 };
129
130
131 class ConsumerThread V8_FINAL : public Thread {
132 public:
133 ConsumerThread() : Thread("ConsumerThread") {}
134 virtual ~ConsumerThread() {}
135
136 virtual void Run() V8_OVERRIDE {
137 for (int n = 0; n < kDataSize; ++n) {
138 used_space.Wait();
139 ASSERT_EQ(static_cast<int>(alphabet[n % kAlphabetSize]),
140 static_cast<int>(buffer[n % kBufferSize]));
141 free_space.Signal();
142 }
143 }
144 };
145
146
147 TEST(ProducerConsumer) {
148 ProducerThread producer_thread;
149 ConsumerThread consumer_thread;
150 producer_thread.Start();
151 consumer_thread.Start();
152 producer_thread.Join();
153 consumer_thread.Join();
154 }
OLDNEW
« no previous file with comments | « test/cctest/test-log.cc ('k') | test/cctest/test-sockets.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698