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

Unified 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 side-by-side diff with in-line comments
Download patch
Index: mojo/edk/system/watcher_dispatcher.cc
diff --git a/mojo/edk/system/watcher_dispatcher.cc b/mojo/edk/system/watcher_dispatcher.cc
index e4cc875580ad3f82322cf6559dc3f4ac677d1e71..a5bbfe175909ff5d6b54488812807d014ce86440 100644
--- a/mojo/edk/system/watcher_dispatcher.cc
+++ b/mojo/edk/system/watcher_dispatcher.cc
@@ -195,11 +195,36 @@ MojoResult WatcherDispatcher::Arm(
if (num_ready_contexts) {
uint32_t max_ready_contexts = *num_ready_contexts;
*num_ready_contexts = 0;
- for (auto& entry : watched_handles_) {
- if (max_ready_contexts == *num_ready_contexts)
- break;
- const Watch* watch = entry.second.get();
+ WatchSet::const_iterator next_ready_iter = ready_watches_.begin();
+ if (last_watch_to_block_arming_) {
+ // Find the next watch to notify in simple round-robin order on the
+ // |ready_watches_| map, wrapping around to the beginning if necessary.
+ next_ready_iter = ready_watches_.find(last_watch_to_block_arming_);
+ if (next_ready_iter != ready_watches_.end())
+ ++next_ready_iter;
+ if (next_ready_iter == ready_watches_.end())
+ next_ready_iter = ready_watches_.begin();
+ }
+
+ // Tracks the last watch whose state has been stored in the output buffers.
+ WatchSet::const_iterator last_ready_iter = ready_watches_.end();
+
+ // Tracks of the first Watch we're returning here so we know when we're done
+ // iterating when the caller has given us more space than needed.
+ const WatchSet::const_iterator first_ready_iter = next_ready_iter;
+ bool exhausted = false;
+
+ while (max_ready_contexts > *num_ready_contexts && !exhausted) {
+ const Watch* const watch = *next_ready_iter;
+
+ last_ready_iter = next_ready_iter;
+ ++next_ready_iter;
+ if (next_ready_iter == ready_watches_.end())
+ next_ready_iter = ready_watches_.begin();
+ if (next_ready_iter == first_ready_iter)
+ exhausted = true;
+
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
continue;
@@ -208,6 +233,9 @@ MojoResult WatcherDispatcher::Arm(
*(ready_signals_states++) = watch->last_known_signals_state();
++(*num_ready_contexts);
}
+
+ if (last_ready_iter != ready_watches_.end())
+ last_watch_to_block_arming_ = *last_ready_iter;
}
return MOJO_RESULT_FAILED_PRECONDITION;

Powered by Google App Engine
This is Rietveld 408576698