| 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_result_(MOJO_RESULT_INTERNAL), |
| 23 awake_context_(static_cast<uint32_t>(-1)) { | 23 awake_context_(static_cast<uint64_t>(-1)) { |
| 24 } | 24 } |
| 25 | 25 |
| 26 Waiter::~Waiter() { | 26 Waiter::~Waiter() {} |
| 27 } | |
| 28 | 27 |
| 29 void Waiter::Init() { | 28 void Waiter::Init() { |
| 30 #ifndef NDEBUG | 29 #ifndef NDEBUG |
| 31 initialized_ = true; | 30 initialized_ = true; |
| 32 #endif | 31 #endif |
| 33 awoken_ = false; | 32 awoken_ = false; |
| 34 // NOTE(vtl): If performance ever becomes an issue, we can disable the setting | 33 // NOTE(vtl): If performance ever becomes an issue, we can disable the setting |
| 35 // of |awake_result_| (except the first one in |Awake()|) in Release builds. | 34 // of |awake_result_| (except the first one in |Awake()|) in Release builds. |
| 36 awake_result_ = MOJO_RESULT_INTERNAL; | 35 awake_result_ = MOJO_RESULT_INTERNAL; |
| 37 } | 36 } |
| 38 | 37 |
| 39 // TODO(vtl): Fast-path the |deadline == 0| case? | 38 // TODO(vtl): Fast-path the |deadline == 0| case? |
| 40 MojoResult Waiter::Wait(MojoDeadline deadline, uint32_t* context) { | 39 MojoResult Waiter::Wait(MojoDeadline deadline, uint64_t* context) { |
| 41 MutexLocker locker(&mutex_); | 40 MutexLocker locker(&mutex_); |
| 42 | 41 |
| 43 #ifndef NDEBUG | 42 #ifndef NDEBUG |
| 44 DCHECK(initialized_); | 43 DCHECK(initialized_); |
| 45 // It'll need to be re-initialized after this. | 44 // It'll need to be re-initialized after this. |
| 46 initialized_ = false; | 45 initialized_ = false; |
| 47 #endif | 46 #endif |
| 48 | 47 |
| 49 // Fast-path the already-awoken case: | 48 // Fast-path the already-awoken case: |
| 50 if (awoken_) { | 49 if (awoken_) { |
| 51 DCHECK_NE(awake_result_, MOJO_RESULT_INTERNAL); | 50 DCHECK_NE(awake_result_, MOJO_RESULT_INTERNAL); |
| 52 if (context) | 51 if (context) |
| 53 *context = static_cast<uint32_t>(awake_context_); | 52 *context = awake_context_; |
| 54 return awake_result_; | 53 return awake_result_; |
| 55 } | 54 } |
| 56 | 55 |
| 57 if (deadline == MOJO_DEADLINE_INDEFINITE) { | 56 if (deadline == MOJO_DEADLINE_INDEFINITE) { |
| 58 do { | 57 do { |
| 59 cv_.Wait(&mutex_); | 58 cv_.Wait(&mutex_); |
| 60 } while (!awoken_); | 59 } while (!awoken_); |
| 61 } else { | 60 } else { |
| 62 // 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 |
| 63 // remaining timeout. | 62 // remaining timeout. |
| (...skipping 17 matching lines...) Expand all Loading... |
| 81 if (elapsed >= deadline) | 80 if (elapsed >= deadline) |
| 82 return MOJO_RESULT_DEADLINE_EXCEEDED; | 81 return MOJO_RESULT_DEADLINE_EXCEEDED; |
| 83 | 82 |
| 84 // Otherwise, recalculate the amount that we have left to wait. | 83 // Otherwise, recalculate the amount that we have left to wait. |
| 85 wait_remaining = deadline - elapsed; | 84 wait_remaining = deadline - elapsed; |
| 86 } | 85 } |
| 87 } | 86 } |
| 88 | 87 |
| 89 DCHECK_NE(awake_result_, MOJO_RESULT_INTERNAL); | 88 DCHECK_NE(awake_result_, MOJO_RESULT_INTERNAL); |
| 90 if (context) | 89 if (context) |
| 91 *context = static_cast<uint32_t>(awake_context_); | 90 *context = awake_context_; |
| 92 return awake_result_; | 91 return awake_result_; |
| 93 } | 92 } |
| 94 | 93 |
| 95 bool Waiter::Awake(MojoResult result, uintptr_t context) { | 94 bool Waiter::Awake(MojoResult result, uint64_t context) { |
| 96 MutexLocker locker(&mutex_); | 95 MutexLocker locker(&mutex_); |
| 97 | 96 |
| 98 if (awoken_) | 97 if (awoken_) |
| 99 return true; | 98 return true; |
| 100 | 99 |
| 101 awoken_ = true; | 100 awoken_ = true; |
| 102 awake_result_ = result; | 101 awake_result_ = result; |
| 103 awake_context_ = context; | 102 awake_context_ = context; |
| 104 cv_.Signal(); | 103 cv_.Signal(); |
| 105 // |cv_.Wait()|/|cv_.WaitWithTimeout()| will return after |mutex_| is | 104 // |cv_.Wait()|/|cv_.WaitWithTimeout()| will return after |mutex_| is |
| 106 // released. | 105 // released. |
| 107 return true; | 106 return true; |
| 108 } | 107 } |
| 109 | 108 |
| 110 } // namespace system | 109 } // namespace system |
| 111 } // namespace mojo | 110 } // namespace mojo |
| OLD | NEW |