OLD | NEW |
(Empty) | |
| 1 /* |
| 2 * Copyright 2013 Google Inc. |
| 3 * |
| 4 * Use of this source code is governed by a BSD-style license that can be |
| 5 * found in the LICENSE file. |
| 6 */ |
| 7 |
| 8 #ifndef SkAtomics_win_DEFINED |
| 9 #define SkAtomics_win_DEFINED |
| 10 |
| 11 /** Windows Interlocked atomics. */ |
| 12 |
| 13 #include "SkTypes.h" |
| 14 |
| 15 #include <intrin.h> |
| 16 |
| 17 //MSDN says in order to declare an interlocked function for use as an |
| 18 //intrinsic, include intrin.h and put the function in a #pragma intrinsic |
| 19 //directive. |
| 20 //The pragma appears to be unnecessary, but doesn't hurt. |
| 21 #pragma intrinsic(_InterlockedIncrement, _InterlockedExchangeAdd, _InterlockedDe
crement) |
| 22 #pragma intrinsic(_InterlockedCompareExchange) |
| 23 |
| 24 static inline int32_t sk_atomic_inc(int32_t* addr) { |
| 25 // InterlockedIncrement returns the new value, we want to return the old. |
| 26 return _InterlockedIncrement(reinterpret_cast<LONG*>(addr)) - 1; |
| 27 } |
| 28 |
| 29 static inline int32_t sk_atomic_add(int32_t* addr, int32_t inc) { |
| 30 return _InterlockedExchangeAdd(reinterpret_cast<LONG*>(addr), static_cast<LO
NG>(inc)); |
| 31 } |
| 32 |
| 33 static inline int32_t sk_atomic_dec(int32_t* addr) { |
| 34 // InterlockedDecrement returns the new value, we want to return the old. |
| 35 return _InterlockedDecrement(reinterpret_cast<LONG*>(addr)) + 1; |
| 36 } |
| 37 |
| 38 static inline void sk_membar_acquire__after_atomic_dec() { } |
| 39 |
| 40 static inline int32_t sk_atomic_conditional_inc(int32_t* addr) { |
| 41 LONG value = *addr; |
| 42 while (true) { |
| 43 if (value == 0) { |
| 44 return 0; |
| 45 } |
| 46 |
| 47 LONG before = _InterlockedCompareExchange(reinterpret_cast<LONG*>(addr),
value + 1, value); |
| 48 |
| 49 if (before == value) { |
| 50 return value; |
| 51 } else { |
| 52 value = before; |
| 53 } |
| 54 } |
| 55 } |
| 56 |
| 57 static inline void sk_membar_acquire__after_atomic_conditional_inc() { } |
| 58 |
| 59 #endif |
OLD | NEW |