1d9947f605a335363b0a0541d6d8cb7a7113ed788bungeman@google.com/* 2d9947f605a335363b0a0541d6d8cb7a7113ed788bungeman@google.com * Copyright 2013 Google Inc. 3d9947f605a335363b0a0541d6d8cb7a7113ed788bungeman@google.com * 4d9947f605a335363b0a0541d6d8cb7a7113ed788bungeman@google.com * Use of this source code is governed by a BSD-style license that can be 5d9947f605a335363b0a0541d6d8cb7a7113ed788bungeman@google.com * found in the LICENSE file. 6d9947f605a335363b0a0541d6d8cb7a7113ed788bungeman@google.com */ 7d9947f605a335363b0a0541d6d8cb7a7113ed788bungeman@google.com 8d9947f605a335363b0a0541d6d8cb7a7113ed788bungeman@google.com#ifndef SkAtomics_sync_DEFINED 9d9947f605a335363b0a0541d6d8cb7a7113ed788bungeman@google.com#define SkAtomics_sync_DEFINED 10d9947f605a335363b0a0541d6d8cb7a7113ed788bungeman@google.com 11d9947f605a335363b0a0541d6d8cb7a7113ed788bungeman@google.com/** GCC/Clang __sync based atomics. */ 12d9947f605a335363b0a0541d6d8cb7a7113ed788bungeman@google.com 13d9947f605a335363b0a0541d6d8cb7a7113ed788bungeman@google.com#include <stdint.h> 14d9947f605a335363b0a0541d6d8cb7a7113ed788bungeman@google.com 15d9947f605a335363b0a0541d6d8cb7a7113ed788bungeman@google.comstatic inline __attribute__((always_inline)) int32_t sk_atomic_inc(int32_t* addr) { 16d9947f605a335363b0a0541d6d8cb7a7113ed788bungeman@google.com return __sync_fetch_and_add(addr, 1); 17d9947f605a335363b0a0541d6d8cb7a7113ed788bungeman@google.com} 18d9947f605a335363b0a0541d6d8cb7a7113ed788bungeman@google.com 1900a8fae0cee239181c9e4fc7775b01b661c72f5ebsalomonstatic inline __attribute__((always_inline)) int64_t sk_atomic_inc(int64_t* addr) { 202d75d0865c7bac54bf5e234855609d0f628388b7Ben Murdoch#if defined(__mips__) && !defined(__LP64__) && !defined(__GCC_HAVE_SYNC_COMPARE_AND_SWAP_8) 212d75d0865c7bac54bf5e234855609d0f628388b7Ben Murdoch /** Some versions of the GCC 32-bit MIPS toolchains (e.g. 4.8) for android are missing 222d75d0865c7bac54bf5e234855609d0f628388b7Ben Murdoch * support for the __sync* functions that operate on 64-bit values. The workaround 232d75d0865c7bac54bf5e234855609d0f628388b7Ben Murdoch * is to use __atomic* functions until we can move everything to <stdatomic.h>. 24eccbfec18abf5011a0f6db9d247b26f035af85f2Derek Sollenberger */ 25eccbfec18abf5011a0f6db9d247b26f035af85f2Derek Sollenberger return __atomic_fetch_add(addr, 1, __ATOMIC_SEQ_CST); 26eccbfec18abf5011a0f6db9d247b26f035af85f2Derek Sollenberger#else 2700a8fae0cee239181c9e4fc7775b01b661c72f5ebsalomon return __sync_fetch_and_add(addr, 1); 28eccbfec18abf5011a0f6db9d247b26f035af85f2Derek Sollenberger#endif 2900a8fae0cee239181c9e4fc7775b01b661c72f5ebsalomon} 3000a8fae0cee239181c9e4fc7775b01b661c72f5ebsalomon 31d9947f605a335363b0a0541d6d8cb7a7113ed788bungeman@google.comstatic inline __attribute__((always_inline)) int32_t sk_atomic_add(int32_t* addr, int32_t inc) { 32d9947f605a335363b0a0541d6d8cb7a7113ed788bungeman@google.com return __sync_fetch_and_add(addr, inc); 33d9947f605a335363b0a0541d6d8cb7a7113ed788bungeman@google.com} 34d9947f605a335363b0a0541d6d8cb7a7113ed788bungeman@google.com 35d9947f605a335363b0a0541d6d8cb7a7113ed788bungeman@google.comstatic inline __attribute__((always_inline)) int32_t sk_atomic_dec(int32_t* addr) { 36d9947f605a335363b0a0541d6d8cb7a7113ed788bungeman@google.com return __sync_fetch_and_add(addr, -1); 37d9947f605a335363b0a0541d6d8cb7a7113ed788bungeman@google.com} 38d9947f605a335363b0a0541d6d8cb7a7113ed788bungeman@google.com 39d9947f605a335363b0a0541d6d8cb7a7113ed788bungeman@google.comstatic inline __attribute__((always_inline)) void sk_membar_acquire__after_atomic_dec() { } 40d9947f605a335363b0a0541d6d8cb7a7113ed788bungeman@google.com 414b5fba5a3cc29058088a9a62df1da83e1a3c7db0commit-bot@chromium.orgstatic inline __attribute__((always_inline)) bool sk_atomic_cas(int32_t* addr, 424b5fba5a3cc29058088a9a62df1da83e1a3c7db0commit-bot@chromium.org int32_t before, 434b5fba5a3cc29058088a9a62df1da83e1a3c7db0commit-bot@chromium.org int32_t after) { 444b5fba5a3cc29058088a9a62df1da83e1a3c7db0commit-bot@chromium.org return __sync_bool_compare_and_swap(addr, before, after); 454b5fba5a3cc29058088a9a62df1da83e1a3c7db0commit-bot@chromium.org} 464b5fba5a3cc29058088a9a62df1da83e1a3c7db0commit-bot@chromium.org 4781496fb21637cc8d2a2b45a790e0f9d6d6f769c4commit-bot@chromium.orgstatic inline __attribute__((always_inline)) void* sk_atomic_cas(void** addr, 4881496fb21637cc8d2a2b45a790e0f9d6d6f769c4commit-bot@chromium.org void* before, 4981496fb21637cc8d2a2b45a790e0f9d6d6f769c4commit-bot@chromium.org void* after) { 5081496fb21637cc8d2a2b45a790e0f9d6d6f769c4commit-bot@chromium.org return __sync_val_compare_and_swap(addr, before, after); 5181496fb21637cc8d2a2b45a790e0f9d6d6f769c4commit-bot@chromium.org} 5281496fb21637cc8d2a2b45a790e0f9d6d6f769c4commit-bot@chromium.org 53d9947f605a335363b0a0541d6d8cb7a7113ed788bungeman@google.comstatic inline __attribute__((always_inline)) void sk_membar_acquire__after_atomic_conditional_inc() { } 54d9947f605a335363b0a0541d6d8cb7a7113ed788bungeman@google.com 55d9947f605a335363b0a0541d6d8cb7a7113ed788bungeman@google.com#endif 56