Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(9)

Side by Side Diff: src/futex-emulation.cc

Issue 1208933006: Atomics Futex API (Closed) Base URL: https://chromium.googlesource.com/v8/v8.git@master
Patch Set: reduce test wait time from 1s -> 0.1s Created 5 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
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
3 // found in the LICENSE file.
4
5 #include "src/futex-emulation.h"
6
7 #include <limits>
8
9 #include "src/base/macros.h"
10 #include "src/base/platform/time.h"
11 #include "src/conversions.h"
12 #include "src/handles-inl.h"
13 #include "src/isolate.h"
14 #include "src/list-inl.h"
15
16 namespace v8 {
17 namespace internal {
18
19 base::LazyMutex FutexEmulation::mutex_ = LAZY_MUTEX_INITIALIZER;
20 base::LazyInstance<FutexWaitList>::type FutexEmulation::wait_list_ =
21 LAZY_INSTANCE_INITIALIZER;
22
23
24 FutexWaitList::FutexWaitList() : head_(nullptr), tail_(nullptr) {}
25
26
27 void FutexWaitList::AddNode(FutexWaitListNode* node) {
28 DCHECK(node->prev_ == nullptr && node->next_ == nullptr);
29 if (tail_) {
30 tail_->next_ = node;
31 } else {
32 head_ = node;
33 }
34
35 node->prev_ = tail_;
36 node->next_ = nullptr;
37 tail_ = node;
38 }
39
40
41 void FutexWaitList::RemoveNode(FutexWaitListNode* node) {
42 if (node->prev_) {
43 node->prev_->next_ = node->next_;
44 } else {
45 head_ = node->next_;
46 }
47
48 if (node->next_) {
49 node->next_->prev_ = node->prev_;
50 } else {
51 tail_ = node->prev_;
52 }
53
54 node->prev_ = node->next_ = nullptr;
55 }
56
57
58 Object* FutexEmulation::Wait(Isolate* isolate,
59 Handle<JSArrayBuffer> array_buffer, size_t addr,
60 int32_t value, double rel_timeout_ms) {
61 // We never want to wait longer than this amount of time; this way we can
62 // interrupt this thread even if this is an "infinitely blocking" wait.
63 // TODO(binji): How long should this be? Should it be configurable?
Jarin 2015/07/17 12:55:27 No idea. Ideally, we should have some notification
binji 2015/07/17 16:29:21 Done. I was thinking about this: because there is
64 const base::TimeDelta kMaxWaitTime = base::TimeDelta::FromMilliseconds(50);
65
66 DCHECK(addr < NumberToSize(isolate, array_buffer->byte_length()));
67
68 void* backing_store = array_buffer->backing_store();
69 int32_t* p =
70 reinterpret_cast<int32_t*>(static_cast<int8_t*>(backing_store) + addr);
71
72 base::LockGuard<base::Mutex> lock_guard(mutex_.Pointer());
73
74 if (*p != value) {
75 return Smi::FromInt(Result::kNotEqual);
76 }
77
78 FutexWaitListNode* node = isolate->futex_wait_list_node();
79
80 node->backing_store_ = backing_store;
81 node->wait_addr_ = addr;
82 node->waiting_ = true;
83
84 bool use_timeout = rel_timeout_ms != V8_INFINITY;
85
86 base::TimeDelta rel_timeout;
87 if (use_timeout) {
88 // Convert to nanoseconds.
89 double rel_timeout_ns = rel_timeout_ms *
90 base::Time::kNanosecondsPerMicrosecond *
91 base::Time::kMicrosecondsPerMillisecond;
92 if (rel_timeout_ns >
93 static_cast<double>(std::numeric_limits<int64_t>::max())) {
94 // 2**63 nanoseconds is 292 years. Let's just treat anything greater as
95 // infinite.
96 use_timeout = false;
97 } else {
98 rel_timeout = base::TimeDelta::FromNanoseconds(
99 static_cast<int64_t>(rel_timeout_ns));
100 }
101 }
102
103 base::TimeDelta rel_time_left = rel_timeout;
104
105 wait_list_.Pointer()->AddNode(node);
106
107 Object* result;
108
109 while (true) {
110 base::TimeDelta time_to_wait = (use_timeout && rel_time_left < kMaxWaitTime)
111 ? rel_time_left
112 : kMaxWaitTime;
113
114 base::Time start_time = base::Time::NowFromSystemTime();
115 bool wait_for_result = node->cond_.WaitFor(mutex_.Pointer(), time_to_wait);
116 USE(wait_for_result);
117
118 if (!node->waiting_) {
119 result = Smi::FromInt(Result::kOk);
120 break;
121 }
122
123 // Spurious wakeup or timeout.
124 base::Time end_time = base::Time::NowFromSystemTime();
125 base::TimeDelta waited_for = end_time - start_time;
126 rel_time_left -= waited_for;
127
128 if (use_timeout && rel_time_left < base::TimeDelta::FromMicroseconds(0)) {
129 result = Smi::FromInt(Result::kTimedOut);
130 break;
131 }
132
133 // Potentially handle interrupts before continuing to wait.
134 Object* interrupt_object = isolate->stack_guard()->HandleInterrupts();
135 if (interrupt_object->IsException()) {
136 result = interrupt_object;
137 break;
138 }
139 }
140
141 wait_list_.Pointer()->RemoveNode(node);
142
143 return result;
144 }
145
146
147 Object* FutexEmulation::Wake(Isolate* isolate,
148 Handle<JSArrayBuffer> array_buffer, size_t addr,
149 int num_waiters_to_wake) {
150 DCHECK(addr < NumberToSize(isolate, array_buffer->byte_length()));
151
152 int waiters_woken = 0;
153 void* backing_store = array_buffer->backing_store();
154
155 base::LockGuard<base::Mutex> lock_guard(mutex_.Pointer());
156 FutexWaitListNode* node = wait_list_.Pointer()->head_;
157 while (node && num_waiters_to_wake > 0) {
158 if (backing_store == node->backing_store_ && addr == node->wait_addr_) {
159 node->waiting_ = false;
160 node->cond_.NotifyOne();
161 --num_waiters_to_wake;
162 waiters_woken++;
163 }
164
165 node = node->next_;
166 }
167
168 return Smi::FromInt(waiters_woken);
169 }
170
171
172 Object* FutexEmulation::WakeOrRequeue(Isolate* isolate,
173 Handle<JSArrayBuffer> array_buffer,
174 size_t addr, int num_waiters_to_wake,
175 int32_t value, size_t addr2) {
176 DCHECK(addr < NumberToSize(isolate, array_buffer->byte_length()));
177 DCHECK(addr2 < NumberToSize(isolate, array_buffer->byte_length()));
178
179 void* backing_store = array_buffer->backing_store();
180 int32_t* p =
181 reinterpret_cast<int32_t*>(static_cast<int8_t*>(backing_store) + addr);
182
183 base::LockGuard<base::Mutex> lock_guard(mutex_.Pointer());
184 if (*p != value) {
185 return Smi::FromInt(Result::kNotEqual);
186 }
187
188 // Wake |num_waiters_to_wake|
189 int waiters_woken = 0;
190 FutexWaitListNode* node = wait_list_.Pointer()->head_;
191 while (node) {
192 if (backing_store == node->backing_store_ && addr == node->wait_addr_) {
193 if (num_waiters_to_wake > 0) {
194 node->waiting_ = false;
195 node->cond_.NotifyOne();
196 --num_waiters_to_wake;
197 waiters_woken++;
198 } else {
199 node->wait_addr_ = addr2;
200 }
201 }
202
203 node = node->next_;
204 }
205
206 return Smi::FromInt(waiters_woken);
207 }
208
209
210 Object* FutexEmulation::NumWaitersForTesting(Isolate* isolate,
211 Handle<JSArrayBuffer> array_buffer,
212 size_t addr) {
213 DCHECK(addr < NumberToSize(isolate, array_buffer->byte_length()));
214 void* backing_store = array_buffer->backing_store();
215
216 base::LockGuard<base::Mutex> lock_guard(mutex_.Pointer());
217
218 int waiters = 0;
219 FutexWaitListNode* node = wait_list_.Pointer()->head_;
220 while (node) {
221 if (backing_store == node->backing_store_ && addr == node->wait_addr_) {
222 waiters++;
223 }
224
225 node = node->next_;
226 }
227
228 return Smi::FromInt(waiters);
229 }
230
231 } // namespace internal
232 } // namespace v8
OLDNEW
« no previous file with comments | « src/futex-emulation.h ('k') | src/harmony-atomics.js » ('j') | test/cctest/test-api.cc » ('J')

Powered by Google App Engine
This is Rietveld 408576698