OLD | NEW |
(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 RUNTIME_ASSERT(FLAG_harmony_futex); |
| 27 CONVERT_ARG_HANDLE_CHECKED(JSTypedArray, sta, 0); |
| 28 CONVERT_SIZE_ARG_CHECKED(index, 1); |
| 29 CONVERT_SMI_ARG_CHECKED(value, 2); |
| 30 CONVERT_DOUBLE_ARG_CHECKED(timeout, 3); |
| 31 RUNTIME_ASSERT(sta->GetBuffer()->is_shared()); |
| 32 RUNTIME_ASSERT(index < NumberToSize(isolate, sta->length())); |
| 33 RUNTIME_ASSERT(sta->type() == kExternalInt32Array); |
| 34 |
| 35 Handle<JSArrayBuffer> array_buffer = sta->GetBuffer(); |
| 36 size_t addr = index << 2; |
| 37 |
| 38 // timeout is a fractional value in milliseconds, convert to nanoseconds. |
| 39 double timeout_ns = timeout * base::Time::kNanosecondsPerMicrosecond * |
| 40 base::Time::kMicrosecondsPerMillisecond; |
| 41 |
| 42 FutexEmulation::Result result; |
| 43 if (timeout == V8_INFINITY || |
| 44 timeout_ns > static_cast<double>(std::numeric_limits<int64_t>::max())) { |
| 45 result = FutexEmulation::Wait(isolate, array_buffer, addr, value); |
| 46 } else { |
| 47 int64_t nanoseconds = static_cast<int64_t>(timeout_ns); |
| 48 base::TimeDelta rel_timeout = base::TimeDelta::FromNanoseconds(nanoseconds); |
| 49 result = |
| 50 FutexEmulation::Wait(isolate, array_buffer, addr, value, rel_timeout); |
| 51 } |
| 52 |
| 53 return Smi::FromInt(static_cast<int>(result)); |
| 54 } |
| 55 |
| 56 |
| 57 RUNTIME_FUNCTION(Runtime_AtomicsFutexWake) { |
| 58 HandleScope scope(isolate); |
| 59 DCHECK(args.length() == 3); |
| 60 RUNTIME_ASSERT(FLAG_harmony_futex); |
| 61 CONVERT_ARG_HANDLE_CHECKED(JSTypedArray, sta, 0); |
| 62 CONVERT_SIZE_ARG_CHECKED(index, 1); |
| 63 CONVERT_SMI_ARG_CHECKED(count, 2); |
| 64 RUNTIME_ASSERT(sta->GetBuffer()->is_shared()); |
| 65 RUNTIME_ASSERT(index < NumberToSize(isolate, sta->length())); |
| 66 RUNTIME_ASSERT(sta->type() == kExternalInt32Array); |
| 67 |
| 68 Handle<JSArrayBuffer> array_buffer = sta->GetBuffer(); |
| 69 size_t addr = index << 2; |
| 70 |
| 71 int result = FutexEmulation::Wake(isolate, array_buffer, addr, count); |
| 72 return Smi::FromInt(result); |
| 73 } |
| 74 |
| 75 |
| 76 RUNTIME_FUNCTION(Runtime_AtomicsFutexWakeOrRequeue) { |
| 77 HandleScope scope(isolate); |
| 78 DCHECK(args.length() == 5); |
| 79 RUNTIME_ASSERT(FLAG_harmony_futex); |
| 80 CONVERT_ARG_HANDLE_CHECKED(JSTypedArray, sta, 0); |
| 81 CONVERT_SIZE_ARG_CHECKED(index1, 1); |
| 82 CONVERT_SMI_ARG_CHECKED(count, 2); |
| 83 CONVERT_SMI_ARG_CHECKED(value, 3); |
| 84 CONVERT_SIZE_ARG_CHECKED(index2, 4); |
| 85 RUNTIME_ASSERT(sta->GetBuffer()->is_shared()); |
| 86 RUNTIME_ASSERT(index1 < NumberToSize(isolate, sta->length())); |
| 87 RUNTIME_ASSERT(index2 < NumberToSize(isolate, sta->length())); |
| 88 RUNTIME_ASSERT(sta->type() == kExternalInt32Array); |
| 89 |
| 90 Handle<JSArrayBuffer> array_buffer = sta->GetBuffer(); |
| 91 size_t addr1 = index1 << 2; |
| 92 size_t addr2 = index2 << 2; |
| 93 |
| 94 int result = FutexEmulation::WakeOrRequeue(isolate, array_buffer, addr1, |
| 95 count, value, addr2); |
| 96 return Smi::FromInt(result); |
| 97 } |
| 98 |
| 99 |
| 100 RUNTIME_FUNCTION(Runtime_AtomicsFutexNumWaitersForTesting) { |
| 101 HandleScope scope(isolate); |
| 102 DCHECK(args.length() == 2); |
| 103 RUNTIME_ASSERT(FLAG_harmony_futex); |
| 104 CONVERT_ARG_HANDLE_CHECKED(JSTypedArray, sta, 0); |
| 105 CONVERT_SIZE_ARG_CHECKED(index, 1); |
| 106 RUNTIME_ASSERT(sta->GetBuffer()->is_shared()); |
| 107 RUNTIME_ASSERT(index < NumberToSize(isolate, sta->length())); |
| 108 RUNTIME_ASSERT(sta->type() == kExternalInt32Array); |
| 109 |
| 110 Handle<JSArrayBuffer> array_buffer = sta->GetBuffer(); |
| 111 size_t addr = index << 2; |
| 112 |
| 113 int result = |
| 114 FutexEmulation::NumWaitersForTesting(isolate, array_buffer, addr); |
| 115 return Smi::FromInt(result); |
| 116 } |
| 117 } |
| 118 } // namespace v8::internal |
OLD | NEW |