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 <limits> | 7 #include <limits> |
8 | 8 |
9 #include "base/logging.h" | 9 #include "base/logging.h" |
10 #include "base/time/time.h" | 10 #include "base/time/time.h" |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
42 #ifndef NDEBUG | 42 #ifndef NDEBUG |
43 DCHECK(initialized_); | 43 DCHECK(initialized_); |
44 // It'll need to be re-initialized after this. | 44 // It'll need to be re-initialized after this. |
45 initialized_ = false; | 45 initialized_ = false; |
46 #endif | 46 #endif |
47 | 47 |
48 // Fast-path the already-awoken case: | 48 // Fast-path the already-awoken case: |
49 if (awoken_) { | 49 if (awoken_) { |
50 DCHECK_NE(awake_result_, MOJO_RESULT_INTERNAL); | 50 DCHECK_NE(awake_result_, MOJO_RESULT_INTERNAL); |
51 if (context) | 51 if (context) |
52 *context = awake_context_; | 52 *context = static_cast<uint32_t>(awake_context_); |
53 return awake_result_; | 53 return awake_result_; |
54 } | 54 } |
55 | 55 |
56 // |MojoDeadline| is actually a |uint64_t|, but we need a signed quantity. | 56 // |MojoDeadline| is actually a |uint64_t|, but we need a signed quantity. |
57 // Treat any out-of-range deadline as "forever" (which is wrong, but okay | 57 // Treat any out-of-range deadline as "forever" (which is wrong, but okay |
58 // since 2^63 microseconds is ~300000 years). Note that this also takes care | 58 // since 2^63 microseconds is ~300000 years). Note that this also takes care |
59 // of the |MOJO_DEADLINE_INDEFINITE| (= 2^64 - 1) case. | 59 // of the |MOJO_DEADLINE_INDEFINITE| (= 2^64 - 1) case. |
60 if (deadline > static_cast<uint64_t>(std::numeric_limits<int64_t>::max())) { | 60 if (deadline > static_cast<uint64_t>(std::numeric_limits<int64_t>::max())) { |
61 do { | 61 do { |
62 cv_.Wait(); | 62 cv_.Wait(); |
63 } while (!awoken_); | 63 } while (!awoken_); |
64 } else { | 64 } else { |
65 // NOTE(vtl): This is very inefficient on POSIX, since pthreads condition | 65 // NOTE(vtl): This is very inefficient on POSIX, since pthreads condition |
66 // variables take an absolute deadline. | 66 // variables take an absolute deadline. |
67 const base::TimeTicks end_time = | 67 const base::TimeTicks end_time = |
68 base::TimeTicks::Now() + | 68 base::TimeTicks::Now() + |
69 base::TimeDelta::FromMicroseconds(static_cast<int64_t>(deadline)); | 69 base::TimeDelta::FromMicroseconds(static_cast<int64_t>(deadline)); |
70 do { | 70 do { |
71 base::TimeTicks now_time = base::TimeTicks::Now(); | 71 base::TimeTicks now_time = base::TimeTicks::Now(); |
72 if (now_time >= end_time) | 72 if (now_time >= end_time) |
73 return MOJO_RESULT_DEADLINE_EXCEEDED; | 73 return MOJO_RESULT_DEADLINE_EXCEEDED; |
74 | 74 |
75 cv_.TimedWait(end_time - now_time); | 75 cv_.TimedWait(end_time - now_time); |
76 } while (!awoken_); | 76 } while (!awoken_); |
77 } | 77 } |
78 | 78 |
79 DCHECK_NE(awake_result_, MOJO_RESULT_INTERNAL); | 79 DCHECK_NE(awake_result_, MOJO_RESULT_INTERNAL); |
80 if (context) | 80 if (context) |
81 *context = awake_context_; | 81 *context = static_cast<uint32_t>(awake_context_); |
82 return awake_result_; | 82 return awake_result_; |
83 } | 83 } |
84 | 84 |
85 void Waiter::Awake(MojoResult result, uint32_t context) { | 85 void Waiter::Awake(MojoResult result, uintptr_t context) { |
86 base::AutoLock locker(lock_); | 86 base::AutoLock locker(lock_); |
87 | 87 |
88 if (awoken_) | 88 if (awoken_) |
89 return; | 89 return; |
90 | 90 |
91 awoken_ = true; | 91 awoken_ = true; |
92 awake_result_ = result; | 92 awake_result_ = result; |
93 awake_context_ = context; | 93 awake_context_ = context; |
94 cv_.Signal(); | 94 cv_.Signal(); |
95 // |cv_.Wait()|/|cv_.TimedWait()| will return after |lock_| is released. | 95 // |cv_.Wait()|/|cv_.TimedWait()| will return after |lock_| is released. |
96 } | 96 } |
97 | 97 |
98 } // namespace system | 98 } // namespace system |
99 } // namespace mojo | 99 } // namespace mojo |
OLD | NEW |