| OLD | NEW |
| 1 // Copyright 2015 the V8 project authors. All rights reserved. | 1 // Copyright 2015 the V8 project authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "src/runtime/runtime-utils.h" | 5 #include "src/runtime/runtime-utils.h" |
| 6 | 6 |
| 7 #include "src/arguments.h" | 7 #include "src/arguments.h" |
| 8 #include "src/base/macros.h" | 8 #include "src/base/macros.h" |
| 9 #include "src/base/platform/mutex.h" | 9 #include "src/base/platform/mutex.h" |
| 10 #include "src/conversions-inl.h" | 10 #include "src/conversions-inl.h" |
| 11 #include "src/factory.h" | 11 #include "src/factory.h" |
| 12 | 12 |
| 13 // Implement Atomic accesses to SharedArrayBuffers as defined in the | 13 // Implement Atomic accesses to SharedArrayBuffers as defined in the |
| 14 // SharedArrayBuffer draft spec, found here | 14 // SharedArrayBuffer draft spec, found here |
| 15 // https://github.com/tc39/ecmascript_sharedmem | 15 // https://github.com/tc39/ecmascript_sharedmem |
| 16 | 16 |
| 17 namespace v8 { | 17 namespace v8 { |
| 18 namespace internal { | 18 namespace internal { |
| 19 | 19 |
| 20 namespace { | 20 namespace { |
| 21 | 21 |
| 22 #if V8_CC_GNU | 22 #if V8_CC_GNU |
| 23 | 23 |
| 24 template <typename T> | 24 template <typename T> |
| 25 inline T ExchangeSeqCst(T* p, T value) { | 25 inline T ExchangeSeqCst(T* p, T value) { |
| 26 return __atomic_exchange_n(p, value, __ATOMIC_SEQ_CST); | 26 return __atomic_exchange_n(p, value, __ATOMIC_SEQ_CST); |
| 27 } | 27 } |
| 28 | 28 |
| 29 template <typename T> |
| 30 inline T CompareExchangeSeqCst(T* p, T oldval, T newval) { |
| 31 (void)__atomic_compare_exchange_n(p, &oldval, newval, 0, __ATOMIC_SEQ_CST, |
| 32 __ATOMIC_SEQ_CST); |
| 33 return oldval; |
| 34 } |
| 35 |
| 29 #elif V8_CC_MSVC | 36 #elif V8_CC_MSVC |
| 30 | 37 |
| 31 #define InterlockedExchange32 _InterlockedExchange | 38 #define InterlockedExchange32 _InterlockedExchange |
| 39 #define InterlockedCompareExchange32 _InterlockedCompareExchange |
| 40 #define InterlockedCompareExchange8 _InterlockedCompareExchange8 |
| 32 | 41 |
| 33 #define ATOMIC_OPS(type, suffix, vctype) \ | 42 #define ATOMIC_OPS(type, suffix, vctype) \ |
| 34 inline type ExchangeSeqCst(type* p, type value) { \ | 43 inline type ExchangeSeqCst(type* p, type value) { \ |
| 35 return InterlockedExchange##suffix(reinterpret_cast<vctype*>(p), \ | 44 return InterlockedExchange##suffix(reinterpret_cast<vctype*>(p), \ |
| 36 bit_cast<vctype>(value)); \ | 45 bit_cast<vctype>(value)); \ |
| 37 } \ | 46 } \ |
| 47 inline type CompareExchangeSeqCst(type* p, type oldval, type newval) { \ |
| 48 return InterlockedCompareExchange##suffix(reinterpret_cast<vctype*>(p), \ |
| 49 bit_cast<vctype>(newval), \ |
| 50 bit_cast<vctype>(oldval)); \ |
| 51 } |
| 38 | 52 |
| 39 ATOMIC_OPS(int8_t, 8, char) | 53 ATOMIC_OPS(int8_t, 8, char) |
| 40 ATOMIC_OPS(uint8_t, 8, char) | 54 ATOMIC_OPS(uint8_t, 8, char) |
| 41 ATOMIC_OPS(int16_t, 16, short) /* NOLINT(runtime/int) */ | 55 ATOMIC_OPS(int16_t, 16, short) /* NOLINT(runtime/int) */ |
| 42 ATOMIC_OPS(uint16_t, 16, short) /* NOLINT(runtime/int) */ | 56 ATOMIC_OPS(uint16_t, 16, short) /* NOLINT(runtime/int) */ |
| 43 ATOMIC_OPS(int32_t, 32, long) /* NOLINT(runtime/int) */ | 57 ATOMIC_OPS(int32_t, 32, long) /* NOLINT(runtime/int) */ |
| 44 ATOMIC_OPS(uint32_t, 32, long) /* NOLINT(runtime/int) */ | 58 ATOMIC_OPS(uint32_t, 32, long) /* NOLINT(runtime/int) */ |
| 45 | 59 |
| 46 #undef ATOMIC_OPS_INTEGER | 60 #undef ATOMIC_OPS_INTEGER |
| 47 #undef ATOMIC_OPS | 61 #undef ATOMIC_OPS |
| 48 | 62 |
| 49 #undef InterlockedExchange32 | 63 #undef InterlockedExchange32 |
| 64 #undef InterlockedCompareExchange32 |
| 65 #undef InterlockedCompareExchange8 |
| 50 | 66 |
| 51 #else | 67 #else |
| 52 | 68 |
| 53 #error Unsupported platform! | 69 #error Unsupported platform! |
| 54 | 70 |
| 55 #endif | 71 #endif |
| 56 | 72 |
| 57 template <typename T> | 73 template <typename T> |
| 58 T FromObject(Handle<Object> number); | 74 T FromObject(Handle<Object> number); |
| 59 | 75 |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 107 } | 123 } |
| 108 | 124 |
| 109 template <typename T> | 125 template <typename T> |
| 110 inline Object* DoExchange(Isolate* isolate, void* buffer, size_t index, | 126 inline Object* DoExchange(Isolate* isolate, void* buffer, size_t index, |
| 111 Handle<Object> obj) { | 127 Handle<Object> obj) { |
| 112 T value = FromObject<T>(obj); | 128 T value = FromObject<T>(obj); |
| 113 T result = ExchangeSeqCst(static_cast<T*>(buffer) + index, value); | 129 T result = ExchangeSeqCst(static_cast<T*>(buffer) + index, value); |
| 114 return ToObject(isolate, result); | 130 return ToObject(isolate, result); |
| 115 } | 131 } |
| 116 | 132 |
| 133 template <typename T> |
| 134 inline Object* DoCompareExchange(Isolate* isolate, void* buffer, size_t index, |
| 135 Handle<Object> oldobj, Handle<Object> newobj) { |
| 136 T oldval = FromObject<T>(oldobj); |
| 137 T newval = FromObject<T>(newobj); |
| 138 T result = |
| 139 CompareExchangeSeqCst(static_cast<T*>(buffer) + index, oldval, newval); |
| 140 return ToObject(isolate, result); |
| 141 } |
| 142 |
| 117 } // anonymous namespace | 143 } // anonymous namespace |
| 118 | 144 |
| 119 // Duplicated from objects.h | 145 // Duplicated from objects.h |
| 120 // V has parameters (Type, type, TYPE, C type, element_size) | 146 // V has parameters (Type, type, TYPE, C type, element_size) |
| 121 #define INTEGER_TYPED_ARRAYS(V) \ | 147 #define INTEGER_TYPED_ARRAYS(V) \ |
| 122 V(Uint8, uint8, UINT8, uint8_t, 1) \ | 148 V(Uint8, uint8, UINT8, uint8_t, 1) \ |
| 123 V(Int8, int8, INT8, int8_t, 1) \ | 149 V(Int8, int8, INT8, int8_t, 1) \ |
| 124 V(Uint16, uint16, UINT16, uint16_t, 2) \ | 150 V(Uint16, uint16, UINT16, uint16_t, 2) \ |
| 125 V(Int16, int16, INT16, int16_t, 2) \ | 151 V(Int16, int16, INT16, int16_t, 2) \ |
| 126 V(Uint32, uint32, UINT32, uint32_t, 4) \ | 152 V(Uint32, uint32, UINT32, uint32_t, 4) \ |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 171 #undef TYPED_ARRAY_CASE | 197 #undef TYPED_ARRAY_CASE |
| 172 | 198 |
| 173 default: | 199 default: |
| 174 break; | 200 break; |
| 175 } | 201 } |
| 176 | 202 |
| 177 UNREACHABLE(); | 203 UNREACHABLE(); |
| 178 return isolate->heap()->undefined_value(); | 204 return isolate->heap()->undefined_value(); |
| 179 } | 205 } |
| 180 | 206 |
| 207 RUNTIME_FUNCTION(Runtime_AtomicsCompareExchange) { |
| 208 HandleScope scope(isolate); |
| 209 DCHECK_EQ(4, args.length()); |
| 210 CONVERT_ARG_HANDLE_CHECKED(JSTypedArray, sta, 0); |
| 211 CONVERT_SIZE_ARG_CHECKED(index, 1); |
| 212 CONVERT_NUMBER_ARG_HANDLE_CHECKED(oldobj, 2); |
| 213 CONVERT_NUMBER_ARG_HANDLE_CHECKED(newobj, 3); |
| 214 CHECK(sta->GetBuffer()->is_shared()); |
| 215 CHECK_LT(index, NumberToSize(sta->length())); |
| 216 |
| 217 uint8_t* source = static_cast<uint8_t*>(sta->GetBuffer()->backing_store()) + |
| 218 NumberToSize(sta->byte_offset()); |
| 219 |
| 220 switch (sta->type()) { |
| 221 #define TYPED_ARRAY_CASE(Type, typeName, TYPE, ctype, size) \ |
| 222 case kExternal##Type##Array: \ |
| 223 return DoCompareExchange<ctype>(isolate, source, index, oldobj, newobj); |
| 224 |
| 225 INTEGER_TYPED_ARRAYS(TYPED_ARRAY_CASE) |
| 226 #undef TYPED_ARRAY_CASE |
| 227 |
| 228 default: |
| 229 break; |
| 230 } |
| 231 |
| 232 UNREACHABLE(); |
| 233 return isolate->heap()->undefined_value(); |
| 234 } |
| 181 | 235 |
| 182 } // namespace internal | 236 } // namespace internal |
| 183 } // namespace v8 | 237 } // namespace v8 |
| OLD | NEW |