blob: a9058a523a4b0338f4e5f0df57127721644007e5 [file] [log] [blame]
Steve Blocka7e24c12009-10-30 11:49:00 +00001// 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 Murdochb8a8cc12014-11-26 15:28:44 +000028#include "src/v8.h"
29#include "test/cctest/cctest.h"
Steve Blocka7e24c12009-10-30 11:49:00 +000030
Ben Murdochb8a8cc12014-11-26 15:28:44 +000031#include "src/base/platform/platform.h"
32#include "src/isolate.h"
Steve Block6ded16b2010-05-10 14:33:55 +010033
34
Ben Murdochb8a8cc12014-11-26 15:28:44 +000035class ThreadIdValidationThread : public v8::base::Thread {
Ben Murdoch8b112d22011-06-08 16:22:53 +010036 public:
Ben Murdochb8a8cc12014-11-26 15:28:44 +000037 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 Murdoch8b112d22011-06-08 16:22:53 +010045
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 Murdoch589d6972011-11-30 16:04:58 +000058
Ben Murdoch8b112d22011-06-08 16:22:53 +010059 private:
60 i::List<i::ThreadId>* refs_;
61 int thread_no_;
Ben Murdochb8a8cc12014-11-26 15:28:44 +000062 v8::base::Thread* thread_to_start_;
63 v8::base::Semaphore* semaphore_;
Ben Murdoch8b112d22011-06-08 16:22:53 +010064};
65
Ben Murdochb8a8cc12014-11-26 15:28:44 +000066
Ben Murdoch8b112d22011-06-08 16:22:53 +010067TEST(ThreadIdValidation) {
68 const int kNThreads = 100;
69 i::List<ThreadIdValidationThread*> threads(kNThreads);
70 i::List<i::ThreadId> refs(kNThreads);
Ben Murdochb8a8cc12014-11-26 15:28:44 +000071 v8::base::Semaphore semaphore(0);
Ben Murdoch8b112d22011-06-08 16:22:53 +010072 ThreadIdValidationThread* prev = NULL;
73 for (int i = kNThreads - 1; i >= 0; i--) {
74 ThreadIdValidationThread* newThread =
Ben Murdochb8a8cc12014-11-26 15:28:44 +000075 new ThreadIdValidationThread(prev, &refs, i, &semaphore);
Ben Murdoch8b112d22011-06-08 16:22:53 +010076 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 Murdochb8a8cc12014-11-26 15:28:44 +000082 semaphore.Wait();
Ben Murdoch8b112d22011-06-08 16:22:53 +010083 }
84 for (int i = 0; i < kNThreads; i++) {
85 delete threads[i];
86 }
87}