| OLD | NEW |
| 1 // Copyright 2008 the V8 project authors. All rights reserved. | 1 // Copyright 2008 the V8 project authors. All rights reserved. |
| 2 // Redistribution and use in source and binary forms, with or without | 2 // Redistribution and use in source and binary forms, with or without |
| 3 // modification, are permitted provided that the following conditions are | 3 // modification, are permitted provided that the following conditions are |
| 4 // met: | 4 // met: |
| 5 // | 5 // |
| 6 // * Redistributions of source code must retain the above copyright | 6 // * Redistributions of source code must retain the above copyright |
| 7 // notice, this list of conditions and the following disclaimer. | 7 // notice, this list of conditions and the following disclaimer. |
| 8 // * Redistributions in binary form must reproduce the above | 8 // * Redistributions in binary form must reproduce the above |
| 9 // copyright notice, this list of conditions and the following | 9 // copyright notice, this list of conditions and the following |
| 10 // disclaimer in the documentation and/or other materials provided | 10 // disclaimer in the documentation and/or other materials provided |
| (...skipping 28 matching lines...) Expand all Loading... |
| 39 | 39 |
| 40 // Track whether this V8 instance has ever called v8::Locker. This allows the | 40 // Track whether this V8 instance has ever called v8::Locker. This allows the |
| 41 // API code to verify that the lock is always held when V8 is being entered. | 41 // API code to verify that the lock is always held when V8 is being entered. |
| 42 bool Locker::active_ = false; | 42 bool Locker::active_ = false; |
| 43 | 43 |
| 44 | 44 |
| 45 // Constructor for the Locker object. Once the Locker is constructed the | 45 // Constructor for the Locker object. Once the Locker is constructed the |
| 46 // current thread will be guaranteed to have the lock for a given isolate. | 46 // current thread will be guaranteed to have the lock for a given isolate. |
| 47 Locker::Locker(v8::Isolate* isolate) | 47 Locker::Locker(v8::Isolate* isolate) |
| 48 : has_lock_(false), | 48 : has_lock_(false), |
| 49 top_level_(false), | 49 top_level_(true), |
| 50 isolate_(reinterpret_cast<i::Isolate*>(isolate)) { | 50 isolate_(reinterpret_cast<i::Isolate*>(isolate)) { |
| 51 if (isolate_ == NULL) { | 51 if (isolate_ == NULL) { |
| 52 isolate_ = i::Isolate::GetDefaultIsolateForLocking(); | 52 isolate_ = i::Isolate::GetDefaultIsolateForLocking(); |
| 53 } | 53 } |
| 54 // Record that the Locker has been used at least once. | 54 // Record that the Locker has been used at least once. |
| 55 active_ = true; | 55 active_ = true; |
| 56 // Get the big lock if necessary. | 56 // Get the big lock if necessary. |
| 57 if (!isolate_->thread_manager()->IsLockedByCurrentThread()) { | 57 if (!isolate_->thread_manager()->IsLockedByCurrentThread()) { |
| 58 isolate_->thread_manager()->Lock(); | 58 isolate_->thread_manager()->Lock(); |
| 59 has_lock_ = true; | 59 has_lock_ = true; |
| (...skipping 334 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 394 state != NULL; | 394 state != NULL; |
| 395 state = state->Next()) { | 395 state = state->Next()) { |
| 396 if (thread_id.Equals(state->id())) { | 396 if (thread_id.Equals(state->id())) { |
| 397 state->set_terminate_on_restore(true); | 397 state->set_terminate_on_restore(true); |
| 398 } | 398 } |
| 399 } | 399 } |
| 400 } | 400 } |
| 401 | 401 |
| 402 | 402 |
| 403 ContextSwitcher::ContextSwitcher(Isolate* isolate, int every_n_ms) | 403 ContextSwitcher::ContextSwitcher(Isolate* isolate, int every_n_ms) |
| 404 : Thread(isolate, "v8:CtxtSwitcher"), | 404 : Thread("v8:CtxtSwitcher"), |
| 405 keep_going_(true), | 405 keep_going_(true), |
| 406 sleep_ms_(every_n_ms) { | 406 sleep_ms_(every_n_ms), |
| 407 isolate_(isolate) { |
| 407 } | 408 } |
| 408 | 409 |
| 409 | 410 |
| 410 // Set the scheduling interval of V8 threads. This function starts the | 411 // Set the scheduling interval of V8 threads. This function starts the |
| 411 // ContextSwitcher thread if needed. | 412 // ContextSwitcher thread if needed. |
| 412 void ContextSwitcher::StartPreemption(int every_n_ms) { | 413 void ContextSwitcher::StartPreemption(int every_n_ms) { |
| 413 Isolate* isolate = Isolate::Current(); | 414 Isolate* isolate = Isolate::Current(); |
| 414 ASSERT(Locker::IsLocked()); | 415 ASSERT(Locker::IsLocked(reinterpret_cast<v8::Isolate*>(isolate))); |
| 415 if (isolate->context_switcher() == NULL) { | 416 if (isolate->context_switcher() == NULL) { |
| 416 // If the ContextSwitcher thread is not running at the moment start it now. | 417 // If the ContextSwitcher thread is not running at the moment start it now. |
| 417 isolate->set_context_switcher(new ContextSwitcher(isolate, every_n_ms)); | 418 isolate->set_context_switcher(new ContextSwitcher(isolate, every_n_ms)); |
| 418 isolate->context_switcher()->Start(); | 419 isolate->context_switcher()->Start(); |
| 419 } else { | 420 } else { |
| 420 // ContextSwitcher thread is already running, so we just change the | 421 // ContextSwitcher thread is already running, so we just change the |
| 421 // scheduling interval. | 422 // scheduling interval. |
| 422 isolate->context_switcher()->sleep_ms_ = every_n_ms; | 423 isolate->context_switcher()->sleep_ms_ = every_n_ms; |
| 423 } | 424 } |
| 424 } | 425 } |
| 425 | 426 |
| 426 | 427 |
| 427 // Disable preemption of V8 threads. If multiple threads want to use V8 they | 428 // Disable preemption of V8 threads. If multiple threads want to use V8 they |
| 428 // must cooperatively schedule amongst them from this point on. | 429 // must cooperatively schedule amongst them from this point on. |
| 429 void ContextSwitcher::StopPreemption() { | 430 void ContextSwitcher::StopPreemption() { |
| 430 Isolate* isolate = Isolate::Current(); | 431 Isolate* isolate = Isolate::Current(); |
| 431 ASSERT(Locker::IsLocked()); | 432 ASSERT(Locker::IsLocked(reinterpret_cast<v8::Isolate*>(isolate))); |
| 432 if (isolate->context_switcher() != NULL) { | 433 if (isolate->context_switcher() != NULL) { |
| 433 // The ContextSwitcher thread is running. We need to stop it and release | 434 // The ContextSwitcher thread is running. We need to stop it and release |
| 434 // its resources. | 435 // its resources. |
| 435 isolate->context_switcher()->keep_going_ = false; | 436 isolate->context_switcher()->keep_going_ = false; |
| 436 // Wait for the ContextSwitcher thread to exit. | 437 // Wait for the ContextSwitcher thread to exit. |
| 437 isolate->context_switcher()->Join(); | 438 isolate->context_switcher()->Join(); |
| 438 // Thread has exited, now we can delete it. | 439 // Thread has exited, now we can delete it. |
| 439 delete(isolate->context_switcher()); | 440 delete(isolate->context_switcher()); |
| 440 isolate->set_context_switcher(NULL); | 441 isolate->set_context_switcher(NULL); |
| 441 } | 442 } |
| (...skipping 13 matching lines...) Expand all Loading... |
| 455 // Acknowledge the preemption by the receiving thread. | 456 // Acknowledge the preemption by the receiving thread. |
| 456 void ContextSwitcher::PreemptionReceived() { | 457 void ContextSwitcher::PreemptionReceived() { |
| 457 ASSERT(Locker::IsLocked()); | 458 ASSERT(Locker::IsLocked()); |
| 458 // There is currently no accounting being done for this. But could be in the | 459 // There is currently no accounting being done for this. But could be in the |
| 459 // future, which is why we leave this in. | 460 // future, which is why we leave this in. |
| 460 } | 461 } |
| 461 | 462 |
| 462 | 463 |
| 463 } // namespace internal | 464 } // namespace internal |
| 464 } // namespace v8 | 465 } // namespace v8 |
| OLD | NEW |