runtime-futex.cc 2.36 KB
Newer Older
binji's avatar
binji committed
1 2 3 4
// 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.

5
#include "src/runtime/runtime-utils.h"
binji's avatar
binji committed
6 7 8

#include "src/arguments.h"
#include "src/base/platform/time.h"
9 10
#include "src/conversions-inl.h"
#include "src/futex-emulation.h"
binji's avatar
binji committed
11 12 13 14
#include "src/globals.h"

// Implement Futex API for SharedArrayBuffers as defined in the
// SharedArrayBuffer draft spec, found here:
15
// https://github.com/tc39/ecmascript_sharedmem
binji's avatar
binji committed
16 17 18 19

namespace v8 {
namespace internal {

20
RUNTIME_FUNCTION(Runtime_AtomicsWait) {
binji's avatar
binji committed
21 22 23 24 25 26
  HandleScope scope(isolate);
  DCHECK(args.length() == 4);
  CONVERT_ARG_HANDLE_CHECKED(JSTypedArray, sta, 0);
  CONVERT_SIZE_ARG_CHECKED(index, 1);
  CONVERT_INT32_ARG_CHECKED(value, 2);
  CONVERT_DOUBLE_ARG_CHECKED(timeout, 3);
27 28 29 30
  CHECK(sta->GetBuffer()->is_shared());
  CHECK_LT(index, NumberToSize(isolate, sta->length()));
  CHECK_EQ(sta->type(), kExternalInt32Array);
  CHECK(timeout == V8_INFINITY || !std::isnan(timeout));
binji's avatar
binji committed
31 32

  Handle<JSArrayBuffer> array_buffer = sta->GetBuffer();
33
  size_t addr = (index << 2) + NumberToSize(isolate, sta->byte_offset());
binji's avatar
binji committed
34 35 36 37

  return FutexEmulation::Wait(isolate, array_buffer, addr, value, timeout);
}

38
RUNTIME_FUNCTION(Runtime_AtomicsWake) {
binji's avatar
binji committed
39 40 41 42 43
  HandleScope scope(isolate);
  DCHECK(args.length() == 3);
  CONVERT_ARG_HANDLE_CHECKED(JSTypedArray, sta, 0);
  CONVERT_SIZE_ARG_CHECKED(index, 1);
  CONVERT_INT32_ARG_CHECKED(count, 2);
44 45 46
  CHECK(sta->GetBuffer()->is_shared());
  CHECK_LT(index, NumberToSize(isolate, sta->length()));
  CHECK_EQ(sta->type(), kExternalInt32Array);
binji's avatar
binji committed
47 48

  Handle<JSArrayBuffer> array_buffer = sta->GetBuffer();
49
  size_t addr = (index << 2) + NumberToSize(isolate, sta->byte_offset());
binji's avatar
binji committed
50 51 52 53

  return FutexEmulation::Wake(isolate, array_buffer, addr, count);
}

54
RUNTIME_FUNCTION(Runtime_AtomicsNumWaitersForTesting) {
binji's avatar
binji committed
55 56 57 58
  HandleScope scope(isolate);
  DCHECK(args.length() == 2);
  CONVERT_ARG_HANDLE_CHECKED(JSTypedArray, sta, 0);
  CONVERT_SIZE_ARG_CHECKED(index, 1);
59 60 61
  CHECK(sta->GetBuffer()->is_shared());
  CHECK_LT(index, NumberToSize(isolate, sta->length()));
  CHECK_EQ(sta->type(), kExternalInt32Array);
binji's avatar
binji committed
62 63

  Handle<JSArrayBuffer> array_buffer = sta->GetBuffer();
64
  size_t addr = (index << 2) + NumberToSize(isolate, sta->byte_offset());
binji's avatar
binji committed
65 66 67

  return FutexEmulation::NumWaitersForTesting(isolate, array_buffer, addr);
}
68 69
}  // namespace internal
}  // namespace v8