runtime-atomics.cc 25.9 KB
Newer Older
binji's avatar
binji committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
// 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 "src/v8.h"

#include "src/arguments.h"
#include "src/base/macros.h"
#include "src/base/platform/mutex.h"
#include "src/conversions.h"
#include "src/runtime/runtime-utils.h"

// Implement Atomic accesses to SharedArrayBuffers as defined in the
// SharedArrayBuffer draft spec, found here
// https://docs.google.com/document/d/1NDGA_gZJ7M7w1Bh8S0AoDyEqwDdRh4uSoTPSNn77PFk

namespace v8 {
namespace internal {

namespace {

#if V8_CC_GNU

template <typename T>
inline T CompareExchangeSeqCst(T* p, T oldval, T newval) {
  (void)__atomic_compare_exchange_n(p, &oldval, newval, 0, __ATOMIC_SEQ_CST,
                                    __ATOMIC_SEQ_CST);
  return oldval;
}

template <typename T>
inline T LoadSeqCst(T* p) {
  T result;
  __atomic_load(p, &result, __ATOMIC_SEQ_CST);
  return result;
}

template <typename T>
inline void StoreSeqCst(T* p, T value) {
  __atomic_store_n(p, value, __ATOMIC_SEQ_CST);
}

template <typename T>
inline T AddSeqCst(T* p, T value) {
  return __atomic_fetch_add(p, value, __ATOMIC_SEQ_CST);
}

template <typename T>
inline T SubSeqCst(T* p, T value) {
  return __atomic_fetch_sub(p, value, __ATOMIC_SEQ_CST);
}

template <typename T>
inline T AndSeqCst(T* p, T value) {
  return __atomic_fetch_and(p, value, __ATOMIC_SEQ_CST);
}

template <typename T>
inline T OrSeqCst(T* p, T value) {
  return __atomic_fetch_or(p, value, __ATOMIC_SEQ_CST);
}

template <typename T>
inline T XorSeqCst(T* p, T value) {
  return __atomic_fetch_xor(p, value, __ATOMIC_SEQ_CST);
}

template <typename T>
inline T ExchangeSeqCst(T* p, T value) {
  return __atomic_exchange_n(p, value, __ATOMIC_SEQ_CST);
}

#if ATOMICS_REQUIRE_LOCK_64_BIT

// We only need to implement the following functions, because the rest of the
// atomic operations only work on integer types, and the only 64-bit type is
// float64. Similarly, because the values are being bit_cast from double ->
// uint64_t, we don't need to implement these functions for int64_t either.

static base::LazyMutex atomic_mutex = LAZY_MUTEX_INITIALIZER;

inline uint64_t CompareExchangeSeqCst(uint64_t* p, uint64_t oldval,
                                      uint64_t newval) {
  base::LockGuard<base::Mutex> lock_guard(atomic_mutex.Pointer());
  uint64_t result = *p;
  if (result == oldval) *p = newval;
  return result;
}


inline uint64_t LoadSeqCst(uint64_t* p) {
  base::LockGuard<base::Mutex> lock_guard(atomic_mutex.Pointer());
  return *p;
}


inline void StoreSeqCst(uint64_t* p, uint64_t value) {
  base::LockGuard<base::Mutex> lock_guard(atomic_mutex.Pointer());
  *p = value;
}

#endif  // ATOMICS_REQUIRE_LOCK_64_BIT

#elif V8_CC_MSVC

106 107 108 109 110 111 112 113 114
#define InterlockedCompareExchange32 _InterlockedCompareExchange
#define InterlockedExchange32 _InterlockedExchange
#define InterlockedExchangeAdd32 _InterlockedExchangeAdd
#define InterlockedAnd32 _InterlockedAnd
#define InterlockedOr32 _InterlockedOr
#define InterlockedXor32 _InterlockedXor
#define InterlockedExchangeAdd16 _InterlockedExchangeAdd16
#define InterlockedCompareExchange8 _InterlockedCompareExchange8
#define InterlockedExchangeAdd8 _InterlockedExchangeAdd8
binji's avatar
binji committed
115

116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
#define ATOMIC_OPS_INTEGER(type, suffix, vctype)                        \
  inline type AddSeqCst(type* p, type value) {                          \
    return InterlockedExchangeAdd##suffix(reinterpret_cast<vctype*>(p), \
                                          bit_cast<vctype>(value));     \
  }                                                                     \
  inline type SubSeqCst(type* p, type value) {                          \
    return InterlockedExchangeAdd##suffix(reinterpret_cast<vctype*>(p), \
                                          -bit_cast<vctype>(value));    \
  }                                                                     \
  inline type AndSeqCst(type* p, type value) {                          \
    return InterlockedAnd##suffix(reinterpret_cast<vctype*>(p),         \
                                  bit_cast<vctype>(value));             \
  }                                                                     \
  inline type OrSeqCst(type* p, type value) {                           \
    return InterlockedOr##suffix(reinterpret_cast<vctype*>(p),          \
                                 bit_cast<vctype>(value));              \
  }                                                                     \
  inline type XorSeqCst(type* p, type value) {                          \
    return InterlockedXor##suffix(reinterpret_cast<vctype*>(p),         \
                                  bit_cast<vctype>(value));             \
  }                                                                     \
  inline type ExchangeSeqCst(type* p, type value) {                     \
    return InterlockedExchange##suffix(reinterpret_cast<vctype*>(p),    \
                                       bit_cast<vctype>(value));        \
  }

