| 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 #ifndef VM_THREAD_REGISTRY_H_ | 5 #ifndef VM_THREAD_REGISTRY_H_ |
| 6 #define VM_THREAD_REGISTRY_H_ | 6 #define VM_THREAD_REGISTRY_H_ |
| 7 | 7 |
| 8 #include "vm/globals.h" | 8 #include "vm/globals.h" |
| 9 #include "vm/growable_array.h" | 9 #include "vm/growable_array.h" |
| 10 #include "vm/isolate.h" | 10 #include "vm/isolate.h" |
| 11 #include "vm/lockers.h" | 11 #include "vm/lockers.h" |
| 12 #include "vm/thread.h" | 12 #include "vm/thread.h" |
| 13 | 13 |
| 14 namespace dart { | 14 namespace dart { |
| 15 | 15 |
| 16 // Unordered collection of threads relating to a particular isolate. | 16 // Unordered collection of threads relating to a particular isolate. |
| 17 class ThreadRegistry { | 17 class ThreadRegistry { |
| 18 public: | 18 public: |
| 19 ThreadRegistry() : mutex_(new Mutex()), entries_() {} | 19 ThreadRegistry() |
| 20 : monitor_(new Monitor()), |
| 21 entries_(), |
| 22 in_rendezvous_(false), |
| 23 remaining_(0), |
| 24 round_(0) {} |
| 25 |
| 26 // Bring all threads in this isolate to a safepoint. The caller is |
| 27 // expected to be implicitly at a safepoint. The threads will wait |
| 28 // until ResumeAllThreads is called. Must be called at a safepoint, |
| 29 // since it first waits for any already pending requests. Any thread |
| 30 // that tries to enter/exit this isolate during rendezvous will wait |
| 31 // in RestoreStateTo/SaveStateFrom, respectively. |
| 32 void SafepointThreads(); |
| 33 |
| 34 // Unblocks all threads participating in the rendezvous that was organized |
| 35 // by a prior call to SafepointThreads. |
| 36 // TODO(koda): Consider adding a scope helper to avoid omitting this call. |
| 37 void ResumeAllThreads(); |
| 38 |
| 39 // Indicate that the current thread is at a safepoint, and offer to wait for |
| 40 // any pending rendezvous request (if none, returns immediately). |
| 41 void CheckSafepoint() { |
| 42 MonitorLocker ml(monitor_); |
| 43 CheckSafepointLocked(); |
| 44 } |
| 20 | 45 |
| 21 bool RestoreStateTo(Thread* thread, Thread::State* state) { | 46 bool RestoreStateTo(Thread* thread, Thread::State* state) { |
| 22 MutexLocker ml(mutex_); | 47 MonitorLocker ml(monitor_); |
| 48 // Wait for any rendezvous in progress. |
| 49 while (in_rendezvous_) { |
| 50 ml.Wait(Monitor::kNoTimeout); |
| 51 } |
| 23 Entry* entry = FindEntry(thread); | 52 Entry* entry = FindEntry(thread); |
| 24 if (entry != NULL) { | 53 if (entry != NULL) { |
| 25 Thread::State st = entry->state; | 54 Thread::State st = entry->state; |
| 26 // TODO(koda): Support same thread re-entering same isolate with | 55 // TODO(koda): Support same thread re-entering same isolate with |
| 27 // Dart frames in between. For now, just assert it doesn't happen. | 56 // Dart frames in between. For now, just assert it doesn't happen. |
| 28 if (st.top_exit_frame_info != thread->top_exit_frame_info()) { | 57 if (st.top_exit_frame_info != thread->top_exit_frame_info()) { |
| 29 ASSERT(thread->top_exit_frame_info() == 0 || | 58 ASSERT(thread->top_exit_frame_info() == 0 || |
| 30 thread->top_exit_frame_info() > st.top_exit_frame_info); | 59 thread->top_exit_frame_info() > st.top_exit_frame_info); |
| 31 } | 60 } |
| 32 ASSERT(!entry->scheduled); | 61 ASSERT(!entry->scheduled); |
| (...skipping 10 matching lines...) Expand all Loading... |
| 43 new_entry.scheduled = true; | 72 new_entry.scheduled = true; |
| 44 #if defined(DEBUG) | 73 #if defined(DEBUG) |
| 45 // State field is not in use, so zap it. | 74 // State field is not in use, so zap it. |
| 46 memset(&new_entry.state, 0xda, sizeof(new_entry.state)); | 75 memset(&new_entry.state, 0xda, sizeof(new_entry.state)); |
| 47 #endif | 76 #endif |
| 48 entries_.Add(new_entry); | 77 entries_.Add(new_entry); |
| 49 return false; | 78 return false; |
| 50 } | 79 } |
| 51 | 80 |
| 52 void SaveStateFrom(Thread* thread, const Thread::State& state) { | 81 void SaveStateFrom(Thread* thread, const Thread::State& state) { |
| 53 MutexLocker ml(mutex_); | 82 MonitorLocker ml(monitor_); |
| 83 // Exiting an isolate must always be a safepoint. |
| 84 CheckSafepointLocked(); |
| 54 Entry* entry = FindEntry(thread); | 85 Entry* entry = FindEntry(thread); |
| 55 ASSERT(entry != NULL); | 86 ASSERT(entry != NULL); |
| 56 ASSERT(entry->scheduled); | 87 ASSERT(entry->scheduled); |
| 57 entry->scheduled = false; | 88 entry->scheduled = false; |
| 58 entry->state = state; | 89 entry->state = state; |
| 59 } | 90 } |
| 60 | 91 |
| 61 bool Contains(Thread* thread) { | 92 bool Contains(Thread* thread) { |
| 62 MutexLocker ml(mutex_); | 93 MonitorLocker ml(monitor_); |
| 63 return (FindEntry(thread) != NULL); | 94 return (FindEntry(thread) != NULL); |
| 64 } | 95 } |
| 65 | 96 |
| 66 void CheckNotScheduled(Isolate* isolate) { | 97 void CheckNotScheduled(Isolate* isolate) { |
| 67 MutexLocker ml(mutex_); | 98 MonitorLocker ml(monitor_); |
| 68 for (int i = 0; i < entries_.length(); ++i) { | 99 for (int i = 0; i < entries_.length(); ++i) { |
| 69 const Entry& entry = entries_[i]; | 100 const Entry& entry = entries_[i]; |
| 70 if (entry.scheduled) { | 101 if (entry.scheduled) { |
| 71 FATAL3("Isolate %p still scheduled on %p (whose isolate_ is %p)\n", | 102 FATAL3("Isolate %p still scheduled on %p (whose isolate_ is %p)\n", |
| 72 isolate, | 103 isolate, |
| 73 entry.thread, | 104 entry.thread, |
| 74 entry.thread->isolate()); | 105 entry.thread->isolate()); |
| 75 } | 106 } |
| 76 } | 107 } |
| 77 } | 108 } |
| 78 | 109 |
| 79 void VisitObjectPointers(ObjectPointerVisitor* visitor) { | 110 void VisitObjectPointers(ObjectPointerVisitor* visitor) { |
| 80 MutexLocker ml(mutex_); | 111 MonitorLocker ml(monitor_); |
| 81 for (int i = 0; i < entries_.length(); ++i) { | 112 for (int i = 0; i < entries_.length(); ++i) { |
| 82 const Entry& entry = entries_[i]; | 113 const Entry& entry = entries_[i]; |
| 83 Zone* zone = entry.scheduled ? entry.thread->zone() : entry.state.zone; | 114 Zone* zone = entry.scheduled ? entry.thread->zone() : entry.state.zone; |
| 84 if (zone != NULL) { | 115 if (zone != NULL) { |
| 85 zone->VisitObjectPointers(visitor); | 116 zone->VisitObjectPointers(visitor); |
| 86 } | 117 } |
| 87 } | 118 } |
| 88 } | 119 } |
| 89 | 120 |
| 90 private: | 121 private: |
| 91 struct Entry { | 122 struct Entry { |
| 92 Thread* thread; | 123 Thread* thread; |
| 93 bool scheduled; | 124 bool scheduled; |
| 94 Thread::State state; | 125 Thread::State state; |
| 95 }; | 126 }; |
| 96 | 127 |
| 97 // Returns Entry corresponding to thread in registry or NULL. | 128 // Returns Entry corresponding to thread in registry or NULL. |
| 98 // Note: Lock should be taken before this function is called. | 129 // Note: Lock should be taken before this function is called. |
| 130 // TODO(koda): Add method Monitor::IsOwnedByCurrentThread. |
| 99 Entry* FindEntry(Thread* thread) { | 131 Entry* FindEntry(Thread* thread) { |
| 100 DEBUG_ASSERT(mutex_->IsOwnedByCurrentThread()); | |
| 101 for (int i = 0; i < entries_.length(); ++i) { | 132 for (int i = 0; i < entries_.length(); ++i) { |
| 102 if (entries_[i].thread == thread) { | 133 if (entries_[i].thread == thread) { |
| 103 return &entries_[i]; | 134 return &entries_[i]; |
| 104 } | 135 } |
| 105 } | 136 } |
| 106 return NULL; | 137 return NULL; |
| 107 } | 138 } |
| 108 | 139 |
| 109 Mutex* mutex_; | 140 // Note: Lock should be taken before this function is called. |
| 141 void CheckSafepointLocked(); |
| 142 |
| 143 // Returns the number threads that are scheduled on this isolate. |
| 144 // Note: Lock should be taken before this function is called. |
| 145 intptr_t CountScheduledLocked(); |
| 146 |
| 147 Monitor* monitor_; // All access is synchronized through this monitor. |
| 110 MallocGrowableArray<Entry> entries_; | 148 MallocGrowableArray<Entry> entries_; |
| 111 | 149 |
| 150 // Safepoint rendezvous state. |
| 151 bool in_rendezvous_; // A safepoint rendezvous request is in progress. |
| 152 intptr_t remaining_; // Number of threads yet to reach their safepoint. |
| 153 int64_t round_; // Counter, to prevent missing updates to remaining_ |
| 154 // (see comments in CheckSafepointLocked). |
| 112 | 155 |
| 113 DISALLOW_COPY_AND_ASSIGN(ThreadRegistry); | 156 DISALLOW_COPY_AND_ASSIGN(ThreadRegistry); |
| 114 }; | 157 }; |
| 115 | 158 |
| 116 } // namespace dart | 159 } // namespace dart |
| 117 | 160 |
| 118 #endif // VM_THREAD_REGISTRY_H_ | 161 #endif // VM_THREAD_REGISTRY_H_ |
| OLD | NEW |