Index: src/runtime/runtime-atomics.cc |
diff --git a/src/runtime/runtime-atomics.cc b/src/runtime/runtime-atomics.cc |
index b455622e4bb2eefd87f9fdb78d53ce7d31ed1f5b..8602eb7a357d31b275d9797446de8e4a3f2c49a1 100644 |
--- a/src/runtime/runtime-atomics.cc |
+++ b/src/runtime/runtime-atomics.cc |
@@ -26,15 +26,29 @@ inline T ExchangeSeqCst(T* p, T value) { |
return __atomic_exchange_n(p, value, __ATOMIC_SEQ_CST); |
} |
+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; |
+} |
+ |
#elif V8_CC_MSVC |
#define InterlockedExchange32 _InterlockedExchange |
+#define InterlockedCompareExchange32 _InterlockedCompareExchange |
+#define InterlockedCompareExchange8 _InterlockedCompareExchange8 |
#define ATOMIC_OPS(type, suffix, vctype) \ |
inline type ExchangeSeqCst(type* p, type value) { \ |
return InterlockedExchange##suffix(reinterpret_cast<vctype*>(p), \ |
bit_cast<vctype>(value)); \ |
} \ |
+ inline type CompareExchangeSeqCst(type* p, type oldval, type newval) { \ |
+ return InterlockedCompareExchange##suffix(reinterpret_cast<vctype*>(p), \ |
+ bit_cast<vctype>(newval), \ |
+ bit_cast<vctype>(oldval)); \ |
+ } |
ATOMIC_OPS(int8_t, 8, char) |
ATOMIC_OPS(uint8_t, 8, char) |
@@ -47,6 +61,8 @@ ATOMIC_OPS(uint32_t, 32, long) /* NOLINT(runtime/int) */ |
#undef ATOMIC_OPS |
#undef InterlockedExchange32 |
+#undef InterlockedCompareExchange32 |
+#undef InterlockedCompareExchange8 |
#else |
@@ -114,6 +130,16 @@ inline Object* DoExchange(Isolate* isolate, void* buffer, size_t index, |
return ToObject(isolate, result); |
} |
+template <typename T> |
+inline Object* DoCompareExchange(Isolate* isolate, void* buffer, size_t index, |
+ Handle<Object> oldobj, Handle<Object> newobj) { |
+ T oldval = FromObject<T>(oldobj); |
+ T newval = FromObject<T>(newobj); |
+ T result = |
+ CompareExchangeSeqCst(static_cast<T*>(buffer) + index, oldval, newval); |
+ return ToObject(isolate, result); |
+} |
+ |
} // anonymous namespace |
// Duplicated from objects.h |
@@ -178,6 +204,34 @@ RUNTIME_FUNCTION(Runtime_AtomicsExchange) { |
return isolate->heap()->undefined_value(); |
} |
+RUNTIME_FUNCTION(Runtime_AtomicsCompareExchange) { |
+ HandleScope scope(isolate); |
+ DCHECK_EQ(4, args.length()); |
+ 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); |
+ CHECK(sta->GetBuffer()->is_shared()); |
+ CHECK_LT(index, NumberToSize(sta->length())); |
+ |
+ uint8_t* source = static_cast<uint8_t*>(sta->GetBuffer()->backing_store()) + |
+ NumberToSize(sta->byte_offset()); |
+ |
+ switch (sta->type()) { |
+#define TYPED_ARRAY_CASE(Type, typeName, TYPE, ctype, size) \ |
+ case kExternal##Type##Array: \ |
+ return DoCompareExchange<ctype>(isolate, source, index, oldobj, newobj); |
+ |
+ INTEGER_TYPED_ARRAYS(TYPED_ARRAY_CASE) |
+#undef TYPED_ARRAY_CASE |
+ |
+ default: |
+ break; |
+ } |
+ |
+ UNREACHABLE(); |
+ return isolate->heap()->undefined_value(); |
+} |
} // namespace internal |
} // namespace v8 |