#define ATOMIC_OPS_FLOAT(type, suffix, vctype)                              \
143 144 145 146 147 148 149 150 151
  inline type CompareExchangeSeqCst(type* p, type oldval, type newval) {    \
    return InterlockedCompareExchange##suffix(reinterpret_cast<vctype*>(p), \
                                              bit_cast<vctype>(newval),     \
                                              bit_cast<vctype>(oldval));    \
  }                                                                         \
  inline type LoadSeqCst(type* p) { return *p; }                            \
  inline void StoreSeqCst(type* p, type value) {                            \
    InterlockedExchange##suffix(reinterpret_cast<vctype*>(p),               \
                                bit_cast<vctype>(value));                   \
binji's avatar
binji committed
152
  }
153 154 155 156 157 158 159 160 161 162 163 164 165 166 167

#define ATOMIC_OPS(type, suffix, vctype)   \
  ATOMIC_OPS_INTEGER(type, suffix, vctype) \
  ATOMIC_OPS_FLOAT(type, suffix, vctype)

ATOMIC_OPS(int8_t, 8, char)
ATOMIC_OPS(uint8_t, 8, char)
ATOMIC_OPS(int16_t, 16, short)  /* NOLINT(runtime/int) */
ATOMIC_OPS(uint16_t, 16, short) /* NOLINT(runtime/int) */
ATOMIC_OPS(int32_t, 32, long)   /* NOLINT(runtime/int) */
ATOMIC_OPS(uint32_t, 32, long)  /* NOLINT(runtime/int) */
ATOMIC_OPS_FLOAT(uint64_t, 64, LONGLONG)

#undef ATOMIC_OPS_INTEGER
#undef ATOMIC_OPS_FLOAT
binji's avatar
binji committed
168 169
#undef ATOMIC_OPS

170 171 172 173 174 175 176 177 178
#undef InterlockedCompareExchange32
#undef InterlockedExchange32
#undef InterlockedExchangeAdd32
#undef InterlockedAnd32
#undef InterlockedOr32
#undef InterlockedXor32
#undef InterlockedExchangeAdd16
#undef InterlockedCompareExchange8
#undef InterlockedExchangeAdd8
binji's avatar
binji committed
179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436

#else

#error Unsupported platform!

#endif

template <typename T>
T FromObject(Handle<Object> number);

template <>
inline uint32_t FromObject<uint32_t>(Handle<Object> number) {
  return NumberToUint32(*number);
}

template <>
inline int32_t FromObject<int32_t>(Handle<Object> number) {
  return NumberToInt32(*number);
}

template <>
inline float FromObject<float>(Handle<Object> number) {
  return static_cast<float>(number->Number());
}

template <>
inline double FromObject<double>(Handle<Object> number) {
  return number->Number();
}

template <typename T, typename F>
inline T ToAtomic(F from) {
  return static_cast<T>(from);
}

