| OLD | NEW |
| 1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
| 3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 | 4 |
| 5 #include "vm/thread_registry.h" | 5 #include "vm/thread_registry.h" |
| 6 | 6 |
| 7 #include "vm/isolate.h" | 7 #include "vm/isolate.h" |
| 8 #include "vm/lockers.h" | 8 #include "vm/lockers.h" |
| 9 | 9 |
| 10 namespace dart { | 10 namespace dart { |
| 11 | 11 |
| 12 void ThreadRegistry::SafepointThreads() { | 12 SafepointId ThreadRegistry::SafepointThreads() { |
| 13 MonitorLocker ml(monitor_); | 13 MonitorLocker ml(monitor_); |
| 14 // First wait for any older rounds that are still in progress. | 14 // First wait for any older rounds that are still in progress. |
| 15 while (in_rendezvous_) { | 15 while (in_rendezvous_) { |
| 16 // Assert we are not the organizer trying to nest calls to SafepointThreads. | 16 // Assert we are not the organizer trying to nest calls to SafepointThreads. |
| 17 ASSERT(remaining_ > 0); | 17 ASSERT(remaining_ > 0); |
| 18 CheckSafepointLocked(); | 18 CheckSafepointLocked(); |
| 19 } | 19 } |
| 20 // Start a new round. | 20 // Start a new round. |
| 21 in_rendezvous_ = true; | 21 in_rendezvous_ = true; |
| 22 ++round_; // Overflows after 240+ years @ 10^9 safepoints per second. | 22 ++round_; // Overflows after 240+ years @ 10^9 safepoints per second. |
| 23 remaining_ = CountScheduledLocked(); | 23 // Count the number of threads that should participate in this round. |
| 24 remaining_ = 0; |
| 25 for (int i = 0; i < entries_.length(); ++i) { |
| 26 const Entry& entry = entries_[i]; |
| 27 if (entry.scheduled) { |
| 28 // All threads that are running should participate. |
| 29 ASSERT(IsParticipant(entry.thread)); |
| 30 ++remaining_; |
| 31 } |
| 32 } |
| 24 Isolate* isolate = Isolate::Current(); | 33 Isolate* isolate = Isolate::Current(); |
| 25 // We only expect this method to be called from within the isolate itself. | 34 // We only expect this method to be called from within the isolate itself. |
| 26 ASSERT(isolate->thread_registry() == this); | 35 ASSERT(isolate->thread_registry() == this); |
| 27 // TODO(koda): Rename Thread::PrepareForGC and call it here? | 36 // TODO(koda): Rename Thread::PrepareForGC and call it here? |
| 28 --remaining_; // Exclude this thread from the count. | 37 --remaining_; // Exclude this thread from the count. |
| 29 // Ensure the main mutator will reach a safepoint (could be running Dart). | 38 // Ensure the main mutator will reach a safepoint (could be running Dart). |
| 30 if (!isolate->MutatorThreadIsCurrentThread()) { | 39 if (!isolate->MutatorThreadIsCurrentThread()) { |
| 31 isolate->ScheduleInterrupts(Isolate::kVMInterrupt); | 40 isolate->ScheduleInterrupts(Isolate::kVMInterrupt); |
| 32 } | 41 } |
| 33 while (remaining_ > 0) { | 42 while (remaining_ > 0) { |
| 34 ml.Wait(Monitor::kNoTimeout); | 43 ml.Wait(Monitor::kNoTimeout); |
| 35 } | 44 } |
| 45 return round_; |
| 36 } | 46 } |
| 37 | 47 |
| 38 | 48 |
| 39 void ThreadRegistry::ResumeAllThreads() { | 49 void ThreadRegistry::ResumeAllThreads() { |
| 40 MonitorLocker ml(monitor_); | 50 MonitorLocker ml(monitor_); |
| 41 ASSERT(in_rendezvous_); | 51 ASSERT(in_rendezvous_); |
| 42 in_rendezvous_ = false; | 52 in_rendezvous_ = false; |
| 43 ml.NotifyAll(); | 53 ml.NotifyAll(); |
| 44 } | 54 } |
| 45 | 55 |
| (...skipping 16 matching lines...) Expand all Loading... |
| 62 monitor_->Wait(Monitor::kNoTimeout); | 72 monitor_->Wait(Monitor::kNoTimeout); |
| 63 // Note: Here, round_ is needed to detect and distinguish two cases: | 73 // Note: Here, round_ is needed to detect and distinguish two cases: |
| 64 // a) The old rendezvous is still in progress, so just keep waiting, or | 74 // a) The old rendezvous is still in progress, so just keep waiting, or |
| 65 // b) after ResumeAllThreads, another call to SafepointThreads was | 75 // b) after ResumeAllThreads, another call to SafepointThreads was |
| 66 // made before this thread got a chance to reaquire monitor_, thus this | 76 // made before this thread got a chance to reaquire monitor_, thus this |
| 67 // thread should (again) decrease remaining_ to indicate cooperation in | 77 // thread should (again) decrease remaining_ to indicate cooperation in |
| 68 // this new round. | 78 // this new round. |
| 69 } | 79 } |
| 70 } | 80 } |
| 71 | 81 |
| 72 | |
| 73 intptr_t ThreadRegistry::CountScheduledLocked() { | |
| 74 intptr_t count = 0; | |
| 75 for (int i = 0; i < entries_.length(); ++i) { | |
| 76 const Entry& entry = entries_[i]; | |
| 77 if (entry.scheduled) { | |
| 78 ++count; | |
| 79 } | |
| 80 } | |
| 81 return count; | |
| 82 } | |
| 83 | |
| 84 } // namespace dart | 82 } // namespace dart |
| OLD | NEW |