Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
thread_registry.cc
Go to the documentation of this 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
3// BSD-style license that can be found in the LICENSE file.
4
6
7#include "vm/json_stream.h"
8#include "vm/lockers.h"
9
10namespace dart {
11
13 // Go over the free thread list and delete the thread objects.
14 {
16 // At this point the active list should be empty.
17 ASSERT(active_list_ == nullptr);
18
19 // Now delete all the threads in the free list.
20 while (free_list_ != nullptr) {
21 Thread* thread = free_list_;
22 free_list_ = thread->next_;
23 delete thread;
24 }
25 }
26}
27
28Thread* ThreadRegistry::GetFreeThreadLocked(bool is_vm_isolate) {
29 ASSERT(threads_lock()->IsOwnedByCurrentThread());
30 Thread* thread = GetFromFreelistLocked(is_vm_isolate);
31 ASSERT(thread->api_top_scope() == nullptr);
32 // Now add this Thread to the active list for the isolate.
33 AddToActiveListLocked(thread);
34 return thread;
35}
36
37void ThreadRegistry::ReturnThreadLocked(Thread* thread) {
38 ASSERT(threads_lock()->IsOwnedByCurrentThread());
39 // Remove thread from the active list for the isolate.
40 RemoveFromActiveListLocked(thread);
41 ReturnToFreelistLocked(thread);
42}
43
45 IsolateGroup* isolate_group_of_interest,
46 ObjectPointerVisitor* visitor,
47 ValidationPolicy validate_frames) {
49 Thread* thread = active_list_;
50 while (thread != nullptr) {
51 if (thread->isolate_group() == isolate_group_of_interest) {
52 // The mutator thread is visited by the isolate itself (see
53 // [IsolateGroup::VisitStackPointers]).
54 if (!thread->IsDartMutatorThread()) {
55 thread->VisitObjectPointers(visitor, validate_frames);
56 }
57 }
58 thread = thread->next_;
59 }
60}
61
63 std::function<void(Thread* thread)> callback) {
65 Thread* thread = active_list_;
66 while (thread != nullptr) {
67 callback(thread);
68 thread = thread->next_;
69 }
70}
71
74 Thread* thread = active_list_;
75 while (thread != nullptr) {
76 if (!thread->BypassSafepoints()) {
77 thread->ReleaseStoreBuffer();
78 }
79 thread = thread->next_;
80 }
81}
82
85 Thread* thread = active_list_;
86 while (thread != nullptr) {
87 if (!thread->BypassSafepoints()) {
88 thread->MarkingStackAcquire();
89 thread->DeferredMarkingStackAcquire();
90 }
91 thread = thread->next_;
92 }
93}
94
97 Thread* thread = active_list_;
98 while (thread != nullptr) {
99 if (!thread->BypassSafepoints()) {
100 thread->MarkingStackRelease();
101 thread->DeferredMarkingStackRelease();
102 ASSERT(!thread->is_marking());
103 }
104 thread = thread->next_;
105 }
106}
107
110 Thread* thread = active_list_;
111 while (thread != nullptr) {
112 if (!thread->BypassSafepoints() && thread->is_marking()) {
113 thread->MarkingStackFlush();
114 thread->DeferredMarkingStackFlush();
115 ASSERT(thread->is_marking());
116 }
117 thread = thread->next_;
118 }
119}
120
121void ThreadRegistry::AddToActiveListLocked(Thread* thread) {
122 ASSERT(thread != nullptr);
123 ASSERT(threads_lock()->IsOwnedByCurrentThread());
124 thread->next_ = active_list_;
125 active_list_ = thread;
126 active_isolates_count_.fetch_add(1);
127}
128
129void ThreadRegistry::RemoveFromActiveListLocked(Thread* thread) {
130 ASSERT(thread != nullptr);
131 ASSERT(threads_lock()->IsOwnedByCurrentThread());
132 Thread* prev = nullptr;
133 Thread* current = active_list_;
134 while (current != nullptr) {
135 if (current == thread) {
136 if (prev == nullptr) {
137 active_list_ = current->next_;
138 } else {
139 prev->next_ = current->next_;
140 }
141 active_isolates_count_.fetch_sub(1);
142 break;
143 }
144 prev = current;
145 current = current->next_;
146 }
147}
148
149Thread* ThreadRegistry::GetFromFreelistLocked(bool is_vm_isolate) {
150 ASSERT(threads_lock()->IsOwnedByCurrentThread());
151 Thread* thread = nullptr;
152 // Get thread structure from free list or create a new one.
153 if (free_list_ == nullptr) {
154 thread = new Thread(is_vm_isolate);
155 } else {
156 thread = free_list_;
157 free_list_ = thread->next_;
158 }
159 return thread;
160}
161
162void ThreadRegistry::ReturnToFreelistLocked(Thread* thread) {
163 ASSERT(thread != nullptr);
164 ASSERT(thread->os_thread() == nullptr);
165 ASSERT(thread->isolate_ == nullptr);
166 ASSERT(thread->isolate_group_ == nullptr);
167 ASSERT(thread->field_table_values_ == nullptr);
168 ASSERT(threads_lock()->IsOwnedByCurrentThread());
169 // Add thread to the free list.
170 thread->next_ = free_list_;
171 free_list_ = thread;
172}
173
174} // namespace dart
static float prev(float f)
T fetch_add(T arg, std::memory_order order=std::memory_order_relaxed)
Definition atomic.h:35
T fetch_sub(T arg, std::memory_order order=std::memory_order_relaxed)
Definition atomic.h:38
void VisitObjectPointers(IsolateGroup *isolate_group_of_interest, ObjectPointerVisitor *visitor, ValidationPolicy validate_frames)
Monitor * threads_lock() const
void ForEachThread(std::function< void(Thread *thread)> callback)
ApiLocalScope * api_top_scope() const
Definition thread.h:512
bool is_marking() const
Definition thread.h:669
void VisitObjectPointers(ObjectPointerVisitor *visitor, ValidationPolicy validate_frames)
Definition thread.cc:901
void ReleaseStoreBuffer()
Definition thread.cc:672
bool IsDartMutatorThread() const
Definition thread.h:546
bool BypassSafepoints() const
Definition thread.h:994
IsolateGroup * isolate_group() const
Definition thread.h:540
#define ASSERT(E)
FlKeyEvent uint64_t FlKeyResponderAsyncCallback callback
ValidationPolicy
Definition thread.h:271