template <>
inline uint32_t ToAtomic<uint32_t, float>(float from) {
  return bit_cast<uint32_t, float>(from);
}

template <>
inline uint64_t ToAtomic<uint64_t, double>(double from) {
  return bit_cast<uint64_t, double>(from);
}

template <typename T, typename F>
inline T FromAtomic(F from) {
  return static_cast<T>(from);
}

template <>
inline float FromAtomic<float, uint32_t>(uint32_t from) {
  return bit_cast<float, uint32_t>(from);
}

template <>
inline double FromAtomic<double, uint64_t>(uint64_t from) {
  return bit_cast<double, uint64_t>(from);
}

template <typename T>
inline Object* ToObject(Isolate* isolate, T t);

template <>
inline Object* ToObject<int8_t>(Isolate* isolate, int8_t t) {
  return Smi::FromInt(t);
}

template <>
inline Object* ToObject<uint8_t>(Isolate* isolate, uint8_t t) {
  return Smi::FromInt(t);
}

template <>
inline Object* ToObject<int16_t>(Isolate* isolate, int16_t t) {
  return Smi::FromInt(t);
}

template <>
inline Object* ToObject<uint16_t>(Isolate* isolate, uint16_t t) {
  return Smi::FromInt(t);
}

template <>
inline Object* ToObject<int32_t>(Isolate* isolate, int32_t t) {
  return *isolate->factory()->NewNumber(t);
}

template <>
inline Object* ToObject<uint32_t>(Isolate* isolate, uint32_t t) {
  return *isolate->factory()->NewNumber(t);
}

template <>
inline Object* ToObject<float>(Isolate* isolate, float t) {
  return *isolate->factory()->NewNumber(t);
}

template <>
inline Object* ToObject<double>(Isolate* isolate, double t) {
  return *isolate->factory()->NewNumber(t);
}

template <typename T>
struct FromObjectTraits {};

template <>
struct FromObjectTraits<int8_t> {
  typedef int32_t convert_type;
  typedef int8_t atomic_type;
};

template <>
struct FromObjectTraits<uint8_t> {
  typedef uint32_t convert_type;
  typedef uint8_t atomic_type;
};

template <>
struct FromObjectTraits<int16_t> {
  typedef int32_t convert_type;
  typedef int16_t atomic_type;
};

template <>
struct FromObjectTraits<uint16_t> {
  typedef uint32_t convert_type;
  typedef uint16_t atomic_type;
};

template <>
struct FromObjectTraits<int32_t> {
  typedef int32_t convert_type;
  typedef int32_t atomic_type;
};

template <>
struct FromObjectTraits<uint32_t> {
  typedef uint32_t convert_type;
  typedef uint32_t atomic_type;
};

template <>
struct FromObjectTraits<float> {
  typedef float convert_type;
  typedef uint32_t atomic_type;
};

template <>
struct FromObjectTraits<double> {
  typedef double convert_type;
  typedef uint64_t atomic_type;
};


template <typename T>
inline Object* DoCompareExchange(Isolate* isolate, void* buffer, size_t index,
                                 Handle<Object> oldobj, Handle<Object> newobj) {
  typedef typename FromObjectTraits<T>::atomic_type atomic_type;
  typedef typename FromObjectTraits<T>::convert_type convert_type;
  atomic_type oldval = ToAtomic<atomic_type>(FromObject<convert_type>(oldobj));
  atomic_type newval = ToAtomic<atomic_type>(FromObject<convert_type>(newobj));
  atomic_type result = CompareExchangeSeqCst(
      static_cast<atomic_type*>(buffer) + index, oldval, newval);
  return ToObject<T>(isolate, FromAtomic<T>(result));
}


template <typename T>
inline Object* DoLoad(Isolate* isolate, void* buffer, size_t index) {
  typedef typename FromObjectTraits<T>::atomic_type atomic_type;
  atomic_type result = LoadSeqCst(static_cast<atomic_type*>(buffer) + index);
  return ToObject<T>(isolate, FromAtomic<T>(result));
}


