OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2013 Google Inc. | 2 * Copyright 2013 Google Inc. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
6 */ | 6 */ |
7 | 7 |
8 #ifndef SkAtomics_android_DEFINED | 8 #ifndef SkAtomics_android_DEFINED |
9 #define SkAtomics_android_DEFINED | 9 #define SkAtomics_android_DEFINED |
10 | 10 |
11 /** Android framework atomics. */ | 11 /** Android framework atomics. */ |
12 | 12 |
13 #include <cutils/atomic.h> | 13 #include <cutils/atomic.h> |
14 #include <stdint.h> | 14 #include <stdint.h> |
15 | 15 |
16 #define sk_atomic_inc(addr) android_atomic_inc(addr) | 16 static inline __attribute__((always_inline)) int32_t sk_atomic_inc(int32_t* addr
) { |
17 #define sk_atomic_add(addr, inc) android_atomic_add(inc, addr) | 17 return android_atomic_inc(addr); |
18 #define sk_atomic_dec(addr) android_atomic_dec(addr) | 18 } |
| 19 |
| 20 static inline __attribute__((always_inline)) int32_t sk_atomic_add(int32_t* addr
, int32_t inc) { |
| 21 return android_atomic_add(inc, addr); |
| 22 } |
| 23 |
| 24 static inline __attribute__((always_inline)) int32_t sk_atomic_dec(int32_t* addr
) { |
| 25 return android_atomic_dec(addr); |
| 26 } |
19 | 27 |
20 static inline __attribute__((always_inline)) void sk_membar_acquire__after_atomi
c_dec() { | 28 static inline __attribute__((always_inline)) void sk_membar_acquire__after_atomi
c_dec() { |
21 //HACK: Android is actually using full memory barriers. | 29 //HACK: Android is actually using full memory barriers. |
22 // Should this change, uncomment below. | 30 // Should this change, uncomment below. |
23 //int dummy; | 31 //int dummy; |
24 //android_atomic_acquire_store(0, &dummy); | 32 //android_atomic_acquire_store(0, &dummy); |
25 } | 33 } |
26 | 34 |
27 static inline __attribute__((always_inline)) int32_t sk_atomic_conditional_inc(i
nt32_t* addr) { | 35 static inline __attribute__((always_inline)) int32_t sk_atomic_conditional_inc(i
nt32_t* addr) { |
28 while (true) { | 36 while (true) { |
29 int32_t value = *addr; | 37 int32_t value = *addr; |
30 if (value == 0) { | 38 if (value == 0) { |
31 return 0; | 39 return 0; |
32 } | 40 } |
33 if (0 == android_atomic_release_cas(value, value + 1, addr)) { | 41 if (0 == android_atomic_release_cas(value, value + 1, addr)) { |
34 return value; | 42 return value; |
35 } | 43 } |
36 } | 44 } |
37 } | 45 } |
38 | 46 |
39 static inline __attribute___((always_inline)) bool sk_atomic_cas(int32_t* addr, | 47 static inline __attribute__((always_inline)) bool sk_atomic_cas(int32_t* addr, |
40 int32_t before, | 48 int32_t before, |
41 int32_t after)
{ | 49 int32_t after)
{ |
42 // android_atomic_release_cas returns 0 for success (if *addr == before and
it wrote after). | 50 // android_atomic_release_cas returns 0 for success (if *addr == before and
it wrote after). |
43 return android_atomic_release_cas(before, after, addr) == 0; | 51 return android_atomic_release_cas(before, after, addr) == 0; |
44 } | 52 } |
45 | 53 |
46 static inline __attribute__((always_inline)) void sk_membar_acquire__after_atomi
c_conditional_inc() { | 54 static inline __attribute__((always_inline)) void sk_membar_acquire__after_atomi
c_conditional_inc() { |
47 //HACK: Android is actually using full memory barriers. | 55 //HACK: Android is actually using full memory barriers. |
48 // Should this change, uncomment below. | 56 // Should this change, uncomment below. |
49 //int dummy; | 57 //int dummy; |
50 //android_atomic_acquire_store(0, &dummy); | 58 //android_atomic_acquire_store(0, &dummy); |
51 } | 59 } |
52 | 60 |
53 #endif | 61 #endif |
OLD | NEW |