| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "mojo/edk/system/waiter.h" | |
| 6 | |
| 7 #include <stdint.h> | |
| 8 | |
| 9 #include <limits> | |
| 10 | |
| 11 #include "base/logging.h" | |
| 12 #include "base/time/time.h" | |
| 13 | |
| 14 namespace mojo { | |
| 15 namespace edk { | |
| 16 | |
| 17 Waiter::Waiter() | |
| 18 : cv_(&lock_), | |
| 19 #if DCHECK_IS_ON() | |
| 20 initialized_(false), | |
| 21 #endif | |
| 22 awoken_(false), | |
| 23 awake_result_(MOJO_RESULT_INTERNAL), | |
| 24 awake_context_(static_cast<uint32_t>(-1)) { | |
| 25 } | |
| 26 | |
| 27 Waiter::~Waiter() { | |
| 28 } | |
| 29 | |
| 30 void Waiter::Init() { | |
| 31 #if DCHECK_IS_ON() | |
| 32 initialized_ = true; | |
| 33 #endif | |
| 34 awoken_ = false; | |
| 35 // NOTE(vtl): If performance ever becomes an issue, we can disable the setting | |
| 36 // of |awake_result_| (except the first one in |Awake()|) in Release builds. | |
| 37 awake_result_ = MOJO_RESULT_INTERNAL; | |
| 38 } | |
| 39 | |
| 40 // TODO(vtl): Fast-path the |deadline == 0| case? | |
| 41 MojoResult Waiter::Wait(MojoDeadline deadline, uintptr_t* context) { | |
| 42 base::AutoLock locker(lock_); | |
| 43 | |
| 44 #if DCHECK_IS_ON() | |
| 45 DCHECK(initialized_); | |
| 46 // It'll need to be re-initialized after this. | |
| 47 initialized_ = false; | |
| 48 #endif | |
| 49 | |
| 50 // Fast-path the already-awoken case: | |
| 51 if (awoken_) { | |
| 52 DCHECK_NE(awake_result_, MOJO_RESULT_INTERNAL); | |
| 53 if (context) | |
| 54 *context = awake_context_; | |
| 55 return awake_result_; | |
| 56 } | |
| 57 | |
| 58 // |MojoDeadline| is actually a |uint64_t|, but we need a signed quantity. | |
| 59 // Treat any out-of-range deadline as "forever" (which is wrong, but okay | |
| 60 // since 2^63 microseconds is ~300000 years). Note that this also takes care | |
| 61 // of the |MOJO_DEADLINE_INDEFINITE| (= 2^64 - 1) case. | |
| 62 if (deadline > static_cast<uint64_t>(std::numeric_limits<int64_t>::max())) { | |
| 63 do { | |
| 64 cv_.Wait(); | |
| 65 } while (!awoken_); | |
| 66 } else { | |
| 67 // NOTE(vtl): This is very inefficient on POSIX, since pthreads condition | |
| 68 // variables take an absolute deadline. | |
| 69 const base::TimeTicks end_time = | |
| 70 base::TimeTicks::Now() + | |
| 71 base::TimeDelta::FromMicroseconds(static_cast<int64_t>(deadline)); | |
| 72 do { | |
| 73 base::TimeTicks now_time = base::TimeTicks::Now(); | |
| 74 if (now_time >= end_time) | |
| 75 return MOJO_RESULT_DEADLINE_EXCEEDED; | |
| 76 | |
| 77 cv_.TimedWait(end_time - now_time); | |
| 78 } while (!awoken_); | |
| 79 } | |
| 80 | |
| 81 DCHECK_NE(awake_result_, MOJO_RESULT_INTERNAL); | |
| 82 if (context) | |
| 83 *context = awake_context_; | |
| 84 return awake_result_; | |
| 85 } | |
| 86 | |
| 87 bool Waiter::Awake(MojoResult result, uintptr_t context) { | |
| 88 base::AutoLock locker(lock_); | |
| 89 | |
| 90 if (awoken_) | |
| 91 return true; | |
| 92 | |
| 93 awoken_ = true; | |
| 94 awake_result_ = result; | |
| 95 awake_context_ = context; | |
| 96 cv_.Signal(); | |
| 97 // |cv_.Wait()|/|cv_.TimedWait()| will return after |lock_| is released. | |
| 98 return true; | |
| 99 } | |
| 100 | |
| 101 } // namespace edk | |
| 102 } // namespace mojo | |
| OLD | NEW |