template <typename T>
inline Object* DoStore(Isolate* isolate, void* buffer, size_t index,
                       Handle<Object> obj) {
  typedef typename FromObjectTraits<T>::atomic_type atomic_type;
  typedef typename FromObjectTraits<T>::convert_type convert_type;
  atomic_type value = ToAtomic<atomic_type>(FromObject<convert_type>(obj));
  StoreSeqCst(static_cast<atomic_type*>(buffer) + index, value);
  return *obj;
}


template <typename T>
inline Object* DoAdd(Isolate* isolate, void* buffer, size_t index,
                     Handle<Object> obj) {
  typedef typename FromObjectTraits<T>::atomic_type atomic_type;
  typedef typename FromObjectTraits<T>::convert_type convert_type;
  atomic_type value = ToAtomic<atomic_type>(FromObject<convert_type>(obj));
  atomic_type result =
      AddSeqCst(static_cast<atomic_type*>(buffer) + index, value);
  return ToObject<T>(isolate, FromAtomic<T>(result));
}


template <typename T>
inline Object* DoSub(Isolate* isolate, void* buffer, size_t index,
                     Handle<Object> obj) {
  typedef typename FromObjectTraits<T>::atomic_type atomic_type;
  typedef typename FromObjectTraits<T>::convert_type convert_type;
  atomic_type value = ToAtomic<atomic_type>(FromObject<convert_type>(obj));
  atomic_type result =
      SubSeqCst(static_cast<atomic_type*>(buffer) + index, value);
  return ToObject<T>(isolate, FromAtomic<T>(result));
}


template <typename T>
inline Object* DoAnd(Isolate* isolate, void* buffer, size_t index,
                     Handle<Object> obj) {
  typedef typename FromObjectTraits<T>::atomic_type atomic_type;
  typedef typename FromObjectTraits<T>::convert_type convert_type;
  atomic_type value = ToAtomic<atomic_type>(FromObject<convert_type>(obj));
  atomic_type result =
      AndSeqCst(static_cast<atomic_type*>(buffer) + index, value);
  return ToObject<T>(isolate, FromAtomic<T>(result));
}


template <typename T>
inline Object* DoOr(Isolate* isolate, void* buffer, size_t index,
                    Handle<Object> obj) {
  typedef typename FromObjectTraits<T>::atomic_type atomic_type;
  typedef typename FromObjectTraits<T>::convert_type convert_type;
  atomic_type value = ToAtomic<atomic_type>(FromObject<convert_type>(obj));
  atomic_type result =
      OrSeqCst(static_cast<atomic_type*>(buffer) + index, value);
  return ToObject<T>(isolate, FromAtomic<T>(result));
}


template <typename T>
inline Object* DoXor(Isolate* isolate, void* buffer, size_t index,
                     Handle<Object> obj) {
  typedef typename FromObjectTraits<T>::atomic_type atomic_type;
  typedef typename FromObjectTraits<T>::convert_type convert_type;
  atomic_type value = ToAtomic<atomic_type>(FromObject<convert_type>(obj));
  atomic_type result =
      XorSeqCst(static_cast<atomic_type*>(buffer) + index, value);
  return ToObject<T>(isolate, FromAtomic<T>(result));
}


template <typename T>
inline Object* DoExchange(Isolate* isolate, void* buffer, size_t index,
                          Handle<Object> obj) {
  typedef typename FromObjectTraits<T>::atomic_type atomic_type;
  typedef typename FromObjectTraits<T>::convert_type convert_type;
  atomic_type value = ToAtomic<atomic_type>(FromObject<convert_type>(obj));
  atomic_type result =
      ExchangeSeqCst(static_cast<atomic_type*>(buffer) + index, value);
  return ToObject<T>(isolate, FromAtomic<T>(result));
}

437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509

// Uint8Clamped functions

uint8_t ClampToUint8(int32_t value) {
  if (value < 0) return 0;
  if (value > 255) return 255;
  return value;
}


inline Object* DoCompareExchangeUint8Clamped(Isolate* isolate, void* buffer,
                                             size_t index,
                                             Handle<Object> oldobj,
                                             Handle<Object> newobj) {
  typedef int32_t convert_type;
  typedef uint8_t atomic_type;
  atomic_type oldval = ClampToUint8(FromObject<convert_type>(oldobj));
  atomic_type newval = ClampToUint8(FromObject<convert_type>(newobj));
  atomic_type result = CompareExchangeSeqCst(
      static_cast<atomic_type*>(buffer) + index, oldval, newval);
  return ToObject<uint8_t>(isolate, FromAtomic<uint8_t>(result));
}


