runtime-atomics.cc 18.6 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 9

#include "src/arguments.h"
#include "src/base/macros.h"
#include "src/base/platform/mutex.h"
10 11
#include "src/conversions-inl.h"
#include "src/factory.h"
binji's avatar
binji committed
12 13 14

// Implement Atomic accesses to 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 20 21

namespace v8 {
namespace internal {

namespace {

22 23 24 25
inline bool AtomicIsLockFree(uint32_t size) {
  return size == 1 || size == 2 || size == 4;
}

binji's avatar
binji committed
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
#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 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);
}

#elif V8_CC_MSVC

67 68 69 70 71 72 73 74 75
#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
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
#define ATOMIC_OPS(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));            \
  }                                                                         \
                                                                            \
103 104 105 106
  inline type CompareExchangeSeqCst(type* p, type oldval, type newval) {    \
    return InterlockedCompareExchange##suffix(reinterpret_cast<vctype*>(p), \
                                              bit_cast<vctype>(newval),     \
                                              bit_cast<vctype>(oldval));    \
binji's avatar
binji committed
107
  }
108 109 110 111 112 113 114 115 116

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) */

#undef ATOMIC_OPS_INTEGER
binji's avatar
binji committed
117 118
#undef ATOMIC_OPS

119 120 121 122 123 124 125 126 127
#undef InterlockedCompareExchange32
#undef InterlockedExchange32
#undef InterlockedExchangeAdd32
#undef InterlockedAnd32
#undef InterlockedOr32
#undef InterlockedXor32
#undef InterlockedExchangeAdd16
#undef InterlockedCompareExchange8
#undef InterlockedExchangeAdd8
binji's avatar
binji committed
128 129 130 131 132 133 134 135 136 137 138

#else

#error Unsupported platform!

#endif

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

template <>
binji's avatar
binji committed
139
inline uint8_t FromObject<uint8_t>(Handle<Object> number) {
binji's avatar
binji committed
140 141 142 143
  return NumberToUint32(*number);
}

template <>
binji's avatar
binji committed
144
inline int8_t FromObject<int8_t>(Handle<Object> number) {
binji's avatar
binji committed
145 146 147 148
  return NumberToInt32(*number);
}

template <>
binji's avatar
binji committed
149 150
inline uint16_t FromObject<uint16_t>(Handle<Object> number) {
  return NumberToUint32(*number);
binji's avatar
binji committed
151 152 153
}

template <>
binji's avatar
binji committed
154 155
inline int16_t FromObject<int16_t>(Handle<Object> number) {
  return NumberToInt32(*number);
binji's avatar
binji committed
156 157 158
}

template <>
binji's avatar
binji committed
159 160
inline uint32_t FromObject<uint32_t>(Handle<Object> number) {
  return NumberToUint32(*number);
binji's avatar
binji committed
161 162 163
}

template <>
binji's avatar
binji committed
164 165
inline int32_t FromObject<int32_t>(Handle<Object> number) {
  return NumberToInt32(*number);
binji's avatar
binji committed
166 167 168
}


binji's avatar
binji committed
169
inline Object* ToObject(Isolate* isolate, int8_t t) { return Smi::FromInt(t); }
binji's avatar
binji committed
170

binji's avatar
binji committed
171
inline Object* ToObject(Isolate* isolate, uint8_t t) { return Smi::FromInt(t); }
binji's avatar
binji committed
172

binji's avatar
binji committed
173
inline Object* ToObject(Isolate* isolate, int16_t t) { return Smi::FromInt(t); }
binji's avatar
binji committed
174

binji's avatar
binji committed
175 176 177
inline Object* ToObject(Isolate* isolate, uint16_t t) {
  return Smi::FromInt(t);
}
binji's avatar
binji committed
178 179


binji's avatar
binji committed
180 181 182
inline Object* ToObject(Isolate* isolate, int32_t t) {
  return *isolate->factory()->NewNumber(t);
}
binji's avatar
binji committed
183 184


binji's avatar
binji committed
185 186 187
inline Object* ToObject(Isolate* isolate, uint32_t t) {
  return *isolate->factory()->NewNumber(t);
}
binji's avatar
binji committed
188 189 190 191 192


