lrn@chromium.org | 0040ca9 | 2011-05-09 10:42:56 +0000 | [diff] [blame] | 1 | // Copyright 2007-2011 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 <limits.h> |
| 29 | |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame^] | 30 | // TODO(dcarney): remove |
| 31 | #define V8_ALLOW_ACCESS_TO_PERSISTENT_IMPLICIT |
| 32 | |
lrn@chromium.org | 0040ca9 | 2011-05-09 10:42:56 +0000 | [diff] [blame] | 33 | #include "v8.h" |
| 34 | |
| 35 | #include "api.h" |
| 36 | #include "isolate.h" |
| 37 | #include "compilation-cache.h" |
| 38 | #include "execution.h" |
danno@chromium.org | ca29dd8 | 2013-04-26 11:59:48 +0000 | [diff] [blame] | 39 | #include "smart-pointers.h" |
lrn@chromium.org | 0040ca9 | 2011-05-09 10:42:56 +0000 | [diff] [blame] | 40 | #include "snapshot.h" |
| 41 | #include "platform.h" |
| 42 | #include "utils.h" |
| 43 | #include "cctest.h" |
| 44 | #include "parser.h" |
| 45 | #include "unicode-inl.h" |
| 46 | |
| 47 | using ::v8::AccessorInfo; |
| 48 | using ::v8::Context; |
| 49 | using ::v8::Extension; |
| 50 | using ::v8::Function; |
| 51 | using ::v8::HandleScope; |
| 52 | using ::v8::Local; |
| 53 | using ::v8::Object; |
| 54 | using ::v8::ObjectTemplate; |
| 55 | using ::v8::Persistent; |
| 56 | using ::v8::Script; |
| 57 | using ::v8::String; |
| 58 | using ::v8::Value; |
| 59 | using ::v8::V8; |
| 60 | |
lrn@chromium.org | 0040ca9 | 2011-05-09 10:42:56 +0000 | [diff] [blame] | 61 | |
| 62 | // Migrating an isolate |
| 63 | class KangarooThread : public v8::internal::Thread { |
| 64 | public: |
danno@chromium.org | ca29dd8 | 2013-04-26 11:59:48 +0000 | [diff] [blame] | 65 | KangarooThread(v8::Isolate* isolate, v8::Handle<v8::Context> context) |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 66 | : Thread("KangarooThread"), |
danno@chromium.org | ca29dd8 | 2013-04-26 11:59:48 +0000 | [diff] [blame] | 67 | isolate_(isolate), |
| 68 | context_(isolate, context) {} |
lrn@chromium.org | 0040ca9 | 2011-05-09 10:42:56 +0000 | [diff] [blame] | 69 | |
| 70 | void Run() { |
| 71 | { |
| 72 | v8::Locker locker(isolate_); |
| 73 | v8::Isolate::Scope isolate_scope(isolate_); |
| 74 | CHECK_EQ(isolate_, v8::internal::Isolate::Current()); |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 75 | v8::HandleScope scope(isolate_); |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame^] | 76 | v8::Context::Scope context_scope(isolate_, context_); |
lrn@chromium.org | 0040ca9 | 2011-05-09 10:42:56 +0000 | [diff] [blame] | 77 | Local<Value> v = CompileRun("getValue()"); |
| 78 | CHECK(v->IsNumber()); |
| 79 | CHECK_EQ(30, static_cast<int>(v->NumberValue())); |
| 80 | } |
| 81 | { |
| 82 | v8::Locker locker(isolate_); |
| 83 | v8::Isolate::Scope isolate_scope(isolate_); |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 84 | v8::HandleScope scope(isolate_); |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame^] | 85 | v8::Context::Scope context_scope(isolate_, context_); |
lrn@chromium.org | 0040ca9 | 2011-05-09 10:42:56 +0000 | [diff] [blame] | 86 | Local<Value> v = CompileRun("getValue()"); |
| 87 | CHECK(v->IsNumber()); |
| 88 | CHECK_EQ(30, static_cast<int>(v->NumberValue())); |
| 89 | } |
| 90 | isolate_->Dispose(); |
| 91 | } |
| 92 | |
| 93 | private: |
| 94 | v8::Isolate* isolate_; |
| 95 | Persistent<v8::Context> context_; |
lrn@chromium.org | 0040ca9 | 2011-05-09 10:42:56 +0000 | [diff] [blame] | 96 | }; |
| 97 | |
| 98 | // Migrates an isolate from one thread to another |
| 99 | TEST(KangarooIsolates) { |
| 100 | v8::Isolate* isolate = v8::Isolate::New(); |
danno@chromium.org | ca29dd8 | 2013-04-26 11:59:48 +0000 | [diff] [blame] | 101 | i::SmartPointer<KangarooThread> thread1; |
lrn@chromium.org | 0040ca9 | 2011-05-09 10:42:56 +0000 | [diff] [blame] | 102 | { |
| 103 | v8::Locker locker(isolate); |
| 104 | v8::Isolate::Scope isolate_scope(isolate); |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 105 | v8::HandleScope handle_scope(isolate); |
danno@chromium.org | ca29dd8 | 2013-04-26 11:59:48 +0000 | [diff] [blame] | 106 | v8::Local<v8::Context> context = v8::Context::New(isolate); |
lrn@chromium.org | 0040ca9 | 2011-05-09 10:42:56 +0000 | [diff] [blame] | 107 | v8::Context::Scope context_scope(context); |
| 108 | CHECK_EQ(isolate, v8::internal::Isolate::Current()); |
| 109 | CompileRun("function getValue() { return 30; }"); |
danno@chromium.org | ca29dd8 | 2013-04-26 11:59:48 +0000 | [diff] [blame] | 110 | thread1.Reset(new KangarooThread(isolate, context)); |
lrn@chromium.org | 0040ca9 | 2011-05-09 10:42:56 +0000 | [diff] [blame] | 111 | } |
danno@chromium.org | ca29dd8 | 2013-04-26 11:59:48 +0000 | [diff] [blame] | 112 | thread1->Start(); |
| 113 | thread1->Join(); |
lrn@chromium.org | 0040ca9 | 2011-05-09 10:42:56 +0000 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | static void CalcFibAndCheck() { |
| 117 | Local<Value> v = CompileRun("function fib(n) {" |
| 118 | " if (n <= 2) return 1;" |
| 119 | " return fib(n-1) + fib(n-2);" |
| 120 | "}" |
| 121 | "fib(10)"); |
| 122 | CHECK(v->IsNumber()); |
| 123 | CHECK_EQ(55, static_cast<int>(v->NumberValue())); |
| 124 | } |
| 125 | |
| 126 | class JoinableThread { |
| 127 | public: |
| 128 | explicit JoinableThread(const char* name) |
| 129 | : name_(name), |
| 130 | semaphore_(i::OS::CreateSemaphore(0)), |
| 131 | thread_(this) { |
| 132 | } |
| 133 | |
| 134 | virtual ~JoinableThread() { |
| 135 | delete semaphore_; |
| 136 | } |
| 137 | |
| 138 | void Start() { |
| 139 | thread_.Start(); |
| 140 | } |
| 141 | |
| 142 | void Join() { |
| 143 | semaphore_->Wait(); |
| 144 | } |
| 145 | |
| 146 | virtual void Run() = 0; |
kmillikin@chromium.org | 83e1682 | 2011-09-13 08:21:47 +0000 | [diff] [blame] | 147 | |
lrn@chromium.org | 0040ca9 | 2011-05-09 10:42:56 +0000 | [diff] [blame] | 148 | private: |
| 149 | class ThreadWithSemaphore : public i::Thread { |
| 150 | public: |
| 151 | explicit ThreadWithSemaphore(JoinableThread* joinable_thread) |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 152 | : Thread(joinable_thread->name_), |
lrn@chromium.org | 0040ca9 | 2011-05-09 10:42:56 +0000 | [diff] [blame] | 153 | joinable_thread_(joinable_thread) { |
| 154 | } |
| 155 | |
| 156 | virtual void Run() { |
| 157 | joinable_thread_->Run(); |
| 158 | joinable_thread_->semaphore_->Signal(); |
| 159 | } |
| 160 | |
| 161 | private: |
| 162 | JoinableThread* joinable_thread_; |
| 163 | }; |
| 164 | |
| 165 | const char* name_; |
| 166 | i::Semaphore* semaphore_; |
| 167 | ThreadWithSemaphore thread_; |
| 168 | |
| 169 | friend class ThreadWithSemaphore; |
| 170 | |
| 171 | DISALLOW_COPY_AND_ASSIGN(JoinableThread); |
| 172 | }; |
| 173 | |
| 174 | |
| 175 | class IsolateLockingThreadWithLocalContext : public JoinableThread { |
| 176 | public: |
| 177 | explicit IsolateLockingThreadWithLocalContext(v8::Isolate* isolate) |
| 178 | : JoinableThread("IsolateLockingThread"), |
| 179 | isolate_(isolate) { |
| 180 | } |
| 181 | |
| 182 | virtual void Run() { |
| 183 | v8::Locker locker(isolate_); |
| 184 | v8::Isolate::Scope isolate_scope(isolate_); |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 185 | v8::HandleScope handle_scope(isolate_); |
lrn@chromium.org | 0040ca9 | 2011-05-09 10:42:56 +0000 | [diff] [blame] | 186 | LocalContext local_context; |
| 187 | CHECK_EQ(isolate_, v8::internal::Isolate::Current()); |
| 188 | CalcFibAndCheck(); |
| 189 | } |
| 190 | private: |
| 191 | v8::Isolate* isolate_; |
| 192 | }; |
| 193 | |
| 194 | static void StartJoinAndDeleteThreads(const i::List<JoinableThread*>& threads) { |
| 195 | for (int i = 0; i < threads.length(); i++) { |
| 196 | threads[i]->Start(); |
| 197 | } |
| 198 | for (int i = 0; i < threads.length(); i++) { |
| 199 | threads[i]->Join(); |
| 200 | } |
| 201 | for (int i = 0; i < threads.length(); i++) { |
| 202 | delete threads[i]; |
| 203 | } |
| 204 | } |
| 205 | |
| 206 | |
| 207 | // Run many threads all locking on the same isolate |
| 208 | TEST(IsolateLockingStress) { |
mstarzinger@chromium.org | f8c6bd5 | 2011-11-23 12:13:52 +0000 | [diff] [blame] | 209 | #ifdef V8_TARGET_ARCH_MIPS |
| 210 | const int kNThreads = 50; |
| 211 | #else |
lrn@chromium.org | 0040ca9 | 2011-05-09 10:42:56 +0000 | [diff] [blame] | 212 | const int kNThreads = 100; |
mstarzinger@chromium.org | f8c6bd5 | 2011-11-23 12:13:52 +0000 | [diff] [blame] | 213 | #endif |
lrn@chromium.org | 0040ca9 | 2011-05-09 10:42:56 +0000 | [diff] [blame] | 214 | i::List<JoinableThread*> threads(kNThreads); |
| 215 | v8::Isolate* isolate = v8::Isolate::New(); |
| 216 | for (int i = 0; i < kNThreads; i++) { |
| 217 | threads.Add(new IsolateLockingThreadWithLocalContext(isolate)); |
| 218 | } |
| 219 | StartJoinAndDeleteThreads(threads); |
| 220 | isolate->Dispose(); |
| 221 | } |
| 222 | |
| 223 | class IsolateNonlockingThread : public JoinableThread { |
| 224 | public: |
| 225 | explicit IsolateNonlockingThread() |
| 226 | : JoinableThread("IsolateNonlockingThread") { |
| 227 | } |
| 228 | |
| 229 | virtual void Run() { |
| 230 | v8::Isolate* isolate = v8::Isolate::New(); |
| 231 | { |
| 232 | v8::Isolate::Scope isolate_scope(isolate); |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 233 | v8::HandleScope handle_scope(isolate); |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame^] | 234 | v8::Handle<v8::Context> context = v8::Context::New(isolate); |
lrn@chromium.org | 0040ca9 | 2011-05-09 10:42:56 +0000 | [diff] [blame] | 235 | v8::Context::Scope context_scope(context); |
| 236 | CHECK_EQ(isolate, v8::internal::Isolate::Current()); |
| 237 | CalcFibAndCheck(); |
| 238 | } |
| 239 | isolate->Dispose(); |
| 240 | } |
| 241 | private: |
| 242 | }; |
| 243 | |
| 244 | // Run many threads each accessing its own isolate without locking |
| 245 | TEST(MultithreadedParallelIsolates) { |
mstarzinger@chromium.org | f8c6bd5 | 2011-11-23 12:13:52 +0000 | [diff] [blame] | 246 | #if defined(V8_TARGET_ARCH_ARM) || defined(V8_TARGET_ARCH_MIPS) |
vegorov@chromium.org | 7304bca | 2011-05-16 12:14:13 +0000 | [diff] [blame] | 247 | const int kNThreads = 10; |
| 248 | #else |
lrn@chromium.org | 0040ca9 | 2011-05-09 10:42:56 +0000 | [diff] [blame] | 249 | const int kNThreads = 50; |
vegorov@chromium.org | 7304bca | 2011-05-16 12:14:13 +0000 | [diff] [blame] | 250 | #endif |
lrn@chromium.org | 0040ca9 | 2011-05-09 10:42:56 +0000 | [diff] [blame] | 251 | i::List<JoinableThread*> threads(kNThreads); |
| 252 | for (int i = 0; i < kNThreads; i++) { |
| 253 | threads.Add(new IsolateNonlockingThread()); |
| 254 | } |
| 255 | StartJoinAndDeleteThreads(threads); |
| 256 | } |
| 257 | |
| 258 | |
| 259 | class IsolateNestedLockingThread : public JoinableThread { |
| 260 | public: |
| 261 | explicit IsolateNestedLockingThread(v8::Isolate* isolate) |
| 262 | : JoinableThread("IsolateNestedLocking"), isolate_(isolate) { |
| 263 | } |
| 264 | virtual void Run() { |
| 265 | v8::Locker lock(isolate_); |
| 266 | v8::Isolate::Scope isolate_scope(isolate_); |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 267 | v8::HandleScope handle_scope(isolate_); |
lrn@chromium.org | 0040ca9 | 2011-05-09 10:42:56 +0000 | [diff] [blame] | 268 | LocalContext local_context; |
| 269 | { |
| 270 | v8::Locker another_lock(isolate_); |
| 271 | CalcFibAndCheck(); |
| 272 | } |
| 273 | { |
| 274 | v8::Locker another_lock(isolate_); |
| 275 | CalcFibAndCheck(); |
| 276 | } |
| 277 | } |
| 278 | private: |
| 279 | v8::Isolate* isolate_; |
| 280 | }; |
| 281 | |
| 282 | // Run many threads with nested locks |
| 283 | TEST(IsolateNestedLocking) { |
mstarzinger@chromium.org | f8c6bd5 | 2011-11-23 12:13:52 +0000 | [diff] [blame] | 284 | #ifdef V8_TARGET_ARCH_MIPS |
| 285 | const int kNThreads = 50; |
| 286 | #else |
lrn@chromium.org | 0040ca9 | 2011-05-09 10:42:56 +0000 | [diff] [blame] | 287 | const int kNThreads = 100; |
mstarzinger@chromium.org | f8c6bd5 | 2011-11-23 12:13:52 +0000 | [diff] [blame] | 288 | #endif |
lrn@chromium.org | 0040ca9 | 2011-05-09 10:42:56 +0000 | [diff] [blame] | 289 | v8::Isolate* isolate = v8::Isolate::New(); |
| 290 | i::List<JoinableThread*> threads(kNThreads); |
| 291 | for (int i = 0; i < kNThreads; i++) { |
| 292 | threads.Add(new IsolateNestedLockingThread(isolate)); |
| 293 | } |
| 294 | StartJoinAndDeleteThreads(threads); |
hpayer@chromium.org | 7c3372b | 2013-02-13 17:26:04 +0000 | [diff] [blame] | 295 | isolate->Dispose(); |
lrn@chromium.org | 0040ca9 | 2011-05-09 10:42:56 +0000 | [diff] [blame] | 296 | } |
| 297 | |
| 298 | |
| 299 | class SeparateIsolatesLocksNonexclusiveThread : public JoinableThread { |
| 300 | public: |
| 301 | SeparateIsolatesLocksNonexclusiveThread(v8::Isolate* isolate1, |
| 302 | v8::Isolate* isolate2) |
| 303 | : JoinableThread("SeparateIsolatesLocksNonexclusiveThread"), |
| 304 | isolate1_(isolate1), isolate2_(isolate2) { |
| 305 | } |
| 306 | |
| 307 | virtual void Run() { |
| 308 | v8::Locker lock(isolate1_); |
| 309 | v8::Isolate::Scope isolate_scope(isolate1_); |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 310 | v8::HandleScope handle_scope(isolate1_); |
lrn@chromium.org | 0040ca9 | 2011-05-09 10:42:56 +0000 | [diff] [blame] | 311 | LocalContext local_context; |
| 312 | |
| 313 | IsolateLockingThreadWithLocalContext threadB(isolate2_); |
| 314 | threadB.Start(); |
| 315 | CalcFibAndCheck(); |
| 316 | threadB.Join(); |
| 317 | } |
| 318 | private: |
| 319 | v8::Isolate* isolate1_; |
| 320 | v8::Isolate* isolate2_; |
| 321 | }; |
| 322 | |
| 323 | // Run parallel threads that lock and access different isolates in parallel |
| 324 | TEST(SeparateIsolatesLocksNonexclusive) { |
mstarzinger@chromium.org | f8c6bd5 | 2011-11-23 12:13:52 +0000 | [diff] [blame] | 325 | #if defined(V8_TARGET_ARCH_ARM) || defined(V8_TARGET_ARCH_MIPS) |
whesse@chromium.org | 030d38e | 2011-07-13 13:23:34 +0000 | [diff] [blame] | 326 | const int kNThreads = 50; |
| 327 | #else |
lrn@chromium.org | 0040ca9 | 2011-05-09 10:42:56 +0000 | [diff] [blame] | 328 | const int kNThreads = 100; |
whesse@chromium.org | 030d38e | 2011-07-13 13:23:34 +0000 | [diff] [blame] | 329 | #endif |
lrn@chromium.org | 0040ca9 | 2011-05-09 10:42:56 +0000 | [diff] [blame] | 330 | v8::Isolate* isolate1 = v8::Isolate::New(); |
| 331 | v8::Isolate* isolate2 = v8::Isolate::New(); |
| 332 | i::List<JoinableThread*> threads(kNThreads); |
| 333 | for (int i = 0; i < kNThreads; i++) { |
| 334 | threads.Add(new SeparateIsolatesLocksNonexclusiveThread(isolate1, |
| 335 | isolate2)); |
| 336 | } |
| 337 | StartJoinAndDeleteThreads(threads); |
| 338 | isolate2->Dispose(); |
| 339 | isolate1->Dispose(); |
| 340 | } |
| 341 | |
| 342 | class LockIsolateAndCalculateFibSharedContextThread : public JoinableThread { |
| 343 | public: |
| 344 | explicit LockIsolateAndCalculateFibSharedContextThread( |
| 345 | v8::Isolate* isolate, v8::Handle<v8::Context> context) |
| 346 | : JoinableThread("LockIsolateAndCalculateFibThread"), |
| 347 | isolate_(isolate), |
danno@chromium.org | ca29dd8 | 2013-04-26 11:59:48 +0000 | [diff] [blame] | 348 | context_(isolate, context) { |
lrn@chromium.org | 0040ca9 | 2011-05-09 10:42:56 +0000 | [diff] [blame] | 349 | } |
| 350 | |
| 351 | virtual void Run() { |
| 352 | v8::Locker lock(isolate_); |
| 353 | v8::Isolate::Scope isolate_scope(isolate_); |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 354 | HandleScope handle_scope(isolate_); |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame^] | 355 | v8::Context::Scope context_scope(isolate_, context_); |
lrn@chromium.org | 0040ca9 | 2011-05-09 10:42:56 +0000 | [diff] [blame] | 356 | CalcFibAndCheck(); |
| 357 | } |
| 358 | private: |
| 359 | v8::Isolate* isolate_; |
| 360 | Persistent<v8::Context> context_; |
| 361 | }; |
| 362 | |
| 363 | class LockerUnlockerThread : public JoinableThread { |
| 364 | public: |
| 365 | explicit LockerUnlockerThread(v8::Isolate* isolate) |
| 366 | : JoinableThread("LockerUnlockerThread"), |
| 367 | isolate_(isolate) { |
| 368 | } |
| 369 | |
| 370 | virtual void Run() { |
| 371 | v8::Locker lock(isolate_); |
| 372 | v8::Isolate::Scope isolate_scope(isolate_); |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 373 | v8::HandleScope handle_scope(isolate_); |
danno@chromium.org | ca29dd8 | 2013-04-26 11:59:48 +0000 | [diff] [blame] | 374 | v8::Local<v8::Context> context = v8::Context::New(isolate_); |
lrn@chromium.org | 0040ca9 | 2011-05-09 10:42:56 +0000 | [diff] [blame] | 375 | { |
| 376 | v8::Context::Scope context_scope(context); |
| 377 | CalcFibAndCheck(); |
| 378 | } |
| 379 | { |
danno@chromium.org | ca29dd8 | 2013-04-26 11:59:48 +0000 | [diff] [blame] | 380 | LockIsolateAndCalculateFibSharedContextThread thread(isolate_, context); |
lrn@chromium.org | 0040ca9 | 2011-05-09 10:42:56 +0000 | [diff] [blame] | 381 | isolate_->Exit(); |
| 382 | v8::Unlocker unlocker(isolate_); |
lrn@chromium.org | 0040ca9 | 2011-05-09 10:42:56 +0000 | [diff] [blame] | 383 | thread.Start(); |
| 384 | thread.Join(); |
| 385 | } |
| 386 | isolate_->Enter(); |
| 387 | { |
| 388 | v8::Context::Scope context_scope(context); |
| 389 | CalcFibAndCheck(); |
| 390 | } |
| 391 | } |
kmillikin@chromium.org | 83e1682 | 2011-09-13 08:21:47 +0000 | [diff] [blame] | 392 | |
lrn@chromium.org | 0040ca9 | 2011-05-09 10:42:56 +0000 | [diff] [blame] | 393 | private: |
| 394 | v8::Isolate* isolate_; |
| 395 | }; |
| 396 | |
| 397 | // Use unlocker inside of a Locker, multiple threads. |
| 398 | TEST(LockerUnlocker) { |
mstarzinger@chromium.org | f8c6bd5 | 2011-11-23 12:13:52 +0000 | [diff] [blame] | 399 | #if defined(V8_TARGET_ARCH_ARM) || defined(V8_TARGET_ARCH_MIPS) |
whesse@chromium.org | 030d38e | 2011-07-13 13:23:34 +0000 | [diff] [blame] | 400 | const int kNThreads = 50; |
| 401 | #else |
lrn@chromium.org | 0040ca9 | 2011-05-09 10:42:56 +0000 | [diff] [blame] | 402 | const int kNThreads = 100; |
whesse@chromium.org | 030d38e | 2011-07-13 13:23:34 +0000 | [diff] [blame] | 403 | #endif |
lrn@chromium.org | 0040ca9 | 2011-05-09 10:42:56 +0000 | [diff] [blame] | 404 | i::List<JoinableThread*> threads(kNThreads); |
| 405 | v8::Isolate* isolate = v8::Isolate::New(); |
| 406 | for (int i = 0; i < kNThreads; i++) { |
| 407 | threads.Add(new LockerUnlockerThread(isolate)); |
| 408 | } |
| 409 | StartJoinAndDeleteThreads(threads); |
| 410 | isolate->Dispose(); |
| 411 | } |
| 412 | |
| 413 | class LockTwiceAndUnlockThread : public JoinableThread { |
| 414 | public: |
| 415 | explicit LockTwiceAndUnlockThread(v8::Isolate* isolate) |
| 416 | : JoinableThread("LockTwiceAndUnlockThread"), |
| 417 | isolate_(isolate) { |
| 418 | } |
| 419 | |
| 420 | virtual void Run() { |
| 421 | v8::Locker lock(isolate_); |
| 422 | v8::Isolate::Scope isolate_scope(isolate_); |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 423 | v8::HandleScope handle_scope(isolate_); |
danno@chromium.org | ca29dd8 | 2013-04-26 11:59:48 +0000 | [diff] [blame] | 424 | v8::Local<v8::Context> context = v8::Context::New(isolate_); |
lrn@chromium.org | 0040ca9 | 2011-05-09 10:42:56 +0000 | [diff] [blame] | 425 | { |
| 426 | v8::Context::Scope context_scope(context); |
| 427 | CalcFibAndCheck(); |
| 428 | } |
| 429 | { |
| 430 | v8::Locker second_lock(isolate_); |
| 431 | { |
danno@chromium.org | ca29dd8 | 2013-04-26 11:59:48 +0000 | [diff] [blame] | 432 | LockIsolateAndCalculateFibSharedContextThread thread(isolate_, context); |
lrn@chromium.org | 0040ca9 | 2011-05-09 10:42:56 +0000 | [diff] [blame] | 433 | isolate_->Exit(); |
| 434 | v8::Unlocker unlocker(isolate_); |
lrn@chromium.org | 0040ca9 | 2011-05-09 10:42:56 +0000 | [diff] [blame] | 435 | thread.Start(); |
| 436 | thread.Join(); |
| 437 | } |
| 438 | } |
| 439 | isolate_->Enter(); |
| 440 | { |
| 441 | v8::Context::Scope context_scope(context); |
| 442 | CalcFibAndCheck(); |
| 443 | } |
| 444 | } |
kmillikin@chromium.org | 83e1682 | 2011-09-13 08:21:47 +0000 | [diff] [blame] | 445 | |
lrn@chromium.org | 0040ca9 | 2011-05-09 10:42:56 +0000 | [diff] [blame] | 446 | private: |
| 447 | v8::Isolate* isolate_; |
| 448 | }; |
| 449 | |
| 450 | // Use Unlocker inside two Lockers. |
| 451 | TEST(LockTwiceAndUnlock) { |
mstarzinger@chromium.org | f8c6bd5 | 2011-11-23 12:13:52 +0000 | [diff] [blame] | 452 | #if defined(V8_TARGET_ARCH_ARM) || defined(V8_TARGET_ARCH_MIPS) |
whesse@chromium.org | 030d38e | 2011-07-13 13:23:34 +0000 | [diff] [blame] | 453 | const int kNThreads = 50; |
| 454 | #else |
lrn@chromium.org | 0040ca9 | 2011-05-09 10:42:56 +0000 | [diff] [blame] | 455 | const int kNThreads = 100; |
whesse@chromium.org | 030d38e | 2011-07-13 13:23:34 +0000 | [diff] [blame] | 456 | #endif |
lrn@chromium.org | 0040ca9 | 2011-05-09 10:42:56 +0000 | [diff] [blame] | 457 | i::List<JoinableThread*> threads(kNThreads); |
| 458 | v8::Isolate* isolate = v8::Isolate::New(); |
| 459 | for (int i = 0; i < kNThreads; i++) { |
| 460 | threads.Add(new LockTwiceAndUnlockThread(isolate)); |
| 461 | } |
| 462 | StartJoinAndDeleteThreads(threads); |
| 463 | isolate->Dispose(); |
| 464 | } |
| 465 | |
| 466 | class LockAndUnlockDifferentIsolatesThread : public JoinableThread { |
| 467 | public: |
| 468 | LockAndUnlockDifferentIsolatesThread(v8::Isolate* isolate1, |
| 469 | v8::Isolate* isolate2) |
| 470 | : JoinableThread("LockAndUnlockDifferentIsolatesThread"), |
| 471 | isolate1_(isolate1), |
| 472 | isolate2_(isolate2) { |
| 473 | } |
| 474 | |
| 475 | virtual void Run() { |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame^] | 476 | i::SmartPointer<LockIsolateAndCalculateFibSharedContextThread> thread; |
lrn@chromium.org | 0040ca9 | 2011-05-09 10:42:56 +0000 | [diff] [blame] | 477 | v8::Locker lock1(isolate1_); |
| 478 | CHECK(v8::Locker::IsLocked(isolate1_)); |
| 479 | CHECK(!v8::Locker::IsLocked(isolate2_)); |
| 480 | { |
| 481 | v8::Isolate::Scope isolate_scope(isolate1_); |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 482 | v8::HandleScope handle_scope(isolate1_); |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame^] | 483 | v8::Handle<v8::Context> context1 = v8::Context::New(isolate1_); |
lrn@chromium.org | 0040ca9 | 2011-05-09 10:42:56 +0000 | [diff] [blame] | 484 | { |
| 485 | v8::Context::Scope context_scope(context1); |
| 486 | CalcFibAndCheck(); |
| 487 | } |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame^] | 488 | thread.Reset(new LockIsolateAndCalculateFibSharedContextThread( |
| 489 | isolate1_, context1)); |
lrn@chromium.org | 0040ca9 | 2011-05-09 10:42:56 +0000 | [diff] [blame] | 490 | } |
| 491 | v8::Locker lock2(isolate2_); |
| 492 | CHECK(v8::Locker::IsLocked(isolate1_)); |
| 493 | CHECK(v8::Locker::IsLocked(isolate2_)); |
| 494 | { |
| 495 | v8::Isolate::Scope isolate_scope(isolate2_); |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 496 | v8::HandleScope handle_scope(isolate2_); |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame^] | 497 | v8::Handle<v8::Context> context2 = v8::Context::New(isolate2_); |
lrn@chromium.org | 0040ca9 | 2011-05-09 10:42:56 +0000 | [diff] [blame] | 498 | { |
| 499 | v8::Context::Scope context_scope(context2); |
| 500 | CalcFibAndCheck(); |
| 501 | } |
lrn@chromium.org | 0040ca9 | 2011-05-09 10:42:56 +0000 | [diff] [blame] | 502 | v8::Unlocker unlock1(isolate1_); |
| 503 | CHECK(!v8::Locker::IsLocked(isolate1_)); |
| 504 | CHECK(v8::Locker::IsLocked(isolate2_)); |
lrn@chromium.org | 0040ca9 | 2011-05-09 10:42:56 +0000 | [diff] [blame] | 505 | v8::Context::Scope context_scope(context2); |
danno@chromium.org | ca29dd8 | 2013-04-26 11:59:48 +0000 | [diff] [blame] | 506 | thread->Start(); |
lrn@chromium.org | 0040ca9 | 2011-05-09 10:42:56 +0000 | [diff] [blame] | 507 | CalcFibAndCheck(); |
danno@chromium.org | ca29dd8 | 2013-04-26 11:59:48 +0000 | [diff] [blame] | 508 | thread->Join(); |
lrn@chromium.org | 0040ca9 | 2011-05-09 10:42:56 +0000 | [diff] [blame] | 509 | } |
| 510 | } |
kmillikin@chromium.org | 83e1682 | 2011-09-13 08:21:47 +0000 | [diff] [blame] | 511 | |
lrn@chromium.org | 0040ca9 | 2011-05-09 10:42:56 +0000 | [diff] [blame] | 512 | private: |
| 513 | v8::Isolate* isolate1_; |
| 514 | v8::Isolate* isolate2_; |
| 515 | }; |
| 516 | |
| 517 | // Lock two isolates and unlock one of them. |
| 518 | TEST(LockAndUnlockDifferentIsolates) { |
| 519 | v8::Isolate* isolate1 = v8::Isolate::New(); |
| 520 | v8::Isolate* isolate2 = v8::Isolate::New(); |
| 521 | LockAndUnlockDifferentIsolatesThread thread(isolate1, isolate2); |
| 522 | thread.Start(); |
| 523 | thread.Join(); |
| 524 | isolate2->Dispose(); |
| 525 | isolate1->Dispose(); |
| 526 | } |
| 527 | |
| 528 | class LockUnlockLockThread : public JoinableThread { |
| 529 | public: |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame^] | 530 | LockUnlockLockThread(v8::Isolate* isolate, v8::Handle<v8::Context> context) |
lrn@chromium.org | 0040ca9 | 2011-05-09 10:42:56 +0000 | [diff] [blame] | 531 | : JoinableThread("LockUnlockLockThread"), |
| 532 | isolate_(isolate), |
danno@chromium.org | ca29dd8 | 2013-04-26 11:59:48 +0000 | [diff] [blame] | 533 | context_(isolate, context) { |
lrn@chromium.org | 0040ca9 | 2011-05-09 10:42:56 +0000 | [diff] [blame] | 534 | } |
| 535 | |
| 536 | virtual void Run() { |
| 537 | v8::Locker lock1(isolate_); |
| 538 | CHECK(v8::Locker::IsLocked(isolate_)); |
yangguo@chromium.org | 46a2a51 | 2013-01-18 16:29:40 +0000 | [diff] [blame] | 539 | CHECK(!v8::Locker::IsLocked(CcTest::default_isolate())); |
lrn@chromium.org | 0040ca9 | 2011-05-09 10:42:56 +0000 | [diff] [blame] | 540 | { |
| 541 | v8::Isolate::Scope isolate_scope(isolate_); |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 542 | v8::HandleScope handle_scope(isolate_); |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame^] | 543 | v8::Context::Scope context_scope(isolate_, context_); |
lrn@chromium.org | 0040ca9 | 2011-05-09 10:42:56 +0000 | [diff] [blame] | 544 | CalcFibAndCheck(); |
| 545 | } |
| 546 | { |
| 547 | v8::Unlocker unlock1(isolate_); |
| 548 | CHECK(!v8::Locker::IsLocked(isolate_)); |
yangguo@chromium.org | 46a2a51 | 2013-01-18 16:29:40 +0000 | [diff] [blame] | 549 | CHECK(!v8::Locker::IsLocked(CcTest::default_isolate())); |
lrn@chromium.org | 0040ca9 | 2011-05-09 10:42:56 +0000 | [diff] [blame] | 550 | { |
| 551 | v8::Locker lock2(isolate_); |
| 552 | v8::Isolate::Scope isolate_scope(isolate_); |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 553 | v8::HandleScope handle_scope(isolate_); |
lrn@chromium.org | 0040ca9 | 2011-05-09 10:42:56 +0000 | [diff] [blame] | 554 | CHECK(v8::Locker::IsLocked(isolate_)); |
yangguo@chromium.org | 46a2a51 | 2013-01-18 16:29:40 +0000 | [diff] [blame] | 555 | CHECK(!v8::Locker::IsLocked(CcTest::default_isolate())); |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame^] | 556 | v8::Context::Scope context_scope(isolate_, context_); |
lrn@chromium.org | 0040ca9 | 2011-05-09 10:42:56 +0000 | [diff] [blame] | 557 | CalcFibAndCheck(); |
| 558 | } |
| 559 | } |
| 560 | } |
| 561 | |
| 562 | private: |
| 563 | v8::Isolate* isolate_; |
| 564 | v8::Persistent<v8::Context> context_; |
| 565 | }; |
| 566 | |
| 567 | // Locker inside an Unlocker inside a Locker. |
| 568 | TEST(LockUnlockLockMultithreaded) { |
mstarzinger@chromium.org | f8c6bd5 | 2011-11-23 12:13:52 +0000 | [diff] [blame] | 569 | #ifdef V8_TARGET_ARCH_MIPS |
| 570 | const int kNThreads = 50; |
| 571 | #else |
lrn@chromium.org | 0040ca9 | 2011-05-09 10:42:56 +0000 | [diff] [blame] | 572 | const int kNThreads = 100; |
mstarzinger@chromium.org | f8c6bd5 | 2011-11-23 12:13:52 +0000 | [diff] [blame] | 573 | #endif |
lrn@chromium.org | 0040ca9 | 2011-05-09 10:42:56 +0000 | [diff] [blame] | 574 | v8::Isolate* isolate = v8::Isolate::New(); |
danno@chromium.org | ca29dd8 | 2013-04-26 11:59:48 +0000 | [diff] [blame] | 575 | i::List<JoinableThread*> threads(kNThreads); |
lrn@chromium.org | 0040ca9 | 2011-05-09 10:42:56 +0000 | [diff] [blame] | 576 | { |
| 577 | v8::Locker locker_(isolate); |
| 578 | v8::Isolate::Scope isolate_scope(isolate); |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 579 | v8::HandleScope handle_scope(isolate); |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame^] | 580 | v8::Handle<v8::Context> context = v8::Context::New(isolate); |
danno@chromium.org | ca29dd8 | 2013-04-26 11:59:48 +0000 | [diff] [blame] | 581 | for (int i = 0; i < kNThreads; i++) { |
| 582 | threads.Add(new LockUnlockLockThread( |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame^] | 583 | isolate, context)); |
danno@chromium.org | ca29dd8 | 2013-04-26 11:59:48 +0000 | [diff] [blame] | 584 | } |
lrn@chromium.org | 0040ca9 | 2011-05-09 10:42:56 +0000 | [diff] [blame] | 585 | } |
| 586 | StartJoinAndDeleteThreads(threads); |
hpayer@chromium.org | 7c3372b | 2013-02-13 17:26:04 +0000 | [diff] [blame] | 587 | isolate->Dispose(); |
lrn@chromium.org | 0040ca9 | 2011-05-09 10:42:56 +0000 | [diff] [blame] | 588 | } |
| 589 | |
| 590 | class LockUnlockLockDefaultIsolateThread : public JoinableThread { |
| 591 | public: |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame^] | 592 | explicit LockUnlockLockDefaultIsolateThread(v8::Handle<v8::Context> context) |
danno@chromium.org | ca29dd8 | 2013-04-26 11:59:48 +0000 | [diff] [blame] | 593 | : JoinableThread("LockUnlockLockDefaultIsolateThread"), |
| 594 | context_(CcTest::default_isolate(), context) {} |
lrn@chromium.org | 0040ca9 | 2011-05-09 10:42:56 +0000 | [diff] [blame] | 595 | |
| 596 | virtual void Run() { |
yangguo@chromium.org | 46a2a51 | 2013-01-18 16:29:40 +0000 | [diff] [blame] | 597 | v8::Locker lock1(CcTest::default_isolate()); |
lrn@chromium.org | 0040ca9 | 2011-05-09 10:42:56 +0000 | [diff] [blame] | 598 | { |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 599 | v8::HandleScope handle_scope(CcTest::default_isolate()); |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame^] | 600 | v8::Context::Scope context_scope(CcTest::default_isolate(), context_); |
lrn@chromium.org | 0040ca9 | 2011-05-09 10:42:56 +0000 | [diff] [blame] | 601 | CalcFibAndCheck(); |
| 602 | } |
| 603 | { |
yangguo@chromium.org | 46a2a51 | 2013-01-18 16:29:40 +0000 | [diff] [blame] | 604 | v8::Unlocker unlock1(CcTest::default_isolate()); |
lrn@chromium.org | 0040ca9 | 2011-05-09 10:42:56 +0000 | [diff] [blame] | 605 | { |
yangguo@chromium.org | 46a2a51 | 2013-01-18 16:29:40 +0000 | [diff] [blame] | 606 | v8::Locker lock2(CcTest::default_isolate()); |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 607 | v8::HandleScope handle_scope(CcTest::default_isolate()); |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame^] | 608 | v8::Context::Scope context_scope(CcTest::default_isolate(), context_); |
lrn@chromium.org | 0040ca9 | 2011-05-09 10:42:56 +0000 | [diff] [blame] | 609 | CalcFibAndCheck(); |
| 610 | } |
| 611 | } |
| 612 | } |
| 613 | |
| 614 | private: |
| 615 | v8::Persistent<v8::Context> context_; |
| 616 | }; |
| 617 | |
| 618 | // Locker inside an Unlocker inside a Locker for default isolate. |
| 619 | TEST(LockUnlockLockDefaultIsolateMultithreaded) { |
mstarzinger@chromium.org | f8c6bd5 | 2011-11-23 12:13:52 +0000 | [diff] [blame] | 620 | #ifdef V8_TARGET_ARCH_MIPS |
| 621 | const int kNThreads = 50; |
| 622 | #else |
lrn@chromium.org | 0040ca9 | 2011-05-09 10:42:56 +0000 | [diff] [blame] | 623 | const int kNThreads = 100; |
mstarzinger@chromium.org | f8c6bd5 | 2011-11-23 12:13:52 +0000 | [diff] [blame] | 624 | #endif |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame^] | 625 | Local<v8::Context> context; |
danno@chromium.org | ca29dd8 | 2013-04-26 11:59:48 +0000 | [diff] [blame] | 626 | i::List<JoinableThread*> threads(kNThreads); |
lrn@chromium.org | 0040ca9 | 2011-05-09 10:42:56 +0000 | [diff] [blame] | 627 | { |
yangguo@chromium.org | 46a2a51 | 2013-01-18 16:29:40 +0000 | [diff] [blame] | 628 | v8::Locker locker_(CcTest::default_isolate()); |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 629 | v8::HandleScope handle_scope(CcTest::default_isolate()); |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame^] | 630 | context = v8::Context::New(CcTest::default_isolate()); |
danno@chromium.org | ca29dd8 | 2013-04-26 11:59:48 +0000 | [diff] [blame] | 631 | for (int i = 0; i < kNThreads; i++) { |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame^] | 632 | threads.Add(new LockUnlockLockDefaultIsolateThread(context)); |
danno@chromium.org | ca29dd8 | 2013-04-26 11:59:48 +0000 | [diff] [blame] | 633 | } |
lrn@chromium.org | 0040ca9 | 2011-05-09 10:42:56 +0000 | [diff] [blame] | 634 | } |
| 635 | StartJoinAndDeleteThreads(threads); |
| 636 | } |
jkummerow@chromium.org | e297f59 | 2011-06-08 10:05:15 +0000 | [diff] [blame] | 637 | |
| 638 | |
| 639 | TEST(Regress1433) { |
| 640 | for (int i = 0; i < 10; i++) { |
| 641 | v8::Isolate* isolate = v8::Isolate::New(); |
| 642 | { |
| 643 | v8::Locker lock(isolate); |
| 644 | v8::Isolate::Scope isolate_scope(isolate); |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 645 | v8::HandleScope handle_scope(isolate); |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame^] | 646 | v8::Handle<Context> context = v8::Context::New(isolate); |
jkummerow@chromium.org | e297f59 | 2011-06-08 10:05:15 +0000 | [diff] [blame] | 647 | v8::Context::Scope context_scope(context); |
| 648 | v8::Handle<String> source = v8::String::New("1+1"); |
| 649 | v8::Handle<Script> script = v8::Script::Compile(source); |
| 650 | v8::Handle<Value> result = script->Run(); |
| 651 | v8::String::AsciiValue ascii(result); |
jkummerow@chromium.org | e297f59 | 2011-06-08 10:05:15 +0000 | [diff] [blame] | 652 | } |
| 653 | isolate->Dispose(); |
| 654 | } |
| 655 | } |
ricow@chromium.org | 27bf288 | 2011-11-17 08:34:43 +0000 | [diff] [blame] | 656 | |
| 657 | |
| 658 | static const char* kSimpleExtensionSource = |
| 659 | "(function Foo() {" |
| 660 | " return 4;" |
| 661 | "})() "; |
| 662 | |
| 663 | class IsolateGenesisThread : public JoinableThread { |
| 664 | public: |
| 665 | IsolateGenesisThread(int count, const char* extension_names[]) |
| 666 | : JoinableThread("IsolateGenesisThread"), |
| 667 | count_(count), |
| 668 | extension_names_(extension_names) |
| 669 | {} |
| 670 | |
| 671 | virtual void Run() { |
| 672 | v8::Isolate* isolate = v8::Isolate::New(); |
| 673 | { |
| 674 | v8::Isolate::Scope isolate_scope(isolate); |
| 675 | CHECK(!i::Isolate::Current()->has_installed_extensions()); |
| 676 | v8::ExtensionConfiguration extensions(count_, extension_names_); |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame^] | 677 | v8::HandleScope handle_scope(isolate); |
| 678 | v8::Context::New(isolate, &extensions); |
ricow@chromium.org | 27bf288 | 2011-11-17 08:34:43 +0000 | [diff] [blame] | 679 | CHECK(i::Isolate::Current()->has_installed_extensions()); |
ricow@chromium.org | 27bf288 | 2011-11-17 08:34:43 +0000 | [diff] [blame] | 680 | } |
| 681 | isolate->Dispose(); |
| 682 | } |
| 683 | private: |
| 684 | int count_; |
| 685 | const char** extension_names_; |
| 686 | }; |
| 687 | |
| 688 | // Test installing extensions in separate isolates concurrently. |
| 689 | // http://code.google.com/p/v8/issues/detail?id=1821 |
| 690 | TEST(ExtensionsRegistration) { |
mstarzinger@chromium.org | f8c6bd5 | 2011-11-23 12:13:52 +0000 | [diff] [blame] | 691 | #if defined(V8_TARGET_ARCH_ARM) || defined(V8_TARGET_ARCH_MIPS) |
| 692 | const int kNThreads = 10; |
| 693 | #else |
ricow@chromium.org | 27bf288 | 2011-11-17 08:34:43 +0000 | [diff] [blame] | 694 | const int kNThreads = 40; |
mstarzinger@chromium.org | f8c6bd5 | 2011-11-23 12:13:52 +0000 | [diff] [blame] | 695 | #endif |
ricow@chromium.org | 27bf288 | 2011-11-17 08:34:43 +0000 | [diff] [blame] | 696 | v8::RegisterExtension(new v8::Extension("test0", |
| 697 | kSimpleExtensionSource)); |
| 698 | v8::RegisterExtension(new v8::Extension("test1", |
| 699 | kSimpleExtensionSource)); |
| 700 | v8::RegisterExtension(new v8::Extension("test2", |
| 701 | kSimpleExtensionSource)); |
| 702 | v8::RegisterExtension(new v8::Extension("test3", |
| 703 | kSimpleExtensionSource)); |
| 704 | v8::RegisterExtension(new v8::Extension("test4", |
| 705 | kSimpleExtensionSource)); |
| 706 | v8::RegisterExtension(new v8::Extension("test5", |
| 707 | kSimpleExtensionSource)); |
| 708 | v8::RegisterExtension(new v8::Extension("test6", |
| 709 | kSimpleExtensionSource)); |
| 710 | v8::RegisterExtension(new v8::Extension("test7", |
| 711 | kSimpleExtensionSource)); |
| 712 | const char* extension_names[] = { "test0", "test1", |
| 713 | "test2", "test3", "test4", |
| 714 | "test5", "test6", "test7" }; |
| 715 | i::List<JoinableThread*> threads(kNThreads); |
| 716 | for (int i = 0; i < kNThreads; i++) { |
| 717 | threads.Add(new IsolateGenesisThread(8, extension_names)); |
| 718 | } |
| 719 | StartJoinAndDeleteThreads(threads); |
| 720 | } |