inline Object* DoStoreUint8Clamped(Isolate* isolate, void* buffer, size_t index,
                                   Handle<Object> obj) {
  typedef int32_t convert_type;
  typedef uint8_t atomic_type;
  atomic_type value = ClampToUint8(FromObject<convert_type>(obj));
  StoreSeqCst(static_cast<atomic_type*>(buffer) + index, value);
  return *obj;
}


#define DO_UINT8_CLAMPED_OP(name, op)                                        \
  inline Object* Do##name##Uint8Clamped(Isolate* isolate, void* buffer,      \
                                        size_t index, Handle<Object> obj) {  \
    typedef int32_t convert_type;                                            \
    typedef uint8_t atomic_type;                                             \
    atomic_type* p = static_cast<atomic_type*>(buffer) + index;              \
    convert_type operand = FromObject<convert_type>(obj);                    \
    atomic_type expected;                                                    \
    atomic_type result;                                                      \
    do {                                                                     \
      expected = *p;                                                         \
      result = ClampToUint8(static_cast<convert_type>(expected) op operand); \
    } while (CompareExchangeSeqCst(p, expected, result) != expected);        \
    return ToObject<uint8_t>(isolate, expected);                             \
  }

DO_UINT8_CLAMPED_OP(Add, +)
DO_UINT8_CLAMPED_OP(Sub, -)
DO_UINT8_CLAMPED_OP(And, &)
DO_UINT8_CLAMPED_OP(Or, | )
DO_UINT8_CLAMPED_OP(Xor, ^)

#undef DO_UINT8_CLAMPED_OP


inline Object* DoExchangeUint8Clamped(Isolate* isolate, void* buffer,
                                      size_t index, Handle<Object> obj) {
  typedef int32_t convert_type;
  typedef uint8_t atomic_type;
  atomic_type* p = static_cast<atomic_type*>(buffer) + index;
  atomic_type result = ClampToUint8(FromObject<convert_type>(obj));
  atomic_type expected;
  do {
    expected = *p;
  } while (CompareExchangeSeqCst(p, expected, result) != expected);
  return ToObject<uint8_t>(isolate, expected);
}


binji's avatar
binji committed
510 511 512 513 514 515 516 517 518 519
}  // anonymous namespace

// Duplicated from objects.h
// V has parameters (Type, type, TYPE, C type, element_size)
#define INTEGER_TYPED_ARRAYS(V)          \
  V(Uint8, uint8, UINT8, uint8_t, 1)     \
  V(Int8, int8, INT8, int8_t, 1)         \
  V(Uint16, uint16, UINT16, uint16_t, 2) \
  V(Int16, int16, INT16, int16_t, 2)     \
  V(Uint32, uint32, UINT32, uint32_t, 4) \
520
  V(Int32, int32, INT32, int32_t, 4)
binji's avatar
binji committed
521 522 523 524 525 526 527 528 529


RUNTIME_FUNCTION(Runtime_AtomicsCompareExchange) {
  HandleScope scope(isolate);
  DCHECK(args.length() == 4);
  CONVERT_ARG_HANDLE_CHECKED(JSTypedArray, sta, 0);
  CONVERT_SIZE_ARG_CHECKED(index, 1);
  CONVERT_NUMBER_ARG_HANDLE_CHECKED(oldobj, 2);
  CONVERT_NUMBER_ARG_HANDLE_CHECKED(newobj, 3);
530 531
  RUNTIME_ASSERT(sta->GetBuffer()->is_shared());
  RUNTIME_ASSERT(index < NumberToSize(isolate, sta->length()));
binji's avatar
binji committed
532 533 534 535 536 537 538 539

  void* buffer = sta->GetBuffer()->backing_store();

  switch (sta->type()) {
#define TYPED_ARRAY_CASE(Type, typeName, TYPE, ctype, size) \
  case kExternal##Type##Array:                              \
    return DoCompareExchange<ctype>(isolate, buffer, index, oldobj, newobj);

540
    INTEGER_TYPED_ARRAYS(TYPED_ARRAY_CASE)
binji's avatar
binji committed
541 542
#undef TYPED_ARRAY_CASE

543 544 545 546 547 548 549 550 551 552
    case kExternalFloat32Array:
      return DoCompareExchange<float>(isolate, buffer, index, oldobj, newobj);

    case kExternalFloat64Array:
      return DoCompareExchange<double>(isolate, buffer, index, oldobj, newobj);

    case kExternalUint8ClampedArray:
      return DoCompareExchangeUint8Clamped(isolate, buffer, index, oldobj,
                                           newobj);

binji's avatar
binji committed
553 554 555 556 557 558 559 560 561 562 563 564 565 566
    default:
      break;
  }

  UNREACHABLE();
  return isolate->heap()->undefined_value();
}


