michael@0: /* michael@0: * Copyright 2013 Google Inc. michael@0: * michael@0: * Use of this source code is governed by a BSD-style license that can be michael@0: * found in the LICENSE file. michael@0: */ michael@0: michael@0: #ifndef SkAtomics_android_DEFINED michael@0: #define SkAtomics_android_DEFINED michael@0: michael@0: /** Android framework atomics. */ michael@0: michael@0: #include michael@0: #include michael@0: michael@0: static inline __attribute__((always_inline)) int32_t sk_atomic_inc(int32_t* addr) { michael@0: return android_atomic_inc(addr); michael@0: } michael@0: michael@0: static inline __attribute__((always_inline)) int32_t sk_atomic_add(int32_t* addr, int32_t inc) { michael@0: return android_atomic_add(inc, addr); michael@0: } michael@0: michael@0: static inline __attribute__((always_inline)) int32_t sk_atomic_dec(int32_t* addr) { michael@0: return android_atomic_dec(addr); michael@0: } michael@0: michael@0: static inline __attribute__((always_inline)) void sk_membar_acquire__after_atomic_dec() { michael@0: //HACK: Android is actually using full memory barriers. michael@0: // Should this change, uncomment below. michael@0: //int dummy; michael@0: //android_atomic_acquire_store(0, &dummy); michael@0: } michael@0: michael@0: static inline __attribute__((always_inline)) int32_t sk_atomic_conditional_inc(int32_t* addr) { michael@0: while (true) { michael@0: int32_t value = *addr; michael@0: if (value == 0) { michael@0: return 0; michael@0: } michael@0: if (0 == android_atomic_release_cas(value, value + 1, addr)) { michael@0: return value; michael@0: } michael@0: } michael@0: } michael@0: michael@0: static inline __attribute__((always_inline)) bool sk_atomic_cas(int32_t* addr, michael@0: int32_t before, michael@0: int32_t after) { michael@0: // android_atomic_release_cas returns 0 for success (if *addr == before and it wrote after). michael@0: return android_atomic_release_cas(before, after, addr) == 0; michael@0: } michael@0: michael@0: static inline __attribute__((always_inline)) void sk_membar_acquire__after_atomic_conditional_inc() { michael@0: //HACK: Android is actually using full memory barriers. michael@0: // Should this change, uncomment below. michael@0: //int dummy; michael@0: //android_atomic_acquire_store(0, &dummy); michael@0: } michael@0: michael@0: #endif