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