Chromium Code Reviews| 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..409dd2a9229cff2cfa5041826545bd7f08544c69 100644 |
| --- a/mojo/edk/system/watcher_dispatcher.cc |
| +++ b/mojo/edk/system/watcher_dispatcher.cc |
| @@ -4,6 +4,7 @@ |
| #include "mojo/edk/system/watcher_dispatcher.h" |
| +#include <algorithm> |
| #include <limits> |
| #include <map> |
| @@ -193,20 +194,32 @@ 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(); |
| - if (!watch->ready()) |
| - continue; |
| - |
| - *(ready_contexts++) = watch->context(); |
| - *(ready_results++) = watch->last_known_result(); |
| - *(ready_signals_states++) = watch->last_known_signals_state(); |
| - ++(*num_ready_contexts); |
| + DCHECK_LE(ready_watches_.size(), std::numeric_limits<uint32_t>::max()); |
| + *num_ready_contexts = std::min( |
| + *num_ready_contexts, static_cast<uint32_t>(ready_watches_.size())); |
| + |
| + 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(); |
| + } |
| + |
| + for (size_t i = 0; i < *num_ready_contexts; ++i) { |
| + const Watch* const watch = *next_ready_iter; |
| + ready_contexts[i] = watch->context(); |
| + ready_results[i] = watch->last_known_result(); |
| + ready_signals_states[i] = watch->last_known_signals_state(); |
| + |
| + // Iterate and wrap around. |
| + last_watch_to_block_arming_ = watch; |
|
yzshen1
2017/03/16 21:06:17
nit: it might further simplify the code by:
======
Ken Rockot(use gerrit already)
2017/03/16 21:17:35
It seems like a simplification at first, but in pr
|
| + ++next_ready_iter; |
| + if (next_ready_iter == ready_watches_.end()) |
| + next_ready_iter = ready_watches_.begin(); |
| } |
| } |