Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1 | // Copyright 2008 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 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 28 | #include "src/v8.h" |
| 29 | #include "test/cctest/cctest.h" |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 30 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 31 | #include "src/base/platform/platform.h" |
| 32 | #include "src/isolate.h" |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 33 | |
| 34 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 35 | class ThreadIdValidationThread : public v8::base::Thread { |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 36 | public: |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 37 | ThreadIdValidationThread(v8::base::Thread* thread_to_start, |
| 38 | i::List<i::ThreadId>* refs, unsigned int thread_no, |
| 39 | v8::base::Semaphore* semaphore) |
| 40 | : Thread(Options("ThreadRefValidationThread")), |
| 41 | refs_(refs), |
| 42 | thread_no_(thread_no), |
| 43 | thread_to_start_(thread_to_start), |
| 44 | semaphore_(semaphore) {} |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 45 | |
| 46 | void Run() { |
| 47 | i::ThreadId thread_id = i::ThreadId::Current(); |
| 48 | for (int i = 0; i < thread_no_; i++) { |
| 49 | CHECK(!(*refs_)[i].Equals(thread_id)); |
| 50 | } |
| 51 | CHECK(thread_id.IsValid()); |
| 52 | (*refs_)[thread_no_] = thread_id; |
| 53 | if (thread_to_start_ != NULL) { |
| 54 | thread_to_start_->Start(); |
| 55 | } |
| 56 | semaphore_->Signal(); |
| 57 | } |
Ben Murdoch | 589d697 | 2011-11-30 16:04:58 +0000 | [diff] [blame] | 58 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 59 | private: |
| 60 | i::List<i::ThreadId>* refs_; |
| 61 | int thread_no_; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 62 | v8::base::Thread* thread_to_start_; |
| 63 | v8::base::Semaphore* semaphore_; |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 64 | }; |
| 65 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 66 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 67 | TEST(ThreadIdValidation) { |
| 68 | const int kNThreads = 100; |
| 69 | i::List<ThreadIdValidationThread*> threads(kNThreads); |
| 70 | i::List<i::ThreadId> refs(kNThreads); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 71 | v8::base::Semaphore semaphore(0); |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 72 | ThreadIdValidationThread* prev = NULL; |
| 73 | for (int i = kNThreads - 1; i >= 0; i--) { |
| 74 | ThreadIdValidationThread* newThread = |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 75 | new ThreadIdValidationThread(prev, &refs, i, &semaphore); |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 76 | threads.Add(newThread); |
| 77 | prev = newThread; |
| 78 | refs.Add(i::ThreadId::Invalid()); |
| 79 | } |
| 80 | prev->Start(); |
| 81 | for (int i = 0; i < kNThreads; i++) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 82 | semaphore.Wait(); |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 83 | } |
| 84 | for (int i = 0; i < kNThreads; i++) { |
| 85 | delete threads[i]; |
| 86 | } |
| 87 | } |