| Index: src/runtime/runtime-futex.cc
|
| diff --git a/src/runtime/runtime-futex.cc b/src/runtime/runtime-futex.cc
|
| new file mode 100644
|
| index 0000000000000000000000000000000000000000..69abd0a806369d101d4bb2d81269012ecf2a460a
|
| --- /dev/null
|
| +++ b/src/runtime/runtime-futex.cc
|
| @@ -0,0 +1,114 @@
|
| +// Copyright 2015 the V8 project authors. All rights reserved.
|
| +// Use of this source code is governed by a BSD-style license that can be
|
| +// found in the LICENSE file.
|
| +
|
| +#include <limits>
|
| +
|
| +#include "src/futex-emulation.h"
|
| +
|
| +#include "src/v8.h"
|
| +
|
| +#include "src/arguments.h"
|
| +#include "src/base/platform/time.h"
|
| +#include "src/globals.h"
|
| +#include "src/runtime/runtime-utils.h"
|
| +
|
| +// Implement Futex API for SharedArrayBuffers as defined in the
|
| +// SharedArrayBuffer draft spec, found here
|
| +// https://docs.google.com/document/d/1NDGA_gZJ7M7w1Bh8S0AoDyEqwDdRh4uSoTPSNn77PFk
|
| +
|
| +namespace v8 {
|
| +namespace internal {
|
| +
|
| +RUNTIME_FUNCTION(Runtime_AtomicsFutexWait) {
|
| + HandleScope scope(isolate);
|
| + DCHECK(args.length() == 4);
|
| + CONVERT_ARG_HANDLE_CHECKED(JSTypedArray, sta, 0);
|
| + CONVERT_SIZE_ARG_CHECKED(index, 1);
|
| + CONVERT_SMI_ARG_CHECKED(value, 2);
|
| + CONVERT_DOUBLE_ARG_CHECKED(timeout, 3);
|
| + RUNTIME_ASSERT(sta->GetBuffer()->is_shared());
|
| + RUNTIME_ASSERT(index < NumberToSize(isolate, sta->length()));
|
| + RUNTIME_ASSERT(sta->type() == kExternalInt32Array);
|
| +
|
| + Handle<JSArrayBuffer> array_buffer = sta->GetBuffer();
|
| + size_t addr = index << 2;
|
| +
|
| + // timeout is a fractional value in milliseconds, convert to nanoseconds.
|
| + double timeout_ns = timeout * base::Time::kNanosecondsPerMicrosecond *
|
| + base::Time::kMicrosecondsPerMillisecond;
|
| +
|
| + FutexEmulation::Result result;
|
| + if (timeout == V8_INFINITY ||
|
| + timeout_ns > static_cast<double>(std::numeric_limits<int64_t>::max())) {
|
| + result = FutexEmulation::Wait(isolate, array_buffer, addr, value);
|
| + } else {
|
| + int64_t nanoseconds = static_cast<int64_t>(timeout_ns);
|
| + base::TimeDelta rel_timeout = base::TimeDelta::FromNanoseconds(nanoseconds);
|
| + result =
|
| + FutexEmulation::Wait(isolate, array_buffer, addr, value, rel_timeout);
|
| + }
|
| +
|
| + return Smi::FromInt(static_cast<int>(result));
|
| +}
|
| +
|
| +
|
| +RUNTIME_FUNCTION(Runtime_AtomicsFutexWake) {
|
| + HandleScope scope(isolate);
|
| + DCHECK(args.length() == 3);
|
| + CONVERT_ARG_HANDLE_CHECKED(JSTypedArray, sta, 0);
|
| + CONVERT_SIZE_ARG_CHECKED(index, 1);
|
| + CONVERT_SMI_ARG_CHECKED(count, 2);
|
| + RUNTIME_ASSERT(sta->GetBuffer()->is_shared());
|
| + RUNTIME_ASSERT(index < NumberToSize(isolate, sta->length()));
|
| + RUNTIME_ASSERT(sta->type() == kExternalInt32Array);
|
| +
|
| + Handle<JSArrayBuffer> array_buffer = sta->GetBuffer();
|
| + size_t addr = index << 2;
|
| +
|
| + int result = FutexEmulation::Wake(isolate, array_buffer, addr, count);
|
| + return Smi::FromInt(result);
|
| +}
|
| +
|
| +
|
| +RUNTIME_FUNCTION(Runtime_AtomicsFutexWakeOrRequeue) {
|
| + HandleScope scope(isolate);
|
| + DCHECK(args.length() == 5);
|
| + CONVERT_ARG_HANDLE_CHECKED(JSTypedArray, sta, 0);
|
| + CONVERT_SIZE_ARG_CHECKED(index1, 1);
|
| + CONVERT_SMI_ARG_CHECKED(count, 2);
|
| + CONVERT_SMI_ARG_CHECKED(value, 3);
|
| + CONVERT_SIZE_ARG_CHECKED(index2, 4);
|
| + RUNTIME_ASSERT(sta->GetBuffer()->is_shared());
|
| + RUNTIME_ASSERT(index1 < NumberToSize(isolate, sta->length()));
|
| + RUNTIME_ASSERT(index2 < NumberToSize(isolate, sta->length()));
|
| + RUNTIME_ASSERT(sta->type() == kExternalInt32Array);
|
| +
|
| + Handle<JSArrayBuffer> array_buffer = sta->GetBuffer();
|
| + size_t addr1 = index1 << 2;
|
| + size_t addr2 = index2 << 2;
|
| +
|
| + int result = FutexEmulation::WakeOrRequeue(isolate, array_buffer, addr1,
|
| + count, value, addr2);
|
| + return Smi::FromInt(result);
|
| +}
|
| +
|
| +
|
| +RUNTIME_FUNCTION(Runtime_AtomicsFutexNumWaitersForTesting) {
|
| + HandleScope scope(isolate);
|
| + DCHECK(args.length() == 2);
|
| + CONVERT_ARG_HANDLE_CHECKED(JSTypedArray, sta, 0);
|
| + CONVERT_SIZE_ARG_CHECKED(index, 1);
|
| + RUNTIME_ASSERT(sta->GetBuffer()->is_shared());
|
| + RUNTIME_ASSERT(index < NumberToSize(isolate, sta->length()));
|
| + RUNTIME_ASSERT(sta->type() == kExternalInt32Array);
|
| +
|
| + Handle<JSArrayBuffer> array_buffer = sta->GetBuffer();
|
| + size_t addr = index << 2;
|
| +
|
| + int result =
|
| + FutexEmulation::NumWaitersForTesting(isolate, array_buffer, addr);
|
| + return Smi::FromInt(result);
|
| +}
|
| +}
|
| +} // namespace v8::internal
|
|
|