OLD | NEW |
1 // Copyright 2015 the V8 project authors. All rights reserved. | 1 // Copyright 2015 the V8 project 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 "src/futex-emulation.h" | 5 #include "src/futex-emulation.h" |
6 | 6 |
7 #include <limits> | 7 #include <limits> |
8 | 8 |
9 #include "src/base/macros.h" | 9 #include "src/base/macros.h" |
10 #include "src/base/platform/time.h" | 10 #include "src/base/platform/time.h" |
(...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
94 static_cast<double>(std::numeric_limits<int64_t>::max())) { | 94 static_cast<double>(std::numeric_limits<int64_t>::max())) { |
95 // 2**63 nanoseconds is 292 years. Let's just treat anything greater as | 95 // 2**63 nanoseconds is 292 years. Let's just treat anything greater as |
96 // infinite. | 96 // infinite. |
97 use_timeout = false; | 97 use_timeout = false; |
98 } else { | 98 } else { |
99 rel_timeout = base::TimeDelta::FromNanoseconds( | 99 rel_timeout = base::TimeDelta::FromNanoseconds( |
100 static_cast<int64_t>(rel_timeout_ns)); | 100 static_cast<int64_t>(rel_timeout_ns)); |
101 } | 101 } |
102 } | 102 } |
103 | 103 |
104 base::TimeDelta rel_time_left = rel_timeout; | 104 base::Time start_time = base::Time::NowFromSystemTime(); |
| 105 base::Time timeout_time = start_time + rel_timeout; |
105 | 106 |
106 wait_list_.Pointer()->AddNode(node); | 107 wait_list_.Pointer()->AddNode(node); |
107 | 108 |
108 Object* result; | 109 Object* result; |
109 | 110 |
110 while (true) { | 111 while (true) { |
111 base::TimeDelta time_to_wait = (use_timeout && rel_time_left < kMaxWaitTime) | 112 base::Time current_time = base::Time::NowFromSystemTime(); |
112 ? rel_time_left | 113 if (use_timeout && current_time > timeout_time) { |
113 : kMaxWaitTime; | 114 result = Smi::FromInt(Result::kTimedOut); |
| 115 break; |
| 116 } |
114 | 117 |
115 base::Time start_time = base::Time::NowFromSystemTime(); | 118 base::TimeDelta time_until_timeout = timeout_time - current_time; |
| 119 base::TimeDelta time_to_wait = |
| 120 (use_timeout && time_until_timeout < kMaxWaitTime) ? time_until_timeout |
| 121 : kMaxWaitTime; |
| 122 |
116 bool wait_for_result = node->cond_.WaitFor(mutex_.Pointer(), time_to_wait); | 123 bool wait_for_result = node->cond_.WaitFor(mutex_.Pointer(), time_to_wait); |
117 USE(wait_for_result); | 124 USE(wait_for_result); |
118 | 125 |
119 if (!node->waiting_) { | 126 if (!node->waiting_) { |
120 result = Smi::FromInt(Result::kOk); | 127 result = Smi::FromInt(Result::kOk); |
121 break; | 128 break; |
122 } | 129 } |
123 | 130 |
124 // Spurious wakeup or timeout. | 131 // Spurious wakeup or timeout. Potentially handle interrupts before |
125 base::Time end_time = base::Time::NowFromSystemTime(); | 132 // continuing to wait. |
126 base::TimeDelta waited_for = end_time - start_time; | |
127 rel_time_left -= waited_for; | |
128 | |
129 if (use_timeout && rel_time_left < base::TimeDelta::FromMicroseconds(0)) { | |
130 result = Smi::FromInt(Result::kTimedOut); | |
131 break; | |
132 } | |
133 | |
134 // Potentially handle interrupts before continuing to wait. | |
135 Object* interrupt_object = isolate->stack_guard()->HandleInterrupts(); | 133 Object* interrupt_object = isolate->stack_guard()->HandleInterrupts(); |
136 if (interrupt_object->IsException()) { | 134 if (interrupt_object->IsException()) { |
137 result = interrupt_object; | 135 result = interrupt_object; |
138 break; | 136 break; |
139 } | 137 } |
140 } | 138 } |
141 | 139 |
142 wait_list_.Pointer()->RemoveNode(node); | 140 wait_list_.Pointer()->RemoveNode(node); |
143 | 141 |
144 return result; | 142 return result; |
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
224 } | 222 } |
225 | 223 |
226 node = node->next_; | 224 node = node->next_; |
227 } | 225 } |
228 | 226 |
229 return Smi::FromInt(waiters); | 227 return Smi::FromInt(waiters); |
230 } | 228 } |
231 | 229 |
232 } // namespace internal | 230 } // namespace internal |
233 } // namespace v8 | 231 } // namespace v8 |
OLD | NEW |