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/runtime/runtime-utils.h"
6
7 #include "src/base/platform/time.h"
8 #include "src/common/globals.h"
9 #include "src/execution/arguments-inl.h"
10 #include "src/execution/futex-emulation.h"
11 #include "src/logging/counters.h"
12 #include "src/numbers/conversions-inl.h"
13 #include "src/objects/heap-object-inl.h"
14 #include "src/objects/js-array-buffer-inl.h"
15
16 // Implement Futex API for SharedArrayBuffers as defined in the
17 // SharedArrayBuffer draft spec, found here:
18 // https://github.com/tc39/ecmascript_sharedmem
19
20 namespace v8 {
21 namespace internal {
22
RUNTIME_FUNCTION(Runtime_AtomicsNumWaitersForTesting)23 RUNTIME_FUNCTION(Runtime_AtomicsNumWaitersForTesting) {
24 HandleScope scope(isolate);
25 DCHECK_EQ(2, args.length());
26 Handle<JSTypedArray> sta = args.at<JSTypedArray>(0);
27 size_t index = NumberToSize(args[1]);
28 CHECK(!sta->WasDetached());
29 CHECK(sta->GetBuffer()->is_shared());
30 CHECK_LT(index, sta->length());
31 CHECK_EQ(sta->type(), kExternalInt32Array);
32
33 Handle<JSArrayBuffer> array_buffer = sta->GetBuffer();
34 size_t addr = (index << 2) + sta->byte_offset();
35
36 return FutexEmulation::NumWaitersForTesting(array_buffer, addr);
37 }
38
RUNTIME_FUNCTION(Runtime_AtomicsNumAsyncWaitersForTesting)39 RUNTIME_FUNCTION(Runtime_AtomicsNumAsyncWaitersForTesting) {
40 DCHECK_EQ(0, args.length());
41 return FutexEmulation::NumAsyncWaitersForTesting(isolate);
42 }
43
RUNTIME_FUNCTION(Runtime_AtomicsNumUnresolvedAsyncPromisesForTesting)44 RUNTIME_FUNCTION(Runtime_AtomicsNumUnresolvedAsyncPromisesForTesting) {
45 HandleScope scope(isolate);
46 DCHECK_EQ(2, args.length());
47 Handle<JSTypedArray> sta = args.at<JSTypedArray>(0);
48 size_t index = NumberToSize(args[1]);
49 CHECK(!sta->WasDetached());
50 CHECK(sta->GetBuffer()->is_shared());
51 CHECK_LT(index, sta->length());
52 CHECK_EQ(sta->type(), kExternalInt32Array);
53
54 Handle<JSArrayBuffer> array_buffer = sta->GetBuffer();
55 size_t addr = (index << 2) + sta->byte_offset();
56
57 return FutexEmulation::NumUnresolvedAsyncPromisesForTesting(array_buffer,
58 addr);
59 }
60
RUNTIME_FUNCTION(Runtime_SetAllowAtomicsWait)61 RUNTIME_FUNCTION(Runtime_SetAllowAtomicsWait) {
62 HandleScope scope(isolate);
63 DCHECK_EQ(1, args.length());
64 bool set = Oddball::cast(args[0]).ToBool(isolate);
65
66 isolate->set_allow_atomics_wait(set);
67 return ReadOnlyRoots(isolate).undefined_value();
68 }
69
70 } // namespace internal
71 } // namespace v8
72