| OLD | NEW |
| 1 // Copyright 2008 the V8 project authors. All rights reserved. | 1 // Copyright 2008 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 10 matching lines...) Expand all Loading... |
| 21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | 21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 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 "v8.h" | 28 #include "v8.h" |
| 29 | 29 |
| 30 #include "platform.h" | 30 #include "platform.h" |
| 31 #include "isolate.h" |
| 31 | 32 |
| 32 #include "cctest.h" | 33 #include "cctest.h" |
| 33 | 34 |
| 34 | 35 |
| 35 TEST(Preemption) { | 36 TEST(Preemption) { |
| 36 v8::Locker locker; | 37 v8::Locker locker; |
| 37 v8::V8::Initialize(); | 38 v8::V8::Initialize(); |
| 38 v8::HandleScope scope; | 39 v8::HandleScope scope; |
| 39 v8::Context::Scope context_scope(v8::Context::New()); | 40 v8::Context::Scope context_scope(v8::Context::New()); |
| 40 | 41 |
| (...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 129 ThreadB threadB(i::Isolate::Current()); | 130 ThreadB threadB(i::Isolate::Current()); |
| 130 | 131 |
| 131 threadA.Start(); | 132 threadA.Start(); |
| 132 threadB.Start(); | 133 threadB.Start(); |
| 133 | 134 |
| 134 threadA.Join(); | 135 threadA.Join(); |
| 135 threadB.Join(); | 136 threadB.Join(); |
| 136 | 137 |
| 137 CHECK_EQ(DONE, turn); | 138 CHECK_EQ(DONE, turn); |
| 138 } | 139 } |
| 140 |
| 141 class ThreadIdValidationThread : public v8::internal::Thread { |
| 142 public: |
| 143 ThreadIdValidationThread(i::Thread* thread_to_start, |
| 144 i::List<i::ThreadId>* refs, |
| 145 unsigned int thread_no, |
| 146 i::Semaphore* semaphore) |
| 147 : Thread(NULL, "ThreadRefValidationThread"), |
| 148 refs_(refs), thread_no_(thread_no), thread_to_start_(thread_to_start), |
| 149 semaphore_(semaphore) { |
| 150 } |
| 151 |
| 152 void Run() { |
| 153 i::ThreadId thread_id = i::ThreadId::Current(); |
| 154 for (int i = 0; i < thread_no_; i++) { |
| 155 CHECK(!(*refs_)[i].Equals(thread_id)); |
| 156 } |
| 157 CHECK(thread_id.IsValid()); |
| 158 (*refs_)[thread_no_] = thread_id; |
| 159 if (thread_to_start_ != NULL) { |
| 160 thread_to_start_->Start(); |
| 161 } |
| 162 semaphore_->Signal(); |
| 163 } |
| 164 private: |
| 165 i::List<i::ThreadId>* refs_; |
| 166 int thread_no_; |
| 167 i::Thread* thread_to_start_; |
| 168 i::Semaphore* semaphore_; |
| 169 }; |
| 170 |
| 171 TEST(ThreadIdValidation) { |
| 172 const int kNThreads = 100; |
| 173 i::List<ThreadIdValidationThread*> threads(kNThreads); |
| 174 i::List<i::ThreadId> refs(kNThreads); |
| 175 i::Semaphore* semaphore = i::OS::CreateSemaphore(0); |
| 176 ThreadIdValidationThread* prev = NULL; |
| 177 for (int i = kNThreads - 1; i >= 0; i--) { |
| 178 ThreadIdValidationThread* newThread = |
| 179 new ThreadIdValidationThread(prev, &refs, i, semaphore); |
| 180 threads.Add(newThread); |
| 181 prev = newThread; |
| 182 refs.Add(i::ThreadId::Invalid()); |
| 183 } |
| 184 prev->Start(); |
| 185 for (int i = 0; i < kNThreads; i++) { |
| 186 semaphore->Wait(); |
| 187 } |
| 188 for (int i = 0; i < kNThreads; i++) { |
| 189 delete threads[i]; |
| 190 } |
| 191 } |
| OLD | NEW |