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

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

Issue 1208933006: Atomics Futex API (Closed) Base URL: https://chromium.googlesource.com/v8/v8.git@master
Patch Set: feedback 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 <limits>
6
7 #include "src/futex-emulation.h"
8
9 #include "src/v8.h"
10
11 #include "src/arguments.h"
12 #include "src/base/platform/time.h"
13 #include "src/globals.h"
14 #include "src/runtime/runtime-utils.h"
15
16 // Implement Futex API for SharedArrayBuffers as defined in the
17 // SharedArrayBuffer draft spec, found here
18 // https://docs.google.com/document/d/1NDGA_gZJ7M7w1Bh8S0AoDyEqwDdRh4uSoTPSNn77P Fk
19
20 namespace v8 {
21 namespace internal {
22
23 RUNTIME_FUNCTION(Runtime_AtomicsFutexWait) {
24 HandleScope scope(isolate);
25 DCHECK(args.length() == 4);
26 CONVERT_ARG_HANDLE_CHECKED(JSTypedArray, sta, 0);
27 CONVERT_SIZE_ARG_CHECKED(index, 1);
28 CONVERT_SMI_ARG_CHECKED(value, 2);
29 CONVERT_DOUBLE_ARG_CHECKED(timeout, 3);
30 RUNTIME_ASSERT(sta->GetBuffer()->is_shared());
31 RUNTIME_ASSERT(index < NumberToSize(isolate, sta->length()));
32 RUNTIME_ASSERT(sta->type() == kExternalInt32Array);
33
34 Handle<JSArrayBuffer> array_buffer = sta->GetBuffer();
35 size_t addr = index << 2;
36
37 // timeout is a fractional value in milliseconds, convert to nanoseconds.
38 double timeout_ns = timeout * base::Time::kNanosecondsPerMicrosecond *
39 base::Time::kMicrosecondsPerMillisecond;
40
41 FutexEmulation::Result result;
42 if (timeout == V8_INFINITY ||
43 timeout_ns > static_cast<double>(std::numeric_limits<int64_t>::max())) {
44 result = FutexEmulation::Wait(isolate, array_buffer, addr, value);
45 } else {
46 int64_t nanoseconds = static_cast<int64_t>(timeout_ns);
47 base::TimeDelta rel_timeout = base::TimeDelta::FromNanoseconds(nanoseconds);
48 result =
49 FutexEmulation::Wait(isolate, array_buffer, addr, value, rel_timeout);
50 }
51
52 return Smi::FromInt(static_cast<int>(result));
53 }
54
55
56 RUNTIME_FUNCTION(Runtime_AtomicsFutexWake) {
57 HandleScope scope(isolate);
58 DCHECK(args.length() == 3);
59 CONVERT_ARG_HANDLE_CHECKED(JSTypedArray, sta, 0);
60 CONVERT_SIZE_ARG_CHECKED(index, 1);
61 CONVERT_SMI_ARG_CHECKED(count, 2);
62 RUNTIME_ASSERT(sta->GetBuffer()->is_shared());
63 RUNTIME_ASSERT(index < NumberToSize(isolate, sta->length()));
64 RUNTIME_ASSERT(sta->type() == kExternalInt32Array);
65
66 Handle<JSArrayBuffer> array_buffer = sta->GetBuffer();
67 size_t addr = index << 2;
68
69 int result = FutexEmulation::Wake(isolate, array_buffer, addr, count);
70 return Smi::FromInt(result);
71 }
72
73
74 RUNTIME_FUNCTION(Runtime_AtomicsFutexWakeOrRequeue) {
75 HandleScope scope(isolate);
76 DCHECK(args.length() == 5);
77 CONVERT_ARG_HANDLE_CHECKED(JSTypedArray, sta, 0);
78 CONVERT_SIZE_ARG_CHECKED(index1, 1);
79 CONVERT_SMI_ARG_CHECKED(count, 2);
80 CONVERT_SMI_ARG_CHECKED(value, 3);
81 CONVERT_SIZE_ARG_CHECKED(index2, 4);
82 RUNTIME_ASSERT(sta->GetBuffer()->is_shared());
83 RUNTIME_ASSERT(index1 < NumberToSize(isolate, sta->length()));
84 RUNTIME_ASSERT(index2 < NumberToSize(isolate, sta->length()));
85 RUNTIME_ASSERT(sta->type() == kExternalInt32Array);
86
87 Handle<JSArrayBuffer> array_buffer = sta->GetBuffer();
88 size_t addr1 = index1 << 2;
89 size_t addr2 = index2 << 2;
90
91 int result = FutexEmulation::WakeOrRequeue(isolate, array_buffer, addr1,
92 count, value, addr2);
93 return Smi::FromInt(result);
94 }
95
96
97 RUNTIME_FUNCTION(Runtime_AtomicsFutexNumWaitersForTesting) {
98 HandleScope scope(isolate);
99 DCHECK(args.length() == 2);
100 CONVERT_ARG_HANDLE_CHECKED(JSTypedArray, sta, 0);
101 CONVERT_SIZE_ARG_CHECKED(index, 1);
102 RUNTIME_ASSERT(sta->GetBuffer()->is_shared());
103 RUNTIME_ASSERT(index < NumberToSize(isolate, sta->length()));
104 RUNTIME_ASSERT(sta->type() == kExternalInt32Array);
105
106 Handle<JSArrayBuffer> array_buffer = sta->GetBuffer();
107 size_t addr = index << 2;
108
109 int result =
110 FutexEmulation::NumWaitersForTesting(isolate, array_buffer, addr);
111 return Smi::FromInt(result);
112 }
113 }
114 } // namespace v8::internal
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698