template <typename T>
inline Object* DoCompareExchange(Isolate* isolate, void* buffer, size_t index,
                                 Handle<Object> oldobj, Handle<Object> newobj) {
binji's avatar
binji committed
193 194 195 196 197
  T oldval = FromObject<T>(oldobj);
  T newval = FromObject<T>(newobj);
  T result =
      CompareExchangeSeqCst(static_cast<T*>(buffer) + index, oldval, newval);
  return ToObject(isolate, result);
binji's avatar
binji committed
198 199 200 201 202 203
}


template <typename T>
inline Object* DoAdd(Isolate* isolate, void* buffer, size_t index,
                     Handle<Object> obj) {
binji's avatar
binji committed
204 205 206
  T value = FromObject<T>(obj);
  T result = AddSeqCst(static_cast<T*>(buffer) + index, value);
  return ToObject(isolate, result);
binji's avatar
binji committed
207 208 209 210 211 212
}


template <typename T>
inline Object* DoSub(Isolate* isolate, void* buffer, size_t index,
                     Handle<Object> obj) {
binji's avatar
binji committed
213 214 215
  T value = FromObject<T>(obj);
  T result = SubSeqCst(static_cast<T*>(buffer) + index, value);
  return ToObject(isolate, result);
binji's avatar
binji committed
216 217 218 219 220 221
}


template <typename T>
inline Object* DoAnd(Isolate* isolate, void* buffer, size_t index,
                     Handle<Object> obj) {
binji's avatar
binji committed
222 223 224
  T value = FromObject<T>(obj);
  T result = AndSeqCst(static_cast<T*>(buffer) + index, value);
  return ToObject(isolate, result);
binji's avatar
binji committed
225 226 227 228 229 230
}


template <typename T>
inline Object* DoOr(Isolate* isolate, void* buffer, size_t index,
                    Handle<Object> obj) {
binji's avatar
binji committed
231 232 233
  T value = FromObject<T>(obj);
  T result = OrSeqCst(static_cast<T*>(buffer) + index, value);
  return ToObject(isolate, result);
binji's avatar
binji committed
234 235 236 237 238 239
}


template <typename T>
inline Object* DoXor(Isolate* isolate, void* buffer, size_t index,
                     Handle<Object> obj) {
binji's avatar
binji committed
240 241 242
  T value = FromObject<T>(obj);
  T result = XorSeqCst(static_cast<T*>(buffer) + index, value);
  return ToObject(isolate, result);
binji's avatar
binji committed
243 244 245 246 247 248
}


template <typename T>
inline Object* DoExchange(Isolate* isolate, void* buffer, size_t index,
                          Handle<Object> obj) {
binji's avatar
binji committed
249 250 251
  T value = FromObject<T>(obj);
  T result = ExchangeSeqCst(static_cast<T*>(buffer) + index, value);
  return ToObject(isolate, result);
binji's avatar
binji committed
252 253
}

254 255 256 257 258 259 260 261 262 263 264 265 266 267 268

// 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;
binji's avatar
binji committed
269 270 271 272 273
  uint8_t oldval = ClampToUint8(FromObject<convert_type>(oldobj));
  uint8_t newval = ClampToUint8(FromObject<convert_type>(newobj));
  uint8_t result = CompareExchangeSeqCst(static_cast<uint8_t*>(buffer) + index,
                                         oldval, newval);
  return ToObject(isolate, result);
274 275 276 277 278 279 280
}


#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;                                            \
binji's avatar
binji committed
281
    uint8_t* p = static_cast<uint8_t*>(buffer) + index;                      \
282
    convert_type operand = FromObject<convert_type>(obj);                    \
binji's avatar
binji committed
283 284
    uint8_t expected;                                                        \
    uint8_t result;                                                          \
285 286 287 288
    do {                                                                     \
      expected = *p;                                                         \
      result = ClampToUint8(static_cast<convert_type>(expected) op operand); \
    } while (CompareExchangeSeqCst(p, expected, result) != expected);        \
binji's avatar
binji committed
289
    return ToObject(isolate, expected);                                      \
290 291 292 293 294 295 296 297 298 299 300 301 302 303
  }

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;
binji's avatar
binji committed
304 305 306
  uint8_t* p = static_cast<uint8_t*>(buffer) + index;
  uint8_t result = ClampToUint8(FromObject<convert_type>(obj));
  uint8_t expected;
307 308 309
  do {
    expected = *p;
  } while (CompareExchangeSeqCst(p, expected, result) != expected);
binji's avatar
binji committed
310
  return ToObject(isolate, expected);
311 312 313
}


binji's avatar
binji committed
314 315 316 317 318 319 320 321 322 323
}  // 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) \
324
  V(Int32, int32, INT32, int32_t, 4)