RUNTIME_FUNCTION(Runtime_AtomicsLoad) {
  HandleScope scope(isolate);
  DCHECK(args.length() == 2);
  CONVERT_ARG_HANDLE_CHECKED(JSTypedArray, sta, 0);
  CONVERT_SIZE_ARG_CHECKED(index, 1);
567 568
  RUNTIME_ASSERT(sta->GetBuffer()->is_shared());
  RUNTIME_ASSERT(index < NumberToSize(isolate, sta->length()));
binji's avatar
binji committed
569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594

  void* buffer = sta->GetBuffer()->backing_store();

  switch (sta->type()) {
#define TYPED_ARRAY_CASE(Type, typeName, TYPE, ctype, size) \
  case kExternal##Type##Array:                              \
    return DoLoad<ctype>(isolate, buffer, index);

    TYPED_ARRAYS(TYPED_ARRAY_CASE)
#undef TYPED_ARRAY_CASE

    default:
      break;
  }

  UNREACHABLE();
  return isolate->heap()->undefined_value();
}


RUNTIME_FUNCTION(Runtime_AtomicsStore) {
  HandleScope scope(isolate);
  DCHECK(args.length() == 3);
  CONVERT_ARG_HANDLE_CHECKED(JSTypedArray, sta, 0);
  CONVERT_SIZE_ARG_CHECKED(index, 1);
  CONVERT_NUMBER_ARG_HANDLE_CHECKED(value, 2);
595 596
  RUNTIME_ASSERT(sta->GetBuffer()->is_shared());
  RUNTIME_ASSERT(index < NumberToSize(isolate, sta->length()));
binji's avatar
binji committed
597 598 599 600 601 602 603 604

  void* buffer = sta->GetBuffer()->backing_store();

  switch (sta->type()) {
#define TYPED_ARRAY_CASE(Type, typeName, TYPE, ctype, size) \
  case kExternal##Type##Array:                              \
    return DoStore<ctype>(isolate, buffer, index, value);

605
    INTEGER_TYPED_ARRAYS(TYPED_ARRAY_CASE)
binji's avatar
binji committed
606 607
#undef TYPED_ARRAY_CASE

608 609 610 611 612 613 614 615 616
    case kExternalFloat32Array:
      return DoStore<float>(isolate, buffer, index, value);

    case kExternalFloat64Array:
      return DoStore<double>(isolate, buffer, index, value);

    case kExternalUint8ClampedArray:
      return DoStoreUint8Clamped(isolate, buffer, index, value);

binji's avatar
binji committed
617 618 619 620 621 622 623 624 625 626 627 628 629 630 631
    default:
      break;
  }

  UNREACHABLE();
  return isolate->heap()->undefined_value();
}


