OLD | NEW |
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, 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 "include/dart_api.h" | 5 #include "include/dart_api.h" |
6 #include "platform/assert.h" | 6 #include "platform/assert.h" |
7 #include "vm/globals.h" | 7 #include "vm/globals.h" |
8 #include "vm/isolate.h" | 8 #include "vm/isolate.h" |
9 #include "vm/lockers.h" | 9 #include "vm/lockers.h" |
| 10 #include "vm/thread_barrier.h" |
10 #include "vm/thread_pool.h" | 11 #include "vm/thread_pool.h" |
11 #include "vm/unit_test.h" | 12 #include "vm/unit_test.h" |
12 | 13 |
13 namespace dart { | 14 namespace dart { |
14 | 15 |
15 UNIT_TEST_CASE(IsolateCurrent) { | 16 UNIT_TEST_CASE(IsolateCurrent) { |
16 Dart_Isolate isolate = Dart_CreateIsolate( | 17 Dart_Isolate isolate = Dart_CreateIsolate( |
17 NULL, NULL, bin::isolate_snapshot_buffer, NULL, NULL, NULL); | 18 NULL, NULL, bin::isolate_snapshot_buffer, NULL, NULL, NULL); |
18 EXPECT_EQ(isolate, Dart_CurrentIsolate()); | 19 EXPECT_EQ(isolate, Dart_CurrentIsolate()); |
19 Dart_ShutdownIsolate(); | 20 Dart_ShutdownIsolate(); |
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
82 EXPECT_VALID(exception_result); | 83 EXPECT_VALID(exception_result); |
83 } | 84 } |
84 | 85 |
85 | 86 |
86 class InterruptChecker : public ThreadPool::Task { | 87 class InterruptChecker : public ThreadPool::Task { |
87 public: | 88 public: |
88 static const intptr_t kTaskCount; | 89 static const intptr_t kTaskCount; |
89 static const intptr_t kIterations; | 90 static const intptr_t kIterations; |
90 | 91 |
91 InterruptChecker(Isolate* isolate, | 92 InterruptChecker(Isolate* isolate, |
92 Monitor* awake_monitor, | 93 ThreadBarrier* barrier) |
93 bool* awake, | |
94 Monitor* round_monitor, | |
95 const intptr_t* round) | |
96 : isolate_(isolate), | 94 : isolate_(isolate), |
97 awake_monitor_(awake_monitor), | 95 barrier_(barrier) { |
98 awake_(awake), | |
99 round_monitor_(round_monitor), | |
100 round_(round) { | |
101 } | 96 } |
102 | 97 |
103 virtual void Run() { | 98 virtual void Run() { |
104 Thread::EnterIsolateAsHelper(isolate_); | 99 Thread::EnterIsolateAsHelper(isolate_); |
105 // Tell main thread that we are ready. | 100 // Tell main thread that we are ready. |
106 { | 101 barrier_->Sync(); |
107 MonitorLocker ml(awake_monitor_); | |
108 ASSERT(!*awake_); | |
109 *awake_ = true; | |
110 ml.Notify(); | |
111 } | |
112 for (intptr_t i = 0; i < kIterations; ++i) { | 102 for (intptr_t i = 0; i < kIterations; ++i) { |
113 // Busy wait for interrupts. | 103 // Busy wait for interrupts. |
114 while (!isolate_->HasInterruptsScheduled(Isolate::kVMInterrupt)) { | 104 while (!isolate_->HasInterruptsScheduled(Isolate::kVMInterrupt)) { |
115 // Do nothing. | 105 // Do nothing. |
116 } | 106 } |
117 // Tell main thread that we observed the interrupt. | 107 // Tell main thread that we observed the interrupt. |
118 { | 108 barrier_->Sync(); |
119 MonitorLocker ml(awake_monitor_); | |
120 ASSERT(!*awake_); | |
121 *awake_ = true; | |
122 ml.Notify(); | |
123 } | |
124 // Wait for main thread to let us resume, i.e., until all tasks are here. | |
125 { | |
126 MonitorLocker ml(round_monitor_); | |
127 EXPECT(*round_ == i || *round_ == (i + 1)); | |
128 while (*round_ == i) { | |
129 ml.Wait(); | |
130 } | |
131 EXPECT(*round_ == i + 1); | |
132 } | |
133 } | 109 } |
134 Thread::ExitIsolateAsHelper(); | 110 Thread::ExitIsolateAsHelper(); |
135 // Use awake also to signal exit. | 111 barrier_->Exit(); |
136 { | |
137 MonitorLocker ml(awake_monitor_); | |
138 *awake_ = true; | |
139 ml.Notify(); | |
140 } | |
141 } | 112 } |
142 | 113 |
143 private: | 114 private: |
144 Isolate* isolate_; | 115 Isolate* isolate_; |
145 Monitor* awake_monitor_; | 116 ThreadBarrier* barrier_; |
146 bool* awake_; | |
147 Monitor* round_monitor_; | |
148 const intptr_t* round_; | |
149 }; | 117 }; |
150 | 118 |
151 | 119 |
152 const intptr_t InterruptChecker::kTaskCount = 5; | 120 const intptr_t InterruptChecker::kTaskCount = 5; |
153 const intptr_t InterruptChecker::kIterations = 10; | 121 const intptr_t InterruptChecker::kIterations = 10; |
154 | 122 |
155 | |
156 // Waits for all tasks to set their individual flag, then clears them all. | |
157 static void WaitForAllTasks(bool* flags, Monitor* monitor) { | |
158 MonitorLocker ml(monitor); | |
159 while (true) { | |
160 intptr_t count = 0; | |
161 for (intptr_t task = 0; task < InterruptChecker::kTaskCount; ++task) { | |
162 if (flags[task]) { | |
163 ++count; | |
164 } | |
165 } | |
166 if (count == InterruptChecker::kTaskCount) { | |
167 memset(flags, 0, sizeof(*flags) * count); | |
168 break; | |
169 } else { | |
170 ml.Wait(); | |
171 } | |
172 } | |
173 } | |
174 | |
175 // Test and document usage of Isolate::HasInterruptsScheduled. | 123 // Test and document usage of Isolate::HasInterruptsScheduled. |
176 // | 124 // |
177 // Go through a number of rounds of scheduling interrupts and waiting until all | 125 // Go through a number of rounds of scheduling interrupts and waiting until all |
178 // unsynchronized busy-waiting tasks observe it (in the current implementation, | 126 // unsynchronized busy-waiting tasks observe it (in the current implementation, |
179 // the exact latency depends on cache coherence). Synchronization is then used | 127 // the exact latency depends on cache coherence). Synchronization is then used |
180 // to ensure that the response to the interrupt, i.e., starting a new round, | 128 // to ensure that the response to the interrupt, i.e., starting a new round, |
181 // happens *after* the interrupt is observed. Without this synchronization, the | 129 // happens *after* the interrupt is observed. Without this synchronization, the |
182 // compiler and/or CPU could reorder operations to make the tasks observe the | 130 // compiler and/or CPU could reorder operations to make the tasks observe the |
183 // round update *before* the interrupt is set. | 131 // round update *before* the interrupt is set. |
184 TEST_CASE(StackLimitInterrupts) { | 132 TEST_CASE(StackLimitInterrupts) { |
185 Monitor awake_monitor; // Synchronizes the 'awake' flags. | |
186 bool awake[InterruptChecker::kTaskCount]; | |
187 memset(awake, 0, sizeof(awake)); | |
188 Monitor round_monitor; // Synchronizes the 'round' counter. | |
189 intptr_t round = 0; | |
190 Isolate* isolate = Thread::Current()->isolate(); | 133 Isolate* isolate = Thread::Current()->isolate(); |
| 134 ThreadBarrier barrier(InterruptChecker::kTaskCount + 1); |
191 // Start all tasks. They will busy-wait until interrupted in the first round. | 135 // Start all tasks. They will busy-wait until interrupted in the first round. |
192 for (intptr_t task = 0; task < InterruptChecker::kTaskCount; task++) { | 136 for (intptr_t task = 0; task < InterruptChecker::kTaskCount; task++) { |
193 Dart::thread_pool()->Run(new InterruptChecker( | 137 Dart::thread_pool()->Run(new InterruptChecker(isolate, &barrier)); |
194 isolate, &awake_monitor, &awake[task], &round_monitor, &round)); | |
195 } | 138 } |
196 // Wait for all tasks to get ready for the first round. | 139 // Wait for all tasks to get ready for the first round. |
197 WaitForAllTasks(awake, &awake_monitor); | 140 barrier.Sync(); |
198 for (intptr_t i = 0; i < InterruptChecker::kIterations; ++i) { | 141 for (intptr_t i = 0; i < InterruptChecker::kIterations; ++i) { |
199 isolate->ScheduleInterrupts(Isolate::kVMInterrupt); | 142 isolate->ScheduleInterrupts(Isolate::kVMInterrupt); |
200 // Wait for all tasks to observe the interrupt. | 143 // Wait for all tasks to observe the interrupt. |
201 WaitForAllTasks(awake, &awake_monitor); | 144 barrier.Sync(); |
202 // Continue with next round. | 145 // Continue with next round. |
203 uword interrupts = isolate->GetAndClearInterrupts(); | 146 uword interrupts = isolate->GetAndClearInterrupts(); |
204 EXPECT((interrupts & Isolate::kVMInterrupt) != 0); | 147 EXPECT((interrupts & Isolate::kVMInterrupt) != 0); |
205 { | |
206 MonitorLocker ml(&round_monitor); | |
207 ++round; | |
208 ml.NotifyAll(); | |
209 } | |
210 } | 148 } |
211 // Wait for tasks to exit cleanly. | 149 barrier.Exit(); |
212 WaitForAllTasks(awake, &awake_monitor); | |
213 } | 150 } |
214 | 151 |
215 } // namespace dart | 152 } // namespace dart |
OLD | NEW |