binji's avatar
binji committed
325

326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348
RUNTIME_FUNCTION(Runtime_ThrowNotIntegerSharedTypedArrayError) {
  HandleScope scope(isolate);
  DCHECK_EQ(1, args.length());
  CONVERT_ARG_HANDLE_CHECKED(Object, value, 0);
  THROW_NEW_ERROR_RETURN_FAILURE(
      isolate,
      NewTypeError(MessageTemplate::kNotIntegerSharedTypedArray, value));
}

RUNTIME_FUNCTION(Runtime_ThrowNotInt32SharedTypedArrayError) {
  HandleScope scope(isolate);
  DCHECK_EQ(1, args.length());
  CONVERT_ARG_HANDLE_CHECKED(Object, value, 0);
  THROW_NEW_ERROR_RETURN_FAILURE(
      isolate, NewTypeError(MessageTemplate::kNotInt32SharedTypedArray, value));
}

RUNTIME_FUNCTION(Runtime_ThrowInvalidAtomicAccessIndexError) {
  HandleScope scope(isolate);
  DCHECK_EQ(0, args.length());
  THROW_NEW_ERROR_RETURN_FAILURE(
      isolate, NewRangeError(MessageTemplate::kInvalidAtomicAccessIndex));
}
binji's avatar
binji committed
349 350 351 352 353 354 355 356

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);
357
  CHECK(sta->GetBuffer()->is_shared());
358
  CHECK_LT(index, NumberToSize(sta->length()));
binji's avatar
binji committed
359

360
  uint8_t* source = static_cast<uint8_t*>(sta->GetBuffer()->backing_store()) +
361
                    NumberToSize(sta->byte_offset());
binji's avatar
binji committed
362 363 364 365

  switch (sta->type()) {
#define TYPED_ARRAY_CASE(Type, typeName, TYPE, ctype, size) \
  case kExternal##Type##Array:                              \
366
    return DoCompareExchange<ctype>(isolate, source, index, oldobj, newobj);
binji's avatar
binji committed
367

368
    INTEGER_TYPED_ARRAYS(TYPED_ARRAY_CASE)
binji's avatar
binji committed
369 370
#undef TYPED_ARRAY_CASE

371
    case kExternalUint8ClampedArray:
372
      return DoCompareExchangeUint8Clamped(isolate, source, index, oldobj,
373 374
                                           newobj);

binji's avatar
binji committed
375
    default:
376 377 378
      break;
  }

binji's avatar
binji committed
379 380 381 382 383 384 385 386 387 388 389
  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);
390
  CHECK(sta->GetBuffer()->is_shared());
391
  CHECK_LT(index, NumberToSize(sta->length()));
binji's avatar
binji committed
392

393
  uint8_t* source = static_cast<uint8_t*>(sta->GetBuffer()->backing_store()) +
394
                    NumberToSize(sta->byte_offset());
binji's avatar
binji committed
395 396 397 398

  switch (sta->type()) {
#define TYPED_ARRAY_CASE(Type, typeName, TYPE, ctype, size) \
  case kExternal##Type##Array:                              \
399
    return DoAdd<ctype>(isolate, source, index, value);
binji's avatar
binji committed
400 401 402 403

    INTEGER_TYPED_ARRAYS(TYPED_ARRAY_CASE)
#undef TYPED_ARRAY_CASE

404
    case kExternalUint8ClampedArray:
405
      return DoAddUint8Clamped(isolate, source, index, value);
406

binji's avatar
binji committed
407 408 409 410 411 412 413 414 415 416 417 418 419 420 421
    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);
422
  CHECK(sta->GetBuffer()->is_shared());
423
  CHECK_LT(index, NumberToSize(sta->length()));
binji's avatar
binji committed
424

425
  uint8_t* source = static_cast<uint8_t*>(sta->GetBuffer()->backing_store()) +
426
                    NumberToSize(sta->byte_offset());
binji's avatar
binji committed
427 428 429 430

  switch (sta->type()) {
#define TYPED_ARRAY_CASE(Type, typeName, TYPE, ctype, size) \
  case kExternal##Type##Array:                              \
431
    return DoSub<ctype>(isolate, source, index, value);
binji's avatar
binji committed
432 433 434 435

    INTEGER_TYPED_ARRAYS(TYPED_ARRAY_CASE)
#undef TYPED_ARRAY_CASE

436
    case kExternalUint8ClampedArray:
437
      return DoSubUint8Clamped(isolate, source, index, value);
438

binji's avatar
binji committed
439 440 441 442 443 444 445 446 447 448 449 450 451 452 453
    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);