RUNTIME_FUNCTION(Runtime_AtomicsAdd) {
  HandleScope scope(isolate);
  DCHECK(args.length() == 3);
  CONVERT_ARG_HANDLE_CHECKED(JSTypedArray, sta, 0);
  CONVERT_SIZE_ARG_CHECKED(index, 1);
  CONVERT_NUMBER_ARG_HANDLE_CHECKED(value, 2);
632 633
  RUNTIME_ASSERT(sta->GetBuffer()->is_shared());
  RUNTIME_ASSERT(index < NumberToSize(isolate, sta->length()));
binji's avatar
binji committed
634 635 636 637 638 639 640 641 642 643 644

  void* buffer = sta->GetBuffer()->backing_store();

  switch (sta->type()) {
#define TYPED_ARRAY_CASE(Type, typeName, TYPE, ctype, size) \
  case kExternal##Type##Array:                              \
    return DoAdd<ctype>(isolate, buffer, index, value);

    INTEGER_TYPED_ARRAYS(TYPED_ARRAY_CASE)
#undef TYPED_ARRAY_CASE

645 646 647
    case kExternalUint8ClampedArray:
      return DoAddUint8Clamped(isolate, buffer, index, value);

binji's avatar
binji committed
648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664
    case kExternalFloat32Array:
    case kExternalFloat64Array:
    default:
      break;
  }

  UNREACHABLE();
  return isolate->heap()->undefined_value();
}


RUNTIME_FUNCTION(Runtime_AtomicsSub) {
  HandleScope scope(isolate);
  DCHECK(args.length() == 3);
  CONVERT_ARG_HANDLE_CHECKED(JSTypedArray, sta, 0);
  CONVERT_SIZE_ARG_CHECKED(index, 1);
  CONVERT_NUMBER_ARG_HANDLE_CHECKED(value, 2);
665 666
  RUNTIME_ASSERT(sta->GetBuffer()->is_shared());
  RUNTIME_ASSERT(index < NumberToSize(isolate, sta->length()));
binji's avatar
binji committed
667 668 669 670 671 672 673 674 675 676 677

  void* buffer = sta->GetBuffer()->backing_store();

  switch (sta->type()) {
#define TYPED_ARRAY_CASE(Type, typeName, TYPE, ctype, size) \
  case kExternal##Type##Array:                              \
    return DoSub<ctype>(isolate, buffer, index, value);

    INTEGER_TYPED_ARRAYS(TYPED_ARRAY_CASE)
#undef TYPED_ARRAY_CASE

678 679 680
    case kExternalUint8ClampedArray:
      return DoSubUint8Clamped(isolate, buffer, index, value);

binji's avatar
binji committed
681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697
    case kExternalFloat32Array:
    case kExternalFloat64Array:
    default:
      break;
  }

  UNREACHABLE();
  return isolate->heap()->undefined_value();
}


RUNTIME_FUNCTION(Runtime_AtomicsAnd) {
  HandleScope scope(isolate);
  DCHECK(args.length() == 3);
  CONVERT_ARG_HANDLE_CHECKED(JSTypedArray, sta, 0);
  CONVERT_SIZE_ARG_CHECKED(index, 1);
  CONVERT_NUMBER_ARG_HANDLE_CHECKED(value, 2);
698 699
  RUNTIME_ASSERT(sta->GetBuffer()->is_shared());
  RUNTIME_ASSERT(index < NumberToSize(isolate, sta->length()));
binji's avatar
binji committed
700 701 702 703 704 705 706 707 708 709 710

  void* buffer = sta->GetBuffer()->backing_store();

  switch (sta->type()) {
#define TYPED_ARRAY_CASE(Type, typeName, TYPE, ctype, size) \
  case kExternal##Type##Array:                              \
    return DoAnd<ctype>(isolate, buffer, index, value);

    INTEGER_TYPED_ARRAYS(TYPED_ARRAY_CASE)
#undef TYPED_ARRAY_CASE

711 712 713
    case kExternalUint8ClampedArray:
      return DoAndUint8Clamped(isolate, buffer, index, value);

binji's avatar
binji committed
714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730
    case kExternalFloat32Array:
    case kExternalFloat64Array:
    default:
      break;
  }

  UNREACHABLE();
  return isolate->heap()->undefined_value();
}


