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
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.
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.
39
v8::HandleScope scope;
40
v8::Context::Scope context_scope(v8::Context::New());
42
v8::Locker::StartPreemption(100);
44
v8::Handle<v8::Script> script = v8::Script::Compile(
45
v8::String::New("var count = 0; var obj = new Object(); count++;\n"));
49
v8::Locker::StopPreemption();
50
v8::internal::OS::Sleep(500); // Make sure the timer fires.
59
SECOND_TIME_FILL_CACHE,
63
static Turn turn = FILL_CACHE;
66
class ThreadA : public v8::internal::Thread {
68
ThreadA() : Thread("ThreadA") { }
71
v8::HandleScope scope;
72
v8::Context::Scope context_scope(v8::Context::New());
74
CHECK_EQ(FILL_CACHE, turn);
76
// Fill String.search cache.
77
v8::Handle<v8::Script> script = v8::Script::Compile(
79
"for (var i = 0; i < 3; i++) {"
80
" var result = \"a\".search(\"a\");"
81
" if (result != 0) throw \"result: \" + result + \" @\" + i;"
84
CHECK(script->Run()->IsTrue());
89
v8::Unlocker unlocker;
92
} while (turn != SECOND_TIME_FILL_CACHE);
95
CHECK(script->Run()->IsTrue());
102
class ThreadB : public v8::internal::Thread {
104
ThreadB() : Thread("ThreadB") { }
109
if (turn == CLEAN_CACHE) {
110
v8::HandleScope scope;
111
v8::Context::Scope context_scope(v8::Context::New());
113
// Clear the caches by forcing major GC.
114
HEAP->CollectAllGarbage(v8::internal::Heap::kNoGCFlags);
115
turn = SECOND_TIME_FILL_CACHE;
126
TEST(JSFunctionResultCachesInTwoThreads) {
127
v8::V8::Initialize();
138
CHECK_EQ(DONE, turn);
141
class ThreadIdValidationThread : public v8::internal::Thread {
143
ThreadIdValidationThread(i::Thread* thread_to_start,
144
i::List<i::ThreadId>* refs,
145
unsigned int thread_no,
146
i::Semaphore* semaphore)
147
: Thread("ThreadRefValidationThread"),
148
refs_(refs), thread_no_(thread_no), thread_to_start_(thread_to_start),
149
semaphore_(semaphore) {
153
i::ThreadId thread_id = i::ThreadId::Current();
154
for (int i = 0; i < thread_no_; i++) {
155
CHECK(!(*refs_)[i].Equals(thread_id));
157
CHECK(thread_id.IsValid());
158
(*refs_)[thread_no_] = thread_id;
159
if (thread_to_start_ != NULL) {
160
thread_to_start_->Start();
162
semaphore_->Signal();
166
i::List<i::ThreadId>* refs_;
168
i::Thread* thread_to_start_;
169
i::Semaphore* semaphore_;
172
TEST(ThreadIdValidation) {
173
const int kNThreads = 100;
174
i::List<ThreadIdValidationThread*> threads(kNThreads);
175
i::List<i::ThreadId> refs(kNThreads);
176
i::Semaphore* semaphore = i::OS::CreateSemaphore(0);
177
ThreadIdValidationThread* prev = NULL;
178
for (int i = kNThreads - 1; i >= 0; i--) {
179
ThreadIdValidationThread* newThread =
180
new ThreadIdValidationThread(prev, &refs, i, semaphore);
181
threads.Add(newThread);
183
refs.Add(i::ThreadId::Invalid());
186
for (int i = 0; i < kNThreads; i++) {
189
for (int i = 0; i < kNThreads; i++) {
195
class ThreadC : public v8::internal::Thread {
197
ThreadC() : Thread("ThreadC") { }
204
TEST(ThreadJoinSelf) {