454
  CHECK(sta->GetBuffer()->is_shared());
455
  CHECK_LT(index, NumberToSize(sta->length()));
binji's avatar
binji committed
456

457
  uint8_t* source = static_cast<uint8_t*>(sta->GetBuffer()->backing_store()) +
458
                    NumberToSize(sta->byte_offset());
binji's avatar
binji committed
459 460 461 462

  switch (sta->type()) {
#define TYPED_ARRAY_CASE(Type, typeName, TYPE, ctype, size) \
  case kExternal##Type##Array:                              \
463
    return DoAnd<ctype>(isolate, source, index, value);
binji's avatar
binji committed
464 465 466 467

    INTEGER_TYPED_ARRAYS(TYPED_ARRAY_CASE)
#undef TYPED_ARRAY_CASE

468
    case kExternalUint8ClampedArray:
469
      return DoAndUint8Clamped(isolate, source, index, value);
470

binji's avatar
binji committed
471 472 473 474 475 476 477 478 479 480 481 482 483 484 485
    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);
486
  CHECK(sta->GetBuffer()->is_shared());
487
  CHECK_LT(index, NumberToSize(sta->length()));
binji's avatar
binji committed
488

489
  uint8_t* source = static_cast<uint8_t*>(sta->GetBuffer()->backing_store()) +
490
                    NumberToSize(sta->byte_offset());
binji's avatar
binji committed
491 492 493 494

  switch (sta->type()) {
#define TYPED_ARRAY_CASE(Type, typeName, TYPE, ctype, size) \
  case kExternal##Type##Array:                              \
495
    return DoOr<ctype>(isolate, source, index, value);
binji's avatar
binji committed
496 497 498 499

    INTEGER_TYPED_ARRAYS(TYPED_ARRAY_CASE)
#undef TYPED_ARRAY_CASE

500
    case kExternalUint8ClampedArray:
501
      return DoOrUint8Clamped(isolate, source, index, value);
502

binji's avatar
binji committed
503 504 505 506 507 508 509 510 511 512 513 514 515 516 517
    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);
518
  CHECK(sta->GetBuffer()->is_shared());
519
  CHECK_LT(index, NumberToSize(sta->length()));
binji's avatar
binji committed
520

521
  uint8_t* source = static_cast<uint8_t*>(sta->GetBuffer()->backing_store()) +
522
                    NumberToSize(sta->byte_offset());
binji's avatar
binji committed
523 524 525 526

  switch (sta->type()) {
#define TYPED_ARRAY_CASE(Type, typeName, TYPE, ctype, size) \
  case kExternal##Type##Array:                              \
527
    return DoXor<ctype>(isolate, source, index, value);
binji's avatar
binji committed
528 529 530 531

    INTEGER_TYPED_ARRAYS(TYPED_ARRAY_CASE)
#undef TYPED_ARRAY_CASE

532
    case kExternalUint8ClampedArray:
533
      return DoXorUint8Clamped(isolate, source, index, value);
534

binji's avatar
binji committed
535 536 537 538 539 540 541 542 543 544 545 546 547 548 549
    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);
550
  CHECK(sta->GetBuffer()->is_shared());
551
  CHECK_LT(index, NumberToSize(sta->length()));
binji's avatar
binji committed
552

553
  uint8_t* source = static_cast<uint8_t*>(sta->GetBuffer()->backing_store()) +
554
                    NumberToSize(sta->byte_offset());
binji's avatar
binji committed
555 556 557 558

  switch (sta->type()) {
#define TYPED_ARRAY_CASE(Type, typeName, TYPE, ctype, size) \
  case kExternal##Type##Array:                              \
559
    return DoExchange<ctype>(isolate, source, index, value);
binji's avatar
binji committed
560 561 562 563

    INTEGER_TYPED_ARRAYS(TYPED_ARRAY_CASE)
#undef TYPED_ARRAY_CASE

564
    case kExternalUint8ClampedArray:
565
      return DoExchangeUint8Clamped(isolate, source, index, value);
566

binji's avatar
binji committed
567 568 569 570 571 572 573 574 575 576 577 578 579 580
    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);
581
  return isolate->heap()->ToBoolean(AtomicIsLockFree(usize));
binji's avatar
binji committed
582
}
583 584
}  // namespace internal
}  // namespace v8