| OLD | NEW |
| 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 18 matching lines...) Expand all Loading... |
| 29 | 29 |
| 30 #include "v8.h" | 30 #include "v8.h" |
| 31 | 31 |
| 32 #include "platform.h" | 32 #include "platform.h" |
| 33 #include "cctest.h" | 33 #include "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 { |
| 40 public: |
| 41 explicit WaitAndSignalThread(Semaphore* semaphore) |
| 42 : Thread("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 = semaphore_->WaitFor(TimeDelta::FromMicroseconds(1)); |
| 49 ASSERT(!result); |
| 50 USE(result); |
| 51 semaphore_->Signal(); |
| 52 } |
| 53 } |
| 54 |
| 55 private: |
| 56 Semaphore* semaphore_; |
| 57 }; |
| 58 |
| 59 |
| 39 TEST(WaitAndSignal) { | 60 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 Semaphore semaphore(0); |
| 61 WaitAndSignalThread t1(&semaphore); | 62 WaitAndSignalThread t1(&semaphore); |
| 62 WaitAndSignalThread t2(&semaphore); | 63 WaitAndSignalThread t2(&semaphore); |
| 63 | 64 |
| 64 t1.Start(); | 65 t1.Start(); |
| 65 t2.Start(); | 66 t2.Start(); |
| 66 | 67 |
| 67 // Make something available. | 68 // Make something available. |
| 68 semaphore.Signal(); | 69 semaphore.Signal(); |
| 69 | 70 |
| (...skipping 16 matching lines...) Expand all Loading... |
| 86 ok = semaphore.WaitFor(TimeDelta::FromMicroseconds(0)); | 87 ok = semaphore.WaitFor(TimeDelta::FromMicroseconds(0)); |
| 87 CHECK(!ok); | 88 CHECK(!ok); |
| 88 ok = semaphore.WaitFor(TimeDelta::FromMicroseconds(100)); | 89 ok = semaphore.WaitFor(TimeDelta::FromMicroseconds(100)); |
| 89 CHECK(!ok); | 90 CHECK(!ok); |
| 90 ok = semaphore.WaitFor(TimeDelta::FromMicroseconds(1000)); | 91 ok = semaphore.WaitFor(TimeDelta::FromMicroseconds(1000)); |
| 91 CHECK(!ok); | 92 CHECK(!ok); |
| 92 | 93 |
| 93 // Semaphore signalled - no timeout. | 94 // Semaphore signalled - no timeout. |
| 94 semaphore.Signal(); | 95 semaphore.Signal(); |
| 95 ok = semaphore.WaitFor(TimeDelta::FromMicroseconds(0)); | 96 ok = semaphore.WaitFor(TimeDelta::FromMicroseconds(0)); |
| 97 CHECK(ok); |
| 96 semaphore.Signal(); | 98 semaphore.Signal(); |
| 97 ok = semaphore.WaitFor(TimeDelta::FromMicroseconds(100)); | 99 ok = semaphore.WaitFor(TimeDelta::FromMicroseconds(100)); |
| 100 CHECK(ok); |
| 98 semaphore.Signal(); | 101 semaphore.Signal(); |
| 99 ok = semaphore.WaitFor(TimeDelta::FromMicroseconds(1000)); | 102 ok = semaphore.WaitFor(TimeDelta::FromMicroseconds(1000)); |
| 100 CHECK(ok); | 103 CHECK(ok); |
| 101 } | 104 } |
| 102 | 105 |
| 103 | 106 |
| 104 static const char alphabet[] = "XKOAD"; | 107 static const char alphabet[] = "XKOAD"; |
| 105 static const int kAlphabetSize = sizeof(alphabet) - 1; | 108 static const int kAlphabetSize = sizeof(alphabet) - 1; |
| 106 static const int kBufferSize = 4096; // GCD(buffer size, alphabet size) = 1 | 109 static const int kBufferSize = 4096; // GCD(buffer size, alphabet size) = 1 |
| 107 static char buffer[kBufferSize]; | 110 static char buffer[kBufferSize]; |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 143 | 146 |
| 144 | 147 |
| 145 TEST(ProducerConsumer) { | 148 TEST(ProducerConsumer) { |
| 146 ProducerThread producer_thread; | 149 ProducerThread producer_thread; |
| 147 ConsumerThread consumer_thread; | 150 ConsumerThread consumer_thread; |
| 148 producer_thread.Start(); | 151 producer_thread.Start(); |
| 149 consumer_thread.Start(); | 152 consumer_thread.Start(); |
| 150 producer_thread.Join(); | 153 producer_thread.Join(); |
| 151 consumer_thread.Join(); | 154 consumer_thread.Join(); |
| 152 } | 155 } |
| OLD | NEW |