Chromium Code Reviews| OLD | NEW |
|---|---|
| 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 <algorithm> | |
| 7 #include <limits> | 8 #include <limits> |
| 8 #include <map> | 9 #include <map> |
| 9 | 10 |
| 10 #include "base/macros.h" | 11 #include "base/macros.h" |
| 11 #include "base/memory/ptr_util.h" | 12 #include "base/memory/ptr_util.h" |
| 12 #include "mojo/edk/system/watch.h" | 13 #include "mojo/edk/system/watch.h" |
| 13 | 14 |
| 14 namespace mojo { | 15 namespace mojo { |
| 15 namespace edk { | 16 namespace edk { |
| 16 | 17 |
| (...skipping 169 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 186 if (watched_handles_.empty()) | 187 if (watched_handles_.empty()) |
| 187 return MOJO_RESULT_NOT_FOUND; | 188 return MOJO_RESULT_NOT_FOUND; |
| 188 | 189 |
| 189 if (ready_watches_.empty()) { | 190 if (ready_watches_.empty()) { |
| 190 // Fast path: No watches are ready to notify, so we're done. | 191 // Fast path: No watches are ready to notify, so we're done. |
| 191 armed_ = true; | 192 armed_ = true; |
| 192 return MOJO_RESULT_OK; | 193 return MOJO_RESULT_OK; |
| 193 } | 194 } |
| 194 | 195 |
| 195 if (num_ready_contexts) { | 196 if (num_ready_contexts) { |
| 196 uint32_t max_ready_contexts = *num_ready_contexts; | 197 DCHECK_LE(ready_watches_.size(), std::numeric_limits<uint32_t>::max()); |
| 197 *num_ready_contexts = 0; | 198 *num_ready_contexts = std::min( |
| 198 for (auto& entry : watched_handles_) { | 199 *num_ready_contexts, static_cast<uint32_t>(ready_watches_.size())); |
| 199 if (max_ready_contexts == *num_ready_contexts) | |
| 200 break; | |
| 201 | 200 |
| 202 const Watch* watch = entry.second.get(); | 201 WatchSet::const_iterator next_ready_iter = ready_watches_.begin(); |
| 203 if (!watch->ready()) | 202 if (last_watch_to_block_arming_) { |
| 204 continue; | 203 // Find the next watch to notify in simple round-robin order on the |
| 204 // |ready_watches_| map, wrapping around to the beginning if necessary. | |
| 205 next_ready_iter = ready_watches_.find(last_watch_to_block_arming_); | |
| 206 if (next_ready_iter != ready_watches_.end()) | |
| 207 ++next_ready_iter; | |
| 208 if (next_ready_iter == ready_watches_.end()) | |
| 209 next_ready_iter = ready_watches_.begin(); | |
| 210 } | |
| 205 | 211 |
| 206 *(ready_contexts++) = watch->context(); | 212 for (size_t i = 0; i < *num_ready_contexts; ++i) { |
| 207 *(ready_results++) = watch->last_known_result(); | 213 const Watch* const watch = *next_ready_iter; |
| 208 *(ready_signals_states++) = watch->last_known_signals_state(); | 214 ready_contexts[i] = watch->context(); |
| 209 ++(*num_ready_contexts); | 215 ready_results[i] = watch->last_known_result(); |
| 216 ready_signals_states[i] = watch->last_known_signals_state(); | |
| 217 | |
| 218 // Iterate and wrap around. | |
| 219 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
| |
| 220 ++next_ready_iter; | |
| 221 if (next_ready_iter == ready_watches_.end()) | |
| 222 next_ready_iter = ready_watches_.begin(); | |
| 210 } | 223 } |
| 211 } | 224 } |
| 212 | 225 |
| 213 return MOJO_RESULT_FAILED_PRECONDITION; | 226 return MOJO_RESULT_FAILED_PRECONDITION; |
| 214 } | 227 } |
| 215 | 228 |
| 216 WatcherDispatcher::~WatcherDispatcher() {} | 229 WatcherDispatcher::~WatcherDispatcher() {} |
| 217 | 230 |
| 218 } // namespace edk | 231 } // namespace edk |
| 219 } // namespace mojo | 232 } // namespace mojo |
| OLD | NEW |