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 
28 #include "src/v8.h"
29 #include "test/cctest/cctest.h"
30 
31 #include "src/base/platform/platform.h"
32 #include "src/isolate.h"
33 
34 
35 class ThreadIdValidationThread : public v8::base::Thread {
36  public:
ThreadIdValidationThread(v8::base::Thread * thread_to_start,i::List<i::ThreadId> * refs,unsigned int thread_no,v8::base::Semaphore * semaphore)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) {}
45 
Run()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   }
58 
59  private:
60   i::List<i::ThreadId>* refs_;
61   int thread_no_;
62   v8::base::Thread* thread_to_start_;
63   v8::base::Semaphore* semaphore_;
64 };
65 
66 
TEST(ThreadIdValidation)67 TEST(ThreadIdValidation) {
68   const int kNThreads = 100;
69   i::List<ThreadIdValidationThread*> threads(kNThreads);
70   i::List<i::ThreadId> refs(kNThreads);
71   v8::base::Semaphore semaphore(0);
72   ThreadIdValidationThread* prev = NULL;
73   for (int i = kNThreads - 1; i >= 0; i--) {
74     ThreadIdValidationThread* newThread =
75         new ThreadIdValidationThread(prev, &refs, i, &semaphore);
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++) {
82     semaphore.Wait();
83   }
84   for (int i = 0; i < kNThreads; i++) {
85     delete threads[i];
86   }
87 }
88