Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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/waiter.h" | 5 #include "mojo/edk/system/waiter.h" |
| 6 | 6 |
| 7 #include "base/logging.h" | 7 #include "base/logging.h" |
| 8 #include "mojo/edk/platform/time_ticks.h" | 8 #include "mojo/edk/platform/time_ticks.h" |
| 9 | 9 |
| 10 using mojo::platform::GetTimeTicks; | 10 using mojo::platform::GetTimeTicks; |
| 11 using mojo::util::MutexLocker; | 11 using mojo::util::MutexLocker; |
| 12 | 12 |
| 13 namespace mojo { | 13 namespace mojo { |
| 14 namespace system { | 14 namespace system { |
| 15 | 15 |
| 16 Waiter::Waiter() | 16 Waiter::Waiter() |
| 17 : | 17 : |
| 18 #ifndef NDEBUG | 18 #ifndef NDEBUG |
| 19 initialized_(false), | 19 initialized_(false), |
| 20 #endif | 20 #endif |
| 21 awoken_(false), | 21 awoken_(false), |
| 22 awake_result_(MOJO_RESULT_INTERNAL), | 22 awake_reason_(AwakeReason::CANCELLED), |
| 23 awake_context_(static_cast<uint64_t>(-1)) { | 23 awake_context_(static_cast<uint64_t>(-1)) { |
| 24 } | 24 } |
| 25 | 25 |
| 26 Waiter::~Waiter() {} | 26 Waiter::~Waiter() {} |
| 27 | 27 |
| 28 void Waiter::Init() { | 28 void Waiter::Init() { |
| 29 #ifndef NDEBUG | 29 #ifndef NDEBUG |
| 30 initialized_ = true; | 30 initialized_ = true; |
| 31 #endif | 31 #endif |
| 32 awoken_ = false; | 32 awoken_ = false; |
| 33 // NOTE(vtl): If performance ever becomes an issue, we can disable the setting | |
| 34 // of |awake_result_| (except the first one in |Awake()|) in Release builds. | |
|
vardhan
2016/06/21 07:49:01
weird
| |
| 35 awake_result_ = MOJO_RESULT_INTERNAL; | |
| 36 } | 33 } |
| 37 | 34 |
| 38 // TODO(vtl): Fast-path the |deadline == 0| case? | 35 // TODO(vtl): Fast-path the |deadline == 0| case? |
| 39 MojoResult Waiter::Wait(MojoDeadline deadline, uint64_t* context) { | 36 MojoResult Waiter::Wait(MojoDeadline deadline, |
| 37 uint64_t* context, | |
| 38 HandleSignalsState* signals_state) { | |
| 40 MutexLocker locker(&mutex_); | 39 MutexLocker locker(&mutex_); |
| 41 | 40 |
| 42 #ifndef NDEBUG | 41 #ifndef NDEBUG |
| 43 DCHECK(initialized_); | 42 DCHECK(initialized_); |
| 44 // It'll need to be re-initialized after this. | 43 // It'll need to be re-initialized after this. |
| 45 initialized_ = false; | 44 initialized_ = false; |
| 46 #endif | 45 #endif |
| 47 | 46 |
| 48 // Fast-path the already-awoken case: | 47 // Fast-path the already-awoken case: |
| 49 if (awoken_) { | 48 if (awoken_) { |
| 50 DCHECK_NE(awake_result_, MOJO_RESULT_INTERNAL); | |
| 51 if (context) | 49 if (context) |
| 52 *context = awake_context_; | 50 *context = awake_context_; |
| 53 return awake_result_; | 51 if (signals_state) |
| 52 *signals_state = signals_state_; | |
| 53 return MojoResultForAwakeReason(awake_reason_); | |
| 54 } | 54 } |
| 55 | 55 |
| 56 if (deadline == MOJO_DEADLINE_INDEFINITE) { | 56 if (deadline == MOJO_DEADLINE_INDEFINITE) { |
| 57 do { | 57 do { |
| 58 cv_.Wait(&mutex_); | 58 cv_.Wait(&mutex_); |
| 59 } while (!awoken_); | 59 } while (!awoken_); |
| 60 } else { | 60 } else { |
| 61 // We may get spurious wakeups, so record the start time and track the | 61 // We may get spurious wakeups, so record the start time and track the |
| 62 // remaining timeout. | 62 // remaining timeout. |
| 63 uint64_t wait_remaining = deadline; | 63 uint64_t wait_remaining = deadline; |
| (...skipping 14 matching lines...) Expand all Loading... | |
| 78 uint64_t elapsed = static_cast<uint64_t>(now - start); | 78 uint64_t elapsed = static_cast<uint64_t>(now - start); |
| 79 // It's possible that the deadline has passed anyway. | 79 // It's possible that the deadline has passed anyway. |
| 80 if (elapsed >= deadline) | 80 if (elapsed >= deadline) |
| 81 return MOJO_RESULT_DEADLINE_EXCEEDED; | 81 return MOJO_RESULT_DEADLINE_EXCEEDED; |
| 82 | 82 |
| 83 // Otherwise, recalculate the amount that we have left to wait. | 83 // Otherwise, recalculate the amount that we have left to wait. |
| 84 wait_remaining = deadline - elapsed; | 84 wait_remaining = deadline - elapsed; |
| 85 } | 85 } |
| 86 } | 86 } |
| 87 | 87 |
| 88 DCHECK_NE(awake_result_, MOJO_RESULT_INTERNAL); | |
| 89 if (context) | 88 if (context) |
| 90 *context = awake_context_; | 89 *context = awake_context_; |
| 91 return awake_result_; | 90 if (signals_state) |
| 91 *signals_state = signals_state_; | |
| 92 return MojoResultForAwakeReason(awake_reason_); | |
| 92 } | 93 } |
| 93 | 94 |
| 94 bool Waiter::Awake(uint64_t context, | 95 bool Waiter::Awake(uint64_t context, |
| 95 AwakeReason reason, | 96 AwakeReason reason, |
| 96 const HandleSignalsState& signals_state) { | 97 const HandleSignalsState& signals_state) { |
| 97 MutexLocker locker(&mutex_); | 98 MutexLocker locker(&mutex_); |
| 98 | 99 |
| 99 if (awoken_) | 100 if (awoken_) |
| 100 return true; | 101 return true; |
| 101 | 102 |
| 102 awoken_ = true; | 103 awoken_ = true; |
| 103 awake_result_ = MojoResultForAwakeReason(reason); | 104 awake_reason_ = reason; |
| 105 signals_state_ = signals_state; | |
| 104 awake_context_ = context; | 106 awake_context_ = context; |
| 105 cv_.Signal(); | 107 cv_.Signal(); |
| 106 // |cv_.Wait()|/|cv_.WaitWithTimeout()| will return after |mutex_| is | 108 // |cv_.Wait()|/|cv_.WaitWithTimeout()| will return after |mutex_| is |
| 107 // released. | 109 // released. |
| 108 return true; | 110 return true; |
| 109 } | 111 } |
| 110 | 112 |
| 111 } // namespace system | 113 } // namespace system |
| 112 } // namespace mojo | 114 } // namespace mojo |
| OLD | NEW |