blob: 0f48e248f715a63956f6c7401dd31633344ae337 [file] [log] [blame]
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001// Copyright 2008 the V8 project authors. All rights reserved.
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002// 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
ager@chromium.org32912102009-01-16 10:38:43 +000028#include "v8.h"
29
30#include "platform.h"
31
32#include "cctest.h"
33
34
35TEST(Preemption) {
36 v8::Locker locker;
37 v8::V8::Initialize();
38 v8::HandleScope scope;
39 v8::Context::Scope context_scope(v8::Context::New());
40
41 v8::Locker::StartPreemption(100);
42
43 v8::Handle<v8::Script> script = v8::Script::Compile(
44 v8::String::New("var count = 0; var obj = new Object(); count++;\n"));
45
46 script->Run();
47
48 v8::Locker::StopPreemption();
49 v8::internal::OS::Sleep(500); // Make sure the timer fires.
50
51 script->Run();
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +000052}
ager@chromium.orgac091b72010-05-05 07:34:42 +000053
54
55enum Turn {
56 FILL_CACHE,
57 CLEAN_CACHE,
58 SECOND_TIME_FILL_CACHE,
59 DONE
60};
61
62static Turn turn = FILL_CACHE;
63
64
65class ThreadA: public v8::internal::Thread {
66 public:
67 void Run() {
68 v8::Locker locker;
69 v8::HandleScope scope;
70 v8::Context::Scope context_scope(v8::Context::New());
71
72 CHECK_EQ(FILL_CACHE, turn);
73
74 // Fill String.search cache.
75 v8::Handle<v8::Script> script = v8::Script::Compile(
76 v8::String::New(
77 "for (var i = 0; i < 3; i++) {"
78 " var result = \"a\".search(\"a\");"
79 " if (result != 0) throw \"result: \" + result + \" @\" + i;"
80 "};"
81 "true"));
82 CHECK(script->Run()->IsTrue());
83
84 turn = CLEAN_CACHE;
85 do {
86 {
87 v8::Unlocker unlocker;
88 Thread::YieldCPU();
89 }
90 } while (turn != SECOND_TIME_FILL_CACHE);
91
92 // Rerun the script.
93 CHECK(script->Run()->IsTrue());
94
95 turn = DONE;
96 }
97};
98
99
100class ThreadB: public v8::internal::Thread {
101 public:
102 void Run() {
103 do {
104 {
105 v8::Locker locker;
106 if (turn == CLEAN_CACHE) {
107 v8::HandleScope scope;
108 v8::Context::Scope context_scope(v8::Context::New());
109
110 // Clear the caches by forcing major GC.
111 v8::internal::Heap::CollectAllGarbage(false);
112 turn = SECOND_TIME_FILL_CACHE;
113 break;
114 }
115 }
116
117 Thread::YieldCPU();
118 } while (true);
119 }
120};
121
122
123TEST(JSFunctionResultCachesInTwoThreads) {
124 v8::V8::Initialize();
125
126 ThreadA threadA;
127 ThreadB threadB;
128
129 threadA.Start();
130 threadB.Start();
131
132 threadA.Join();
133 threadB.Join();
134
135 CHECK_EQ(DONE, turn);
136}