| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright (C) 2007, 2008, 2010, 2012 Apple Inc. All rights reserved. | 2 * Copyright (C) 2007, 2008, 2010, 2012 Apple Inc. All rights reserved. |
| 3 * Copyright (C) 2007 Justin Haygood (jhaygood@reaktix.com) | 3 * Copyright (C) 2007 Justin Haygood (jhaygood@reaktix.com) |
| 4 * | 4 * |
| 5 * Redistribution and use in source and binary forms, with or without | 5 * Redistribution and use in source and binary forms, with or without |
| 6 * modification, are permitted provided that the following conditions | 6 * modification, are permitted provided that the following conditions |
| 7 * are met: | 7 * are met: |
| 8 * | 8 * |
| 9 * 1. Redistributions of source code must retain the above copyright | 9 * 1. Redistributions of source code must retain the above copyright |
| 10 * notice, this list of conditions and the following disclaimer. | 10 * notice, this list of conditions and the following disclaimer. |
| (...skipping 17 matching lines...) Expand all Loading... |
| 28 */ | 28 */ |
| 29 | 29 |
| 30 #ifndef Atomics_h | 30 #ifndef Atomics_h |
| 31 #define Atomics_h | 31 #define Atomics_h |
| 32 | 32 |
| 33 #include "wtf/Assertions.h" | 33 #include "wtf/Assertions.h" |
| 34 #include "wtf/CPU.h" | 34 #include "wtf/CPU.h" |
| 35 | 35 |
| 36 #include <stdint.h> | 36 #include <stdint.h> |
| 37 | 37 |
| 38 #if COMPILER(MSVC) |
| 39 #include <windows.h> |
| 40 #endif |
| 41 |
| 38 #if defined(THREAD_SANITIZER) | 42 #if defined(THREAD_SANITIZER) |
| 39 #include <sanitizer/tsan_interface_atomic.h> | 43 #include <sanitizer/tsan_interface_atomic.h> |
| 40 #endif | 44 #endif |
| 41 | 45 |
| 42 namespace WTF { | 46 namespace WTF { |
| 43 | 47 |
| 48 #if COMPILER(MSVC) |
| 49 |
| 50 // atomicAdd returns the result of the addition. |
| 51 ALWAYS_INLINE int atomicAdd(int volatile* addend, int increment) |
| 52 { |
| 53 return InterlockedExchangeAdd(reinterpret_cast<long volatile*>(addend), stat
ic_cast<long>(increment)) + increment; |
| 54 } |
| 55 |
| 56 // atomicSubtract returns the result of the subtraction. |
| 57 ALWAYS_INLINE int atomicSubtract(int volatile* addend, int decrement) |
| 58 { |
| 59 return InterlockedExchangeAdd(reinterpret_cast<long volatile*>(addend), stat
ic_cast<long>(-decrement)) - decrement; |
| 60 } |
| 61 |
| 62 ALWAYS_INLINE int atomicIncrement(int volatile* addend) { return InterlockedIncr
ement(reinterpret_cast<long volatile*>(addend)); } |
| 63 ALWAYS_INLINE int atomicDecrement(int volatile* addend) { return InterlockedDecr
ement(reinterpret_cast<long volatile*>(addend)); } |
| 64 |
| 65 ALWAYS_INLINE int64_t atomicIncrement(int64_t volatile* addend) { return Interlo
ckedIncrement64(reinterpret_cast<long long volatile*>(addend)); } |
| 66 ALWAYS_INLINE int64_t atomicDecrement(int64_t volatile* addend) { return Interlo
ckedDecrement64(reinterpret_cast<long long volatile*>(addend)); } |
| 67 |
| 68 ALWAYS_INLINE int atomicTestAndSetToOne(int volatile* ptr) |
| 69 { |
| 70 int ret = InterlockedExchange(reinterpret_cast<long volatile*>(ptr), 1); |
| 71 ASSERT(!ret || ret == 1); |
| 72 return ret; |
| 73 } |
| 74 |
| 75 ALWAYS_INLINE void atomicSetOneToZero(int volatile* ptr) |
| 76 { |
| 77 ASSERT(*ptr == 1); |
| 78 InterlockedExchange(reinterpret_cast<long volatile*>(ptr), 0); |
| 79 } |
| 80 |
| 81 #else |
| 82 |
| 44 // atomicAdd returns the result of the addition. | 83 // atomicAdd returns the result of the addition. |
| 45 ALWAYS_INLINE int atomicAdd(int volatile* addend, int increment) { return __sync
_add_and_fetch(addend, increment); } | 84 ALWAYS_INLINE int atomicAdd(int volatile* addend, int increment) { return __sync
_add_and_fetch(addend, increment); } |
| 46 // atomicSubtract returns the result of the subtraction. | 85 // atomicSubtract returns the result of the subtraction. |
| 47 ALWAYS_INLINE int atomicSubtract(int volatile* addend, int decrement) { return _
_sync_sub_and_fetch(addend, decrement); } | 86 ALWAYS_INLINE int atomicSubtract(int volatile* addend, int decrement) { return _
_sync_sub_and_fetch(addend, decrement); } |
| 48 | 87 |
| 49 ALWAYS_INLINE int atomicIncrement(int volatile* addend) { return atomicAdd(adden
d, 1); } | 88 ALWAYS_INLINE int atomicIncrement(int volatile* addend) { return atomicAdd(adden
d, 1); } |
| 50 ALWAYS_INLINE int atomicDecrement(int volatile* addend) { return atomicSubtract(
addend, 1); } | 89 ALWAYS_INLINE int atomicDecrement(int volatile* addend) { return atomicSubtract(
addend, 1); } |
| 51 | 90 |
| 52 ALWAYS_INLINE int64_t atomicIncrement(int64_t volatile* addend) { return __sync_
add_and_fetch(addend, 1); } | 91 ALWAYS_INLINE int64_t atomicIncrement(int64_t volatile* addend) { return __sync_
add_and_fetch(addend, 1); } |
| 53 ALWAYS_INLINE int64_t atomicDecrement(int64_t volatile* addend) { return __sync_
sub_and_fetch(addend, 1); } | 92 ALWAYS_INLINE int64_t atomicDecrement(int64_t volatile* addend) { return __sync_
sub_and_fetch(addend, 1); } |
| 54 | 93 |
| 55 ALWAYS_INLINE int atomicTestAndSetToOne(int volatile* ptr) | 94 ALWAYS_INLINE int atomicTestAndSetToOne(int volatile* ptr) |
| 56 { | 95 { |
| 57 int ret = __sync_lock_test_and_set(ptr, 1); | 96 int ret = __sync_lock_test_and_set(ptr, 1); |
| 58 ASSERT(!ret || ret == 1); | 97 ASSERT(!ret || ret == 1); |
| 59 return ret; | 98 return ret; |
| 60 } | 99 } |
| 61 | 100 |
| 62 ALWAYS_INLINE void atomicSetOneToZero(int volatile* ptr) | 101 ALWAYS_INLINE void atomicSetOneToZero(int volatile* ptr) |
| 63 { | 102 { |
| 64 ASSERT(*ptr == 1); | 103 ASSERT(*ptr == 1); |
| 65 __sync_lock_release(ptr); | 104 __sync_lock_release(ptr); |
| 66 } | 105 } |
| 106 #endif |
| 67 | 107 |
| 68 #if defined(THREAD_SANITIZER) | 108 #if defined(THREAD_SANITIZER) |
| 69 ALWAYS_INLINE void releaseStore(volatile int* ptr, int value) | 109 ALWAYS_INLINE void releaseStore(volatile int* ptr, int value) |
| 70 { | 110 { |
| 71 __tsan_atomic32_store(ptr, value, __tsan_memory_order_release); | 111 __tsan_atomic32_store(ptr, value, __tsan_memory_order_release); |
| 72 } | 112 } |
| 73 | 113 |
| 74 ALWAYS_INLINE int acquireLoad(volatile const int* ptr) | 114 ALWAYS_INLINE int acquireLoad(volatile const int* ptr) |
| 75 { | 115 { |
| 76 return __tsan_atomic32_load(ptr, __tsan_memory_order_acquire); | 116 return __tsan_atomic32_load(ptr, __tsan_memory_order_acquire); |
| 77 } | 117 } |
| 78 | 118 |
| 79 ALWAYS_INLINE void releaseStore(volatile unsigned* ptr, unsigned value) | 119 ALWAYS_INLINE void releaseStore(volatile unsigned* ptr, unsigned value) |
| 80 { | 120 { |
| 81 __tsan_atomic32_store(reinterpret_cast<volatile int*>(ptr), static_cast<int>
(value), __tsan_memory_order_release); | 121 __tsan_atomic32_store(reinterpret_cast<volatile int*>(ptr), static_cast<int>
(value), __tsan_memory_order_release); |
| 82 } | 122 } |
| 83 | 123 |
| 84 ALWAYS_INLINE unsigned acquireLoad(volatile const unsigned* ptr) | 124 ALWAYS_INLINE unsigned acquireLoad(volatile const unsigned* ptr) |
| 85 { | 125 { |
| 86 return static_cast<unsigned>(__tsan_atomic32_load(reinterpret_cast<volatile
const int*>(ptr), __tsan_memory_order_acquire)); | 126 return static_cast<unsigned>(__tsan_atomic32_load(reinterpret_cast<volatile
const int*>(ptr), __tsan_memory_order_acquire)); |
| 87 } | 127 } |
| 88 #else | 128 #else |
| 89 | 129 |
| 90 #if CPU(X86) || CPU(X86_64) | 130 #if CPU(X86) || CPU(X86_64) |
| 91 // Only compiler barrier is needed. | 131 // Only compiler barrier is needed. |
| 132 #if COMPILER(MSVC) |
| 133 // Starting from Visual Studio 2005 compiler guarantees acquire and release |
| 134 // semantics for operations on volatile variables. See MSDN entry for |
| 135 // MemoryBarrier macro. |
| 136 #define MEMORY_BARRIER() |
| 137 #else |
| 92 #define MEMORY_BARRIER() __asm__ __volatile__("" : : : "memory") | 138 #define MEMORY_BARRIER() __asm__ __volatile__("" : : : "memory") |
| 139 #endif |
| 93 #elif CPU(ARM) && (OS(LINUX) || OS(ANDROID)) | 140 #elif CPU(ARM) && (OS(LINUX) || OS(ANDROID)) |
| 94 // On ARM __sync_synchronize generates dmb which is very expensive on single | 141 // On ARM __sync_synchronize generates dmb which is very expensive on single |
| 95 // core devices which don't actually need it. Avoid the cost by calling into | 142 // core devices which don't actually need it. Avoid the cost by calling into |
| 96 // kuser_memory_barrier helper. | 143 // kuser_memory_barrier helper. |
| 97 inline void memoryBarrier() | 144 inline void memoryBarrier() |
| 98 { | 145 { |
| 99 // Note: This is a function call, which is also an implicit compiler barrier
. | 146 // Note: This is a function call, which is also an implicit compiler barrier
. |
| 100 typedef void (*KernelMemoryBarrierFunc)(); | 147 typedef void (*KernelMemoryBarrierFunc)(); |
| 101 ((KernelMemoryBarrierFunc)0xffff0fa0)(); | 148 ((KernelMemoryBarrierFunc)0xffff0fa0)(); |
| 102 } | 149 } |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 141 using WTF::atomicAdd; | 188 using WTF::atomicAdd; |
| 142 using WTF::atomicSubtract; | 189 using WTF::atomicSubtract; |
| 143 using WTF::atomicDecrement; | 190 using WTF::atomicDecrement; |
| 144 using WTF::atomicIncrement; | 191 using WTF::atomicIncrement; |
| 145 using WTF::atomicTestAndSetToOne; | 192 using WTF::atomicTestAndSetToOne; |
| 146 using WTF::atomicSetOneToZero; | 193 using WTF::atomicSetOneToZero; |
| 147 using WTF::acquireLoad; | 194 using WTF::acquireLoad; |
| 148 using WTF::releaseStore; | 195 using WTF::releaseStore; |
| 149 | 196 |
| 150 #endif // Atomics_h | 197 #endif // Atomics_h |
| OLD | NEW |