1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/gfx/skia/trunk/src/ports/SkAtomics_win.h Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,62 @@ 1.4 +/* 1.5 + * Copyright 2013 Google Inc. 1.6 + * 1.7 + * Use of this source code is governed by a BSD-style license that can be 1.8 + * found in the LICENSE file. 1.9 + */ 1.10 + 1.11 +#ifndef SkAtomics_win_DEFINED 1.12 +#define SkAtomics_win_DEFINED 1.13 + 1.14 +/** Windows Interlocked atomics. */ 1.15 + 1.16 +#include <intrin.h> 1.17 +#include <stdint.h> 1.18 + 1.19 +//MSDN says in order to declare an interlocked function for use as an 1.20 +//intrinsic, include intrin.h and put the function in a #pragma intrinsic 1.21 +//directive. 1.22 +//The pragma appears to be unnecessary, but doesn't hurt. 1.23 +#pragma intrinsic(_InterlockedIncrement, _InterlockedExchangeAdd, _InterlockedDecrement) 1.24 +#pragma intrinsic(_InterlockedCompareExchange) 1.25 + 1.26 +static inline int32_t sk_atomic_inc(int32_t* addr) { 1.27 + // InterlockedIncrement returns the new value, we want to return the old. 1.28 + return _InterlockedIncrement(reinterpret_cast<long*>(addr)) - 1; 1.29 +} 1.30 + 1.31 +static inline int32_t sk_atomic_add(int32_t* addr, int32_t inc) { 1.32 + return _InterlockedExchangeAdd(reinterpret_cast<long*>(addr), static_cast<long>(inc)); 1.33 +} 1.34 + 1.35 +static inline int32_t sk_atomic_dec(int32_t* addr) { 1.36 + // InterlockedDecrement returns the new value, we want to return the old. 1.37 + return _InterlockedDecrement(reinterpret_cast<long*>(addr)) + 1; 1.38 +} 1.39 + 1.40 +static inline void sk_membar_acquire__after_atomic_dec() { } 1.41 + 1.42 +static inline int32_t sk_atomic_conditional_inc(int32_t* addr) { 1.43 + long value = *addr; 1.44 + while (true) { 1.45 + if (value == 0) { 1.46 + return 0; 1.47 + } 1.48 + 1.49 + long before = _InterlockedCompareExchange(reinterpret_cast<long*>(addr), value + 1, value); 1.50 + 1.51 + if (before == value) { 1.52 + return value; 1.53 + } else { 1.54 + value = before; 1.55 + } 1.56 + } 1.57 +} 1.58 + 1.59 +static inline bool sk_atomic_cas(int32_t* addr, int32_t before, int32_t after) { 1.60 + return _InterlockedCompareExchange(reinterpret_cast<long*>(addr), after, before) == before; 1.61 +} 1.62 + 1.63 +static inline void sk_membar_acquire__after_atomic_conditional_inc() { } 1.64 + 1.65 +#endif