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

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

Issue 358363002: Move platform abstraction to base library (Closed) Base URL: https://v8.googlecode.com/svn/branches/bleeding_edge
Patch Set: updates Created 6 years, 5 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
OLDNEW
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 11 matching lines...) Expand all
22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 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. 26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 27
28 #include <stdlib.h> 28 #include <stdlib.h>
29 29
30 #include "src/v8.h" 30 #include "src/v8.h"
31 31
32 #include "src/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 Thread { 39 class WaitAndSignalThread V8_FINAL : public v8::base::Thread {
40 public: 40 public:
41 explicit WaitAndSignalThread(Semaphore* semaphore) 41 explicit WaitAndSignalThread(v8::base::Semaphore* semaphore)
42 : Thread("WaitAndSignalThread"), semaphore_(semaphore) {} 42 : Thread("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 = semaphore_->WaitFor(TimeDelta::FromMicroseconds(1)); 48 bool result =
49 semaphore_->WaitFor(v8::base::TimeDelta::FromMicroseconds(1));
49 ASSERT(!result); 50 ASSERT(!result);
50 USE(result); 51 USE(result);
51 semaphore_->Signal(); 52 semaphore_->Signal();
52 } 53 }
53 } 54 }
54 55
55 private: 56 private:
56 Semaphore* semaphore_; 57 v8::base::Semaphore* semaphore_;
57 }; 58 };
58 59
59 60
60 TEST(WaitAndSignal) { 61 TEST(WaitAndSignal) {
61 Semaphore semaphore(0); 62 v8::base::Semaphore semaphore(0);
62 WaitAndSignalThread t1(&semaphore); 63 WaitAndSignalThread t1(&semaphore);
63 WaitAndSignalThread t2(&semaphore); 64 WaitAndSignalThread t2(&semaphore);
64 65
65 t1.Start(); 66 t1.Start();
66 t2.Start(); 67 t2.Start();
67 68
68 // Make something available. 69 // Make something available.
69 semaphore.Signal(); 70 semaphore.Signal();
70 71
71 t1.Join(); 72 t1.Join();
72 t2.Join(); 73 t2.Join();
73 74
74 semaphore.Wait(); 75 semaphore.Wait();
75 76
76 bool result = semaphore.WaitFor(TimeDelta::FromMicroseconds(1)); 77 bool result = semaphore.WaitFor(v8::base::TimeDelta::FromMicroseconds(1));
77 ASSERT(!result); 78 ASSERT(!result);
78 USE(result); 79 USE(result);
79 } 80 }
80 81
81 82
82 TEST(WaitFor) { 83 TEST(WaitFor) {
83 bool ok; 84 bool ok;
84 Semaphore semaphore(0); 85 v8::base::Semaphore semaphore(0);
85 86
86 // Semaphore not signalled - timeout. 87 // Semaphore not signalled - timeout.
87 ok = semaphore.WaitFor(TimeDelta::FromMicroseconds(0)); 88 ok = semaphore.WaitFor(v8::base::TimeDelta::FromMicroseconds(0));
88 CHECK(!ok); 89 CHECK(!ok);
89 ok = semaphore.WaitFor(TimeDelta::FromMicroseconds(100)); 90 ok = semaphore.WaitFor(v8::base::TimeDelta::FromMicroseconds(100));
90 CHECK(!ok); 91 CHECK(!ok);
91 ok = semaphore.WaitFor(TimeDelta::FromMicroseconds(1000)); 92 ok = semaphore.WaitFor(v8::base::TimeDelta::FromMicroseconds(1000));
92 CHECK(!ok); 93 CHECK(!ok);
93 94
94 // Semaphore signalled - no timeout. 95 // Semaphore signalled - no timeout.
95 semaphore.Signal(); 96 semaphore.Signal();
96 ok = semaphore.WaitFor(TimeDelta::FromMicroseconds(0)); 97 ok = semaphore.WaitFor(v8::base::TimeDelta::FromMicroseconds(0));
97 CHECK(ok); 98 CHECK(ok);
98 semaphore.Signal(); 99 semaphore.Signal();
99 ok = semaphore.WaitFor(TimeDelta::FromMicroseconds(100)); 100 ok = semaphore.WaitFor(v8::base::TimeDelta::FromMicroseconds(100));
100 CHECK(ok); 101 CHECK(ok);
101 semaphore.Signal(); 102 semaphore.Signal();
102 ok = semaphore.WaitFor(TimeDelta::FromMicroseconds(1000)); 103 ok = semaphore.WaitFor(v8::base::TimeDelta::FromMicroseconds(1000));
103 CHECK(ok); 104 CHECK(ok);
104 } 105 }
105 106
106 107
107 static const char alphabet[] = "XKOAD"; 108 static const char alphabet[] = "XKOAD";
108 static const int kAlphabetSize = sizeof(alphabet) - 1; 109 static const int kAlphabetSize = sizeof(alphabet) - 1;
109 static const int kBufferSize = 4096; // GCD(buffer size, alphabet size) = 1 110 static const int kBufferSize = 4096; // GCD(buffer size, alphabet size) = 1
110 static char buffer[kBufferSize]; 111 static char buffer[kBufferSize];
111 static const int kDataSize = kBufferSize * kAlphabetSize * 10; 112 static const int kDataSize = kBufferSize * kAlphabetSize * 10;
112 113
113 static Semaphore free_space(kBufferSize); 114 static v8::base::Semaphore free_space(kBufferSize);
114 static Semaphore used_space(0); 115 static v8::base::Semaphore used_space(0);
115 116
116 117
117 class ProducerThread V8_FINAL : public Thread { 118 class ProducerThread V8_FINAL : public v8::base::Thread {
118 public: 119 public:
119 ProducerThread() : Thread("ProducerThread") {} 120 ProducerThread() : Thread("ProducerThread") {}
120 virtual ~ProducerThread() {} 121 virtual ~ProducerThread() {}
121 122
122 virtual void Run() V8_OVERRIDE { 123 virtual void Run() V8_OVERRIDE {
123 for (int n = 0; n < kDataSize; ++n) { 124 for (int n = 0; n < kDataSize; ++n) {
124 free_space.Wait(); 125 free_space.Wait();
125 buffer[n % kBufferSize] = alphabet[n % kAlphabetSize]; 126 buffer[n % kBufferSize] = alphabet[n % kAlphabetSize];
126 used_space.Signal(); 127 used_space.Signal();
127 } 128 }
128 } 129 }
129 }; 130 };
130 131
131 132
132 class ConsumerThread V8_FINAL : public Thread { 133 class ConsumerThread V8_FINAL : public v8::base::Thread {
133 public: 134 public:
134 ConsumerThread() : Thread("ConsumerThread") {} 135 ConsumerThread() : Thread("ConsumerThread") {}
135 virtual ~ConsumerThread() {} 136 virtual ~ConsumerThread() {}
136 137
137 virtual void Run() V8_OVERRIDE { 138 virtual void Run() V8_OVERRIDE {
138 for (int n = 0; n < kDataSize; ++n) { 139 for (int n = 0; n < kDataSize; ++n) {
139 used_space.Wait(); 140 used_space.Wait();
140 ASSERT_EQ(static_cast<int>(alphabet[n % kAlphabetSize]), 141 ASSERT_EQ(static_cast<int>(alphabet[n % kAlphabetSize]),
141 static_cast<int>(buffer[n % kBufferSize])); 142 static_cast<int>(buffer[n % kBufferSize]));
142 free_space.Signal(); 143 free_space.Signal();
143 } 144 }
144 } 145 }
145 }; 146 };
146 147
147 148
148 TEST(ProducerConsumer) { 149 TEST(ProducerConsumer) {
149 ProducerThread producer_thread; 150 ProducerThread producer_thread;
150 ConsumerThread consumer_thread; 151 ConsumerThread consumer_thread;
151 producer_thread.Start(); 152 producer_thread.Start();
152 consumer_thread.Start(); 153 consumer_thread.Start();
153 producer_thread.Join(); 154 producer_thread.Join();
154 consumer_thread.Join(); 155 consumer_thread.Join();
155 } 156 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698