Index: src/runtime/runtime-atomics.cc |
diff --git a/src/runtime/runtime-atomics.cc b/src/runtime/runtime-atomics.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..b1febb5bc7271e61bc69b5b9d86660fdaf83cf5e |
--- /dev/null |
+++ b/src/runtime/runtime-atomics.cc |
@@ -0,0 +1,555 @@ |
+// 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/conversions.h" |
+#include "src/runtime/runtime-utils.h" |
+ |
+ |
+namespace v8 { |
+namespace internal { |
+ |
+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* CompareExchange(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_compare_exchange_n(static_cast<atomic_type*>(buffer) + index, |
+ &oldval, newval, 0, __ATOMIC_SEQ_CST, |
+ __ATOMIC_SEQ_CST); |
+ return ToObject<T>(isolate, FromAtomic<T>(oldval)); |
+} |
+ |
+ |
+template <typename T> |
+inline Object* Load(Isolate* isolate, void* buffer, size_t index) { |
+ typedef typename FromObjectTraits<T>::atomic_type atomic_type; |
+ atomic_type result; |
+ __atomic_load(static_cast<atomic_type*>(buffer) + index, &result, |
+ __ATOMIC_SEQ_CST); |
+ return ToObject<T>(isolate, FromAtomic<T>(result)); |
+} |
+ |
+ |
+template <typename T> |
+inline Object* Store(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_store_n(static_cast<atomic_type*>(buffer) + index, value, |
+ __ATOMIC_SEQ_CST); |
+ return *obj; |
+} |
+ |
+ |
+template <typename T> |
+inline Object* Add(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 = __atomic_fetch_add( |
+ static_cast<atomic_type*>(buffer) + index, value, __ATOMIC_SEQ_CST); |
+ return ToObject<T>(isolate, FromAtomic<T>(result)); |
+} |
+ |
+ |
+template <typename T> |
+inline Object* Sub(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 = __atomic_fetch_sub( |
+ static_cast<atomic_type*>(buffer) + index, value, __ATOMIC_SEQ_CST); |
+ return ToObject<T>(isolate, FromAtomic<T>(result)); |
+} |
+ |
+ |
+template <typename T> |
+inline Object* And(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 = __atomic_fetch_and( |
+ static_cast<atomic_type*>(buffer) + index, value, __ATOMIC_SEQ_CST); |
+ return ToObject<T>(isolate, FromAtomic<T>(result)); |
+} |
+ |
+ |
+template <typename T> |
+inline Object* Or(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 = __atomic_fetch_or( |
+ static_cast<atomic_type*>(buffer) + index, value, __ATOMIC_SEQ_CST); |
+ return ToObject<T>(isolate, FromAtomic<T>(result)); |
+} |
+ |
+ |
+template <typename T> |
+inline Object* Xor(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 = __atomic_fetch_xor( |
+ static_cast<atomic_type*>(buffer) + index, value, __ATOMIC_SEQ_CST); |
+ return ToObject<T>(isolate, FromAtomic<T>(result)); |
+} |
+ |
+ |
+// 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) \ |
+ V(Int32, int32, INT32, int32_t, 4) \ |
+ V(Uint8Clamped, uint8_clamped, UINT8_CLAMPED, uint8_t, 1) |
+ |
+ |
+RUNTIME_FUNCTION(Runtime_AtomicsCompareExchange) { |
+ HandleScope scope(isolate); |
+ DCHECK(args.length() == 4); |
+ CONVERT_ARG_HANDLE_CHECKED(JSTypedArray, typedarray, 0); |
+ CONVERT_SIZE_ARG_CHECKED(index, 1); |
+ CONVERT_NUMBER_ARG_HANDLE_CHECKED(oldobj, 2); |
+ CONVERT_NUMBER_ARG_HANDLE_CHECKED(newobj, 3); |
+ |
+ Handle<JSTypedArray> sta(JSTypedArray::cast(*typedarray)); |
+ size_t sta_length = NumberToSize(isolate, sta->length()); |
+ |
+ DCHECK(typedarray->is_shared()); |
+ DCHECK(index < sta_length); |
+ |
+ void* buffer = sta->GetBuffer()->backing_store(); |
+ |
+ switch (typedarray->type()) { |
+#define TYPED_ARRAY_CASE(Type, typeName, TYPE, ctype, size) \ |
+ case kExternal##Type##Array: \ |
+ return CompareExchange<ctype>(isolate, buffer, index, oldobj, newobj); |
+ |
+ TYPED_ARRAYS(TYPED_ARRAY_CASE) |
+#undef TYPED_ARRAY_CASE |
+ |
+ default: |
+ break; |
+ } |
+ |
+ UNREACHABLE(); |
+ return isolate->heap()->undefined_value(); |
+} |
+ |
+ |
+RUNTIME_FUNCTION(Runtime_AtomicsLoad) { |
+ HandleScope scope(isolate); |
+ DCHECK(args.length() == 2); |
+ CONVERT_ARG_HANDLE_CHECKED(JSTypedArray, typedarray, 0); |
+ CONVERT_SIZE_ARG_CHECKED(index, 1); |
+ |
+ Handle<JSTypedArray> sta(JSTypedArray::cast(*typedarray)); |
+ size_t sta_length = NumberToSize(isolate, sta->length()); |
+ |
+ DCHECK(typedarray->is_shared()); |
+ DCHECK(index < sta_length); |
+ |
+ void* buffer = sta->GetBuffer()->backing_store(); |
+ |
+ switch (typedarray->type()) { |
+#define TYPED_ARRAY_CASE(Type, typeName, TYPE, ctype, size) \ |
+ case kExternal##Type##Array: \ |
+ return Load<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, typedarray, 0); |
+ CONVERT_SIZE_ARG_CHECKED(index, 1); |
+ CONVERT_NUMBER_ARG_HANDLE_CHECKED(value, 2); |
+ |
+ Handle<JSTypedArray> sta(JSTypedArray::cast(*typedarray)); |
+ size_t sta_length = NumberToSize(isolate, sta->length()); |
+ |
+ DCHECK(typedarray->is_shared()); |
+ DCHECK(index < sta_length); |
+ |
+ void* buffer = sta->GetBuffer()->backing_store(); |
+ |
+ switch (typedarray->type()) { |
+#define TYPED_ARRAY_CASE(Type, typeName, TYPE, ctype, size) \ |
+ case kExternal##Type##Array: \ |
+ return Store<ctype>(isolate, buffer, index, value); |
+ |
+ TYPED_ARRAYS(TYPED_ARRAY_CASE) |
+#undef TYPED_ARRAY_CASE |
+ |
+ default: |
+ break; |
+ } |
+ |
+ UNREACHABLE(); |
+ return isolate->heap()->undefined_value(); |
+} |
+ |
+ |
+RUNTIME_FUNCTION(Runtime_AtomicsAdd) { |
+ HandleScope scope(isolate); |
+ DCHECK(args.length() == 3); |
+ CONVERT_ARG_HANDLE_CHECKED(JSTypedArray, typedarray, 0); |
+ CONVERT_SIZE_ARG_CHECKED(index, 1); |
+ CONVERT_NUMBER_ARG_HANDLE_CHECKED(value, 2); |
+ |
+ Handle<JSTypedArray> sta(JSTypedArray::cast(*typedarray)); |
+ size_t sta_length = NumberToSize(isolate, sta->length()); |
+ |
+ DCHECK(typedarray->is_shared()); |
+ DCHECK(index < sta_length); |
+ |
+ void* buffer = sta->GetBuffer()->backing_store(); |
+ |
+ switch (typedarray->type()) { |
+#define TYPED_ARRAY_CASE(Type, typeName, TYPE, ctype, size) \ |
+ case kExternal##Type##Array: \ |
+ return Add<ctype>(isolate, buffer, index, value); |
+ |
+ INTEGER_TYPED_ARRAYS(TYPED_ARRAY_CASE) |
+#undef TYPED_ARRAY_CASE |
+ |
+ 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, typedarray, 0); |
+ CONVERT_SIZE_ARG_CHECKED(index, 1); |
+ CONVERT_NUMBER_ARG_HANDLE_CHECKED(value, 2); |
+ |
+ Handle<JSTypedArray> sta(JSTypedArray::cast(*typedarray)); |
+ size_t sta_length = NumberToSize(isolate, sta->length()); |
+ |
+ DCHECK(typedarray->is_shared()); |
+ DCHECK(index < sta_length); |
+ |
+ void* buffer = sta->GetBuffer()->backing_store(); |
+ |
+ switch (typedarray->type()) { |
+#define TYPED_ARRAY_CASE(Type, typeName, TYPE, ctype, size) \ |
+ case kExternal##Type##Array: \ |
+ return Sub<ctype>(isolate, buffer, index, value); |
+ |
+ INTEGER_TYPED_ARRAYS(TYPED_ARRAY_CASE) |
+#undef TYPED_ARRAY_CASE |
+ |
+ 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, typedarray, 0); |
+ CONVERT_SIZE_ARG_CHECKED(index, 1); |
+ CONVERT_NUMBER_ARG_HANDLE_CHECKED(value, 2); |
+ |
+ Handle<JSTypedArray> sta(JSTypedArray::cast(*typedarray)); |
+ size_t sta_length = NumberToSize(isolate, sta->length()); |
+ |
+ DCHECK(typedarray->is_shared()); |
+ DCHECK(index < sta_length); |
+ |
+ void* buffer = sta->GetBuffer()->backing_store(); |
+ |
+ switch (typedarray->type()) { |
+#define TYPED_ARRAY_CASE(Type, typeName, TYPE, ctype, size) \ |
+ case kExternal##Type##Array: \ |
+ return And<ctype>(isolate, buffer, index, value); |
+ |
+ INTEGER_TYPED_ARRAYS(TYPED_ARRAY_CASE) |
+#undef TYPED_ARRAY_CASE |
+ |
+ 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, typedarray, 0); |
+ CONVERT_SIZE_ARG_CHECKED(index, 1); |
+ CONVERT_NUMBER_ARG_HANDLE_CHECKED(value, 2); |
+ |
+ Handle<JSTypedArray> sta(JSTypedArray::cast(*typedarray)); |
+ size_t sta_length = NumberToSize(isolate, sta->length()); |
+ |
+ DCHECK(typedarray->is_shared()); |
+ DCHECK(index < sta_length); |
+ |
+ void* buffer = sta->GetBuffer()->backing_store(); |
+ |
+ switch (typedarray->type()) { |
+#define TYPED_ARRAY_CASE(Type, typeName, TYPE, ctype, size) \ |
+ case kExternal##Type##Array: \ |
+ return Or<ctype>(isolate, buffer, index, value); |
+ |
+ INTEGER_TYPED_ARRAYS(TYPED_ARRAY_CASE) |
+#undef TYPED_ARRAY_CASE |
+ |
+ 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, typedarray, 0); |
+ CONVERT_SIZE_ARG_CHECKED(index, 1); |
+ CONVERT_NUMBER_ARG_HANDLE_CHECKED(value, 2); |
+ |
+ Handle<JSTypedArray> sta(JSTypedArray::cast(*typedarray)); |
+ size_t sta_length = NumberToSize(isolate, sta->length()); |
+ |
+ DCHECK(typedarray->is_shared()); |
+ DCHECK(index < sta_length); |
+ |
+ void* buffer = sta->GetBuffer()->backing_store(); |
+ |
+ switch (typedarray->type()) { |
+#define TYPED_ARRAY_CASE(Type, typeName, TYPE, ctype, size) \ |
+ case kExternal##Type##Array: \ |
+ return Xor<ctype>(isolate, buffer, index, value); |
+ |
+ INTEGER_TYPED_ARRAYS(TYPED_ARRAY_CASE) |
+#undef TYPED_ARRAY_CASE |
+ |
+ 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, typedarray, 0); |
+ CONVERT_SIZE_ARG_CHECKED(index, 1); |
+ CONVERT_NUMBER_ARG_HANDLE_CHECKED(value, 1); |
+ DCHECK(typedarray->is_shared()); |
+ (void)index; |
+ (void)value; |
+ return isolate->heap()->undefined_value(); |
+} |
+ |
+ |
+RUNTIME_FUNCTION(Runtime_AtomicsIsLockFree) { |
+ return isolate->heap()->true_value(); |
+} |
+} |
+} // namespace v8::internal |