RUNTIME_FUNCTION(Runtime_AtomicsOr) {
  HandleScope scope(isolate);
  DCHECK(args.length() == 3);
  CONVERT_ARG_HANDLE_CHECKED(JSTypedArray, sta, 0);
  CONVERT_SIZE_ARG_CHECKED(index, 1);
  CONVERT_NUMBER_ARG_HANDLE_CHECKED(value, 2);
731 732
  RUNTIME_ASSERT(sta->GetBuffer()->is_shared());
  RUNTIME_ASSERT(index < NumberToSize(isolate, sta->length()));
binji's avatar
binji committed
733 734 735 736 737 738 739 740 741 742 743

  void* buffer = sta->GetBuffer()->backing_store();

  switch (sta->type()) {
#define TYPED_ARRAY_CASE(Type, typeName, TYPE, ctype, size) \
  case kExternal##Type##Array:                              \
    return DoOr<ctype>(isolate, buffer, index, value);

    INTEGER_TYPED_ARRAYS(TYPED_ARRAY_CASE)
#undef TYPED_ARRAY_CASE

744 745 746
    case kExternalUint8ClampedArray:
      return DoOrUint8Clamped(isolate, buffer, index, value);

binji's avatar
binji committed
747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763
    case kExternalFloat32Array:
    case kExternalFloat64Array:
    default:
      break;
  }

  UNREACHABLE();
  return isolate->heap()->undefined_value();
}


RUNTIME_FUNCTION(Runtime_AtomicsXor) {
  HandleScope scope(isolate);
  DCHECK(args.length() == 3);
  CONVERT_ARG_HANDLE_CHECKED(JSTypedArray, sta, 0);
  CONVERT_SIZE_ARG_CHECKED(index, 1);
  CONVERT_NUMBER_ARG_HANDLE_CHECKED(value, 2);
764 765
  RUNTIME_ASSERT(sta->GetBuffer()->is_shared());
  RUNTIME_ASSERT(index < NumberToSize(isolate, sta->length()));
binji's avatar
binji committed
766 767 768 769 770 771 772 773 774 775 776

  void* buffer = sta->GetBuffer()->backing_store();

  switch (sta->type()) {
#define TYPED_ARRAY_CASE(Type, typeName, TYPE, ctype, size) \
  case kExternal##Type##Array:                              \
    return DoXor<ctype>(isolate, buffer, index, value);

    INTEGER_TYPED_ARRAYS(TYPED_ARRAY_CASE)
#undef TYPED_ARRAY_CASE

777 778 779
    case kExternalUint8ClampedArray:
      return DoXorUint8Clamped(isolate, buffer, index, value);

binji's avatar
binji committed
780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796
    case kExternalFloat32Array:
    case kExternalFloat64Array:
    default:
      break;
  }

  UNREACHABLE();
  return isolate->heap()->undefined_value();
}


RUNTIME_FUNCTION(Runtime_AtomicsExchange) {
  HandleScope scope(isolate);
  DCHECK(args.length() == 3);
  CONVERT_ARG_HANDLE_CHECKED(JSTypedArray, sta, 0);
  CONVERT_SIZE_ARG_CHECKED(index, 1);
  CONVERT_NUMBER_ARG_HANDLE_CHECKED(value, 2);
797 798
  RUNTIME_ASSERT(sta->GetBuffer()->is_shared());
  RUNTIME_ASSERT(index < NumberToSize(isolate, sta->length()));
binji's avatar
binji committed
799 800 801 802 803 804 805 806 807 808 809

  void* buffer = sta->GetBuffer()->backing_store();

  switch (sta->type()) {
#define TYPED_ARRAY_CASE(Type, typeName, TYPE, ctype, size) \
  case kExternal##Type##Array:                              \
    return DoExchange<ctype>(isolate, buffer, index, value);

    INTEGER_TYPED_ARRAYS(TYPED_ARRAY_CASE)
#undef TYPED_ARRAY_CASE

810 811 812
    case kExternalUint8ClampedArray:
      return DoExchangeUint8Clamped(isolate, buffer, index, value);

binji's avatar
binji committed
813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834
    case kExternalFloat32Array:
    case kExternalFloat64Array:
    default:
      break;
  }

  UNREACHABLE();
  return isolate->heap()->undefined_value();
}


RUNTIME_FUNCTION(Runtime_AtomicsIsLockFree) {
  HandleScope scope(isolate);
  DCHECK(args.length() == 1);
  CONVERT_NUMBER_ARG_HANDLE_CHECKED(size, 0);
  uint32_t usize = NumberToUint32(*size);

  return Runtime::AtomicIsLockFree(usize) ? isolate->heap()->true_value()
                                          : isolate->heap()->false_value();
}
}
}  // namespace v8::internal