| 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 "v8.h" | 
|  | 29 | 
|  | 30 #include "cctest.h" | 
|  | 31 #include "platform/condition-variable.h" | 
|  | 32 #include "platform/time.h" | 
|  | 33 | 
|  | 34 using namespace ::v8::internal; | 
|  | 35 | 
|  | 36 | 
|  | 37 TEST(WaitForAfterNofityOnSameThread) { | 
|  | 38   for (int n = 0; n < 10; ++n) { | 
|  | 39     Mutex mutex; | 
|  | 40     ConditionVariable cv; | 
|  | 41 | 
|  | 42     LockGuard<Mutex> lock_guard(&mutex); | 
|  | 43 | 
|  | 44     cv.NotifyOne(); | 
|  | 45     CHECK_EQ(false, cv.WaitFor(&mutex, TimeDelta::FromMicroseconds(n))); | 
|  | 46 | 
|  | 47     cv.NotifyAll(); | 
|  | 48     CHECK_EQ(false, cv.WaitFor(&mutex, TimeDelta::FromMicroseconds(n))); | 
|  | 49   } | 
|  | 50 } | 
|  | 51 | 
|  | 52 | 
|  | 53 class ThreadWithMutexAndConditionVariable V8_FINAL : public Thread { | 
|  | 54  public: | 
|  | 55   ThreadWithMutexAndConditionVariable() | 
|  | 56       : Thread("ThreadWithMutexAndConditionVariable"), | 
|  | 57         running_(false), finished_(false) {} | 
|  | 58   virtual ~ThreadWithMutexAndConditionVariable() {} | 
|  | 59 | 
|  | 60   virtual void Run() V8_OVERRIDE { | 
|  | 61     LockGuard<Mutex> lock_guard(&mutex_); | 
|  | 62     running_ = true; | 
|  | 63     cv_.NotifyOne(); | 
|  | 64     cv_.Wait(&mutex_); | 
|  | 65     running_ = false; | 
|  | 66     finished_ = true; | 
|  | 67     cv_.NotifyOne(); | 
|  | 68   } | 
|  | 69 | 
|  | 70   volatile bool running_; | 
|  | 71   volatile bool finished_; | 
|  | 72   ConditionVariable cv_; | 
|  | 73   Mutex mutex_; | 
|  | 74 }; | 
|  | 75 | 
|  | 76 | 
|  | 77 TEST(MultipleThreadsWithSeparateConditionVariables) { | 
|  | 78   static const int kThreadCount = 16; | 
|  | 79   static const TimeDelta kMaxThreadStartTime = | 
|  | 80       TimeDelta::FromMilliseconds(250) * kThreadCount; | 
|  | 81   ThreadWithMutexAndConditionVariable threads[kThreadCount]; | 
|  | 82 | 
|  | 83   for (int n = 0; n < kThreadCount; ++n) { | 
|  | 84     LockGuard<Mutex> lock_guard(&threads[n].mutex_); | 
|  | 85     CHECK(!threads[n].running_); | 
|  | 86     CHECK(!threads[n].finished_); | 
|  | 87     threads[n].Start(); | 
|  | 88     // Wait for nth thread to start. | 
|  | 89     CHECK(threads[n].cv_.WaitFor(&threads[n].mutex_, kMaxThreadStartTime)); | 
|  | 90   } | 
|  | 91 | 
|  | 92   for (int n = kThreadCount - 1; n >= 0; --n) { | 
|  | 93     LockGuard<Mutex> lock_guard(&threads[n].mutex_); | 
|  | 94     CHECK(threads[n].running_); | 
|  | 95     CHECK(!threads[n].finished_); | 
|  | 96   } | 
|  | 97 | 
|  | 98   for (int n = 0; n < kThreadCount; ++n) { | 
|  | 99     threads[n].cv_.NotifyOne(); | 
|  | 100   } | 
|  | 101 | 
|  | 102   for (int n = kThreadCount - 1; n >= 0; --n) { | 
|  | 103     // Wait for nth thread to quit. | 
|  | 104     threads[n].Join(); | 
|  | 105     LockGuard<Mutex> lock_guard(&threads[n].mutex_); | 
|  | 106     CHECK(!threads[n].running_); | 
|  | 107     CHECK(threads[n].finished_); | 
|  | 108   } | 
|  | 109 } | 
|  | 110 | 
|  | 111 | 
|  | 112 static int loop_counter = 0; | 
|  | 113 static const int kLoopCounterLimit = 100; | 
|  | 114 | 
|  | 115 class LoopIncrementThread V8_FINAL : public Thread { | 
|  | 116  public: | 
|  | 117   LoopIncrementThread(const char* name, | 
|  | 118                       int rem, | 
|  | 119                       ConditionVariable* cv, | 
|  | 120                       Mutex* mutex) | 
|  | 121       : Thread(name), rem_(rem), cv_(cv), mutex_(mutex) {} | 
|  | 122   virtual ~LoopIncrementThread() {} | 
|  | 123 | 
|  | 124   virtual void Run() V8_OVERRIDE { | 
|  | 125     int last_count = -1; | 
|  | 126     while (true) { | 
|  | 127       LockGuard<Mutex> lock_guard(mutex_); | 
|  | 128       int count = loop_counter; | 
|  | 129       while (count % 2 != rem_ && count < kLoopCounterLimit) { | 
|  | 130         cv_->Wait(mutex_); | 
|  | 131         count = loop_counter; | 
|  | 132       } | 
|  | 133       if (count >= kLoopCounterLimit) break; | 
|  | 134       CHECK_EQ(loop_counter, count); | 
|  | 135       if (last_count != -1) { | 
|  | 136         CHECK_EQ(last_count + 1, count); | 
|  | 137       } | 
|  | 138       count++; | 
|  | 139       loop_counter = count; | 
|  | 140       last_count = count; | 
|  | 141       cv_->NotifyOne(); | 
|  | 142     } | 
|  | 143   } | 
|  | 144 | 
|  | 145  private: | 
|  | 146   const int rem_; | 
|  | 147   ConditionVariable* cv_; | 
|  | 148   Mutex* mutex_; | 
|  | 149 }; | 
|  | 150 | 
|  | 151 | 
|  | 152 TEST(LoopIncrement) { | 
|  | 153   Mutex mutex; | 
|  | 154   ConditionVariable cv; | 
|  | 155   LoopIncrementThread t0("t0", 0, &cv, &mutex); | 
|  | 156   LoopIncrementThread t1("t1", 1, &cv, &mutex); | 
|  | 157   t0.Start(); | 
|  | 158   t1.Start(); | 
|  | 159   t0.Join(); | 
|  | 160   t1.Join(); | 
|  | 161   CHECK_EQ(kLoopCounterLimit, loop_counter); | 
|  | 162 } | 
| OLD | NEW | 
|---|