Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(934)

Side by Side Diff: mojo/edk/system/watcher_dispatcher.cc

Issue 2754083003: Mojo EDK: Circulate MojoArmWatcher outputs to avoid starvation (Closed)
Patch Set: . Created 3 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2017 The Chromium Authors. All rights reserved. 1 // Copyright 2017 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "mojo/edk/system/watcher_dispatcher.h" 5 #include "mojo/edk/system/watcher_dispatcher.h"
6 6
7 #include <limits> 7 #include <limits>
8 #include <map> 8 #include <map>
9 9
10 #include "base/macros.h" 10 #include "base/macros.h"
(...skipping 177 matching lines...) Expand 10 before | Expand all | Expand 10 after
188 188
189 if (ready_watches_.empty()) { 189 if (ready_watches_.empty()) {
190 // Fast path: No watches are ready to notify, so we're done. 190 // Fast path: No watches are ready to notify, so we're done.
191 armed_ = true; 191 armed_ = true;
192 return MOJO_RESULT_OK; 192 return MOJO_RESULT_OK;
193 } 193 }
194 194
195 if (num_ready_contexts) { 195 if (num_ready_contexts) {
196 uint32_t max_ready_contexts = *num_ready_contexts; 196 uint32_t max_ready_contexts = *num_ready_contexts;
197 *num_ready_contexts = 0; 197 *num_ready_contexts = 0;
198 for (auto& entry : watched_handles_) {
199 if (max_ready_contexts == *num_ready_contexts)
200 break;
201 198
202 const Watch* watch = entry.second.get(); 199 WatchSet::const_iterator next_ready_iter = ready_watches_.begin();
200 if (last_watch_to_block_arming_) {
201 // Find the next watch to notify in simple round-robin order on the
202 // |ready_watches_| map, wrapping around to the beginning if necessary.
203 next_ready_iter = ready_watches_.find(last_watch_to_block_arming_);
204 if (next_ready_iter != ready_watches_.end())
205 ++next_ready_iter;
206 if (next_ready_iter == ready_watches_.end())
207 next_ready_iter = ready_watches_.begin();
208 }
209
210 // Tracks the last watch whose state has been stored in the output buffers.
211 WatchSet::const_iterator last_ready_iter = ready_watches_.end();
212
213 // Tracks of the first Watch we're returning here so we know when we're done
214 // iterating when the caller has given us more space than needed.
215 const WatchSet::const_iterator first_ready_iter = next_ready_iter;
216 bool exhausted = false;
217
218 while (max_ready_contexts > *num_ready_contexts && !exhausted) {
219 const Watch* const watch = *next_ready_iter;
220
221 last_ready_iter = next_ready_iter;
222 ++next_ready_iter;
223 if (next_ready_iter == ready_watches_.end())
224 next_ready_iter = ready_watches_.begin();
225 if (next_ready_iter == first_ready_iter)
226 exhausted = true;
227
203 if (!watch->ready()) 228 if (!watch->ready())
yzshen1 2017/03/16 18:59:01 [A question to help my own understanding] Why do w
Ken Rockot(use gerrit already) 2017/03/16 19:21:11 Very good point! This test was only here because t
204 continue; 229 continue;
205 230
206 *(ready_contexts++) = watch->context(); 231 *(ready_contexts++) = watch->context();
207 *(ready_results++) = watch->last_known_result(); 232 *(ready_results++) = watch->last_known_result();
208 *(ready_signals_states++) = watch->last_known_signals_state(); 233 *(ready_signals_states++) = watch->last_known_signals_state();
209 ++(*num_ready_contexts); 234 ++(*num_ready_contexts);
210 } 235 }
236
237 if (last_ready_iter != ready_watches_.end())
238 last_watch_to_block_arming_ = *last_ready_iter;
211 } 239 }
212 240
213 return MOJO_RESULT_FAILED_PRECONDITION; 241 return MOJO_RESULT_FAILED_PRECONDITION;
214 } 242 }
215 243
216 WatcherDispatcher::~WatcherDispatcher() {} 244 WatcherDispatcher::~WatcherDispatcher() {}
217 245
218 } // namespace edk 246 } // namespace edk
219 } // namespace mojo 247 } // namespace mojo
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698