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