v8  3.11.10(node0.8.26)
V8 is Google's open source JavaScript engine
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
test-threads.cc
Go to the documentation of this file.
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 "v8.h"
29 
30 #include "platform.h"
31 #include "isolate.h"
32 
33 #include "cctest.h"
34 
35 
36 TEST(Preemption) {
37  v8::Locker locker;
39  v8::HandleScope scope;
40  v8::Context::Scope context_scope(v8::Context::New());
41 
43 
45  v8::String::New("var count = 0; var obj = new Object(); count++;\n"));
46 
47  script->Run();
48 
50  v8::internal::OS::Sleep(500); // Make sure the timer fires.
51 
52  script->Run();
53 }
54 
55 
56 enum Turn {
61 };
62 
63 static Turn turn = FILL_CACHE;
64 
65 
66 class ThreadA : public v8::internal::Thread {
67  public:
68  ThreadA() : Thread("ThreadA") { }
69  void Run() {
70  v8::Locker locker;
71  v8::HandleScope scope;
72  v8::Context::Scope context_scope(v8::Context::New());
73 
74  CHECK_EQ(FILL_CACHE, turn);
75 
76  // Fill String.search cache.
79  "for (var i = 0; i < 3; i++) {"
80  " var result = \"a\".search(\"a\");"
81  " if (result != 0) throw \"result: \" + result + \" @\" + i;"
82  "};"
83  "true"));
84  CHECK(script->Run()->IsTrue());
85 
86  turn = CLEAN_CACHE;
87  do {
88  {
89  v8::Unlocker unlocker;
90  Thread::YieldCPU();
91  }
92  } while (turn != SECOND_TIME_FILL_CACHE);
93 
94  // Rerun the script.
95  CHECK(script->Run()->IsTrue());
96 
97  turn = DONE;
98  }
99 };
100 
101 
103  public:
104  ThreadB() : Thread("ThreadB") { }
105  void Run() {
106  do {
107  {
108  v8::Locker locker;
109  if (turn == CLEAN_CACHE) {
110  v8::HandleScope scope;
111  v8::Context::Scope context_scope(v8::Context::New());
112 
113  // Clear the caches by forcing major GC.
114  HEAP->CollectAllGarbage(v8::internal::Heap::kNoGCFlags);
115  turn = SECOND_TIME_FILL_CACHE;
116  break;
117  }
118  }
119 
120  Thread::YieldCPU();
121  } while (true);
122  }
123 };
124 
125 
126 TEST(JSFunctionResultCachesInTwoThreads) {
128 
129  ThreadA threadA;
130  ThreadB threadB;
131 
132  threadA.Start();
133  threadB.Start();
134 
135  threadA.Join();
136  threadB.Join();
137 
138  CHECK_EQ(DONE, turn);
139 }
140 
142  public:
144  i::List<i::ThreadId>* refs,
145  unsigned int thread_no,
147  : Thread("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 
165  private:
166  i::List<i::ThreadId>* refs_;
167  int thread_no_;
168  i::Thread* thread_to_start_;
169  i::Semaphore* semaphore_;
170 };
171 
172 TEST(ThreadIdValidation) {
173  const int kNThreads = 100;
174  i::List<ThreadIdValidationThread*> threads(kNThreads);
175  i::List<i::ThreadId> refs(kNThreads);
178  for (int i = kNThreads - 1; i >= 0; i--) {
179  ThreadIdValidationThread* newThread =
180  new ThreadIdValidationThread(prev, &refs, i, semaphore);
181  threads.Add(newThread);
182  prev = newThread;
183  refs.Add(i::ThreadId::Invalid());
184  }
185  prev->Start();
186  for (int i = 0; i < kNThreads; i++) {
187  semaphore->Wait();
188  }
189  for (int i = 0; i < kNThreads; i++) {
190  delete threads[i];
191  }
192 }
193 
194 
196  public:
197  ThreadC() : Thread("ThreadC") { }
198  void Run() {
199  Join();
200  }
201 };
202 
203 
204 TEST(ThreadJoinSelf) {
205  ThreadC thread;
206  thread.Start();
207  thread.Join();
208 }
static Local< Script > Compile(Handle< String > source, ScriptOrigin *origin=NULL, ScriptData *pre_data=NULL, Handle< String > script_data=Handle< String >())
Definition: api.cc:1560
#define CHECK_EQ(expected, value)
Definition: checks.h:219
Thread(const Options &options)
void Run()
static V8EXPORT Local< String > New(const char *data, int length=-1)
Definition: api.cc:4655
void Run()
Definition: test-threads.cc:69
#define CHECK(condition)
Definition: checks.h:56
ThreadIdValidationThread(i::Thread *thread_to_start, i::List< i::ThreadId > *refs, unsigned int thread_no, i::Semaphore *semaphore)
TEST(Preemption)
Definition: test-threads.cc:36
static ThreadId Current()
Definition: isolate.h:153
void Run()
activate correct semantics for inheriting readonliness enable harmony semantics for typeof enable harmony enable harmony proxies enable all harmony harmony_scoping harmony_proxies harmony_scoping tracks arrays with only smi values automatically unbox arrays of doubles use crankshaft use hydrogen range analysis use hydrogen global value numbering use function inlining maximum number of AST nodes considered for a single inlining loop invariant code motion print statistics for hydrogen trace generated IR for specified phases trace register allocator trace range analysis trace representation types environment for every instruction put a break point before deoptimizing polymorphic inlining perform array bounds checks elimination trace on stack replacement optimize closures functions with arguments object optimize functions containing for in loops profiler considers IC stability primitive functions trigger their own optimization re try self optimization if it failed insert an interrupt check at function exit execution budget before interrupt is triggered call count before self optimization self_optimization count_based_interrupts weighted_back_edges trace_opt emit comments in code disassembly enable use of SSE3 instructions if available enable use of CMOV instruction if available enable use of SAHF instruction if enable use of VFP3 instructions if available this implies enabling ARMv7 enable use of ARMv7 instructions if enable use of MIPS FPU instructions if NULL
static const int kNoGCFlags
Definition: heap.h:1049
Definition: v8.h:104
static void Sleep(const int milliseconds)
static Semaphore * CreateSemaphore(int count)
virtual void Signal()=0
#define HEAP
Definition: isolate.h:1408
static void StartPreemption(int every_n_ms)
Definition: v8threads.cc:142
virtual void Wait()=0
static ThreadId Invalid()
Definition: isolate.h:156
void Add(const T &element, AllocationPolicy allocator=AllocationPolicy())
Definition: list-inl.h:38
Local< Value > Run()
Definition: api.cc:1590
static Persistent< Context > New(ExtensionConfiguration *extensions=NULL, Handle< ObjectTemplate > global_template=Handle< ObjectTemplate >(), Handle< Value > global_object=Handle< Value >())
Definition: api.cc:4308
static void StopPreemption()
Definition: v8threads.cc:147
Turn
Definition: test-threads.cc:56
v8::internal::Semaphore * semaphore
static bool Initialize()
Definition: api.cc:4204