Ashok Bhat | c15d2ce | 2013-11-13 16:20:49 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2014 The Android Open Source Project |
| 3 | * All rights reserved. |
| 4 | * |
| 5 | * Redistribution and use in source and binary forms, with or without |
| 6 | * modification, are permitted provided that the following conditions |
| 7 | * are met: |
| 8 | * * Redistributions of source code must retain the above copyright |
| 9 | * notice, this list of conditions and the following disclaimer. |
| 10 | * * Redistributions in binary form must reproduce the above copyright |
| 11 | * notice, this list of conditions and the following disclaimer in |
| 12 | * the documentation and/or other materials provided with the |
| 13 | * distribution. |
| 14 | * |
| 15 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 16 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 17 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS |
| 18 | * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE |
| 19 | * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 20 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, |
| 21 | * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS |
| 22 | * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED |
| 23 | * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
| 24 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT |
| 25 | * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 26 | * SUCH DAMAGE. |
| 27 | */ |
| 28 | |
| 29 | #ifndef ANDROID_CUTILS_ATOMIC_AARCH64_H |
| 30 | #define ANDROID_CUTILS_ATOMIC_AARCH64_H |
| 31 | |
| 32 | #include <stdint.h> |
| 33 | |
| 34 | #ifndef ANDROID_ATOMIC_INLINE |
| 35 | #define ANDROID_ATOMIC_INLINE inline __attribute__((always_inline)) |
| 36 | #endif |
| 37 | |
| 38 | /* |
| 39 | TODOAArch64: Revisit the below functions and check for potential |
| 40 | optimizations using assembly code or otherwise. |
| 41 | */ |
| 42 | |
| 43 | extern ANDROID_ATOMIC_INLINE |
| 44 | void android_compiler_barrier(void) |
| 45 | { |
| 46 | __asm__ __volatile__ ("" : : : "memory"); |
| 47 | } |
| 48 | |
Ashok Bhat | c15d2ce | 2013-11-13 16:20:49 +0000 | [diff] [blame] | 49 | extern ANDROID_ATOMIC_INLINE |
| 50 | void android_memory_barrier(void) |
| 51 | { |
| 52 | __asm__ __volatile__ ("dmb ish" : : : "memory"); |
| 53 | } |
| 54 | extern ANDROID_ATOMIC_INLINE |
| 55 | void android_memory_store_barrier(void) |
| 56 | { |
Colin Cross | 3510637 | 2014-01-22 19:04:28 -0800 | [diff] [blame] | 57 | __asm__ __volatile__ ("dmb ishst" : : : "memory"); |
Ashok Bhat | c15d2ce | 2013-11-13 16:20:49 +0000 | [diff] [blame] | 58 | } |
Ashok Bhat | c15d2ce | 2013-11-13 16:20:49 +0000 | [diff] [blame] | 59 | |
| 60 | extern ANDROID_ATOMIC_INLINE |
| 61 | int32_t android_atomic_acquire_load(volatile const int32_t *ptr) |
| 62 | { |
| 63 | int32_t value = *ptr; |
| 64 | android_memory_barrier(); |
| 65 | return value; |
| 66 | } |
| 67 | |
| 68 | extern ANDROID_ATOMIC_INLINE |
| 69 | int64_t android_atomic_acquire_load64(volatile const int64_t *ptr) |
| 70 | { |
| 71 | int64_t value = *ptr; |
| 72 | android_memory_barrier(); |
| 73 | return value; |
| 74 | } |
| 75 | |
| 76 | extern ANDROID_ATOMIC_INLINE |
| 77 | int32_t android_atomic_release_load(volatile const int32_t *ptr) |
| 78 | { |
| 79 | android_memory_barrier(); |
| 80 | return *ptr; |
| 81 | } |
| 82 | |
| 83 | extern ANDROID_ATOMIC_INLINE |
| 84 | int64_t android_atomic_release_load64(volatile const int64_t *ptr) |
| 85 | { |
| 86 | android_memory_barrier(); |
| 87 | return *ptr; |
| 88 | } |
| 89 | |
| 90 | extern ANDROID_ATOMIC_INLINE |
| 91 | void android_atomic_acquire_store(int32_t value, volatile int32_t *ptr) |
| 92 | { |
| 93 | *ptr = value; |
| 94 | android_memory_barrier(); |
| 95 | } |
| 96 | |
| 97 | extern ANDROID_ATOMIC_INLINE |
| 98 | void android_atomic_acquire_store64(int64_t value, volatile int64_t *ptr) |
| 99 | { |
| 100 | *ptr = value; |
| 101 | android_memory_barrier(); |
| 102 | } |
| 103 | |
| 104 | extern ANDROID_ATOMIC_INLINE |
| 105 | void android_atomic_release_store(int32_t value, volatile int32_t *ptr) |
| 106 | { |
| 107 | android_memory_barrier(); |
| 108 | *ptr = value; |
| 109 | } |
| 110 | |
| 111 | extern ANDROID_ATOMIC_INLINE |
| 112 | void android_atomic_release_store64(int64_t value, volatile int64_t *ptr) |
| 113 | { |
| 114 | android_memory_barrier(); |
| 115 | *ptr = value; |
| 116 | } |
| 117 | |
| 118 | extern ANDROID_ATOMIC_INLINE |
| 119 | int android_atomic_cas(int32_t old_value, int32_t new_value, |
| 120 | volatile int32_t *ptr) |
| 121 | { |
| 122 | return __sync_val_compare_and_swap(ptr, old_value, new_value) != old_value; |
| 123 | } |
| 124 | |
| 125 | extern ANDROID_ATOMIC_INLINE |
| 126 | int64_t android_atomic_cas64(int64_t old_value, int64_t new_value, |
| 127 | volatile int64_t *ptr) |
| 128 | { |
| 129 | return __sync_val_compare_and_swap(ptr, old_value, new_value) != old_value; |
| 130 | } |
| 131 | |
| 132 | extern ANDROID_ATOMIC_INLINE |
| 133 | int android_atomic_acquire_cas(int32_t old_value, int32_t new_value, |
| 134 | volatile int32_t *ptr) |
| 135 | { |
| 136 | int status = android_atomic_cas(old_value, new_value, ptr); |
| 137 | android_memory_barrier(); |
| 138 | return status; |
| 139 | } |
| 140 | |
| 141 | extern ANDROID_ATOMIC_INLINE |
| 142 | int64_t android_atomic_acquire_cas64(int64_t old_value, int64_t new_value, |
| 143 | volatile int64_t *ptr) |
| 144 | { |
| 145 | int status = android_atomic_cas64(old_value, new_value, ptr); |
| 146 | android_memory_barrier(); |
| 147 | return status; |
| 148 | } |
| 149 | |
| 150 | extern ANDROID_ATOMIC_INLINE |
| 151 | int android_atomic_release_cas(int32_t old_value, int32_t new_value, |
| 152 | volatile int32_t *ptr) |
| 153 | { |
| 154 | android_memory_barrier(); |
| 155 | return android_atomic_cas(old_value, new_value, ptr); |
| 156 | } |
| 157 | |
| 158 | extern ANDROID_ATOMIC_INLINE |
| 159 | int64_t android_atomic_release_cas64(int64_t old_value, int64_t new_value, |
| 160 | volatile int64_t *ptr) |
| 161 | { |
| 162 | android_memory_barrier(); |
| 163 | return android_atomic_cas64(old_value, new_value, ptr); |
| 164 | } |
| 165 | |
| 166 | extern ANDROID_ATOMIC_INLINE |
| 167 | int32_t android_atomic_add(int32_t increment, volatile int32_t *ptr) |
| 168 | { |
| 169 | int32_t prev, status; |
| 170 | android_memory_barrier(); |
| 171 | do { |
| 172 | prev = *ptr; |
| 173 | status = android_atomic_cas(prev, prev + increment, ptr); |
| 174 | } while (__builtin_expect(status != 0, 0)); |
| 175 | return prev; |
| 176 | } |
| 177 | |
| 178 | extern ANDROID_ATOMIC_INLINE |
| 179 | int32_t android_atomic_inc(volatile int32_t *addr) |
| 180 | { |
| 181 | return android_atomic_add(1, addr); |
| 182 | } |
| 183 | |
| 184 | extern ANDROID_ATOMIC_INLINE |
| 185 | int32_t android_atomic_dec(volatile int32_t *addr) |
| 186 | { |
| 187 | return android_atomic_add(-1, addr); |
| 188 | } |
| 189 | |
| 190 | extern ANDROID_ATOMIC_INLINE |
| 191 | int32_t android_atomic_and(int32_t value, volatile int32_t *ptr) |
| 192 | { |
| 193 | int32_t prev, status; |
| 194 | android_memory_barrier(); |
| 195 | do { |
| 196 | prev = *ptr; |
| 197 | status = android_atomic_cas(prev, prev & value, ptr); |
| 198 | } while (__builtin_expect(status != 0, 0)); |
| 199 | return prev; |
| 200 | } |
| 201 | |
| 202 | extern ANDROID_ATOMIC_INLINE |
| 203 | int32_t android_atomic_or(int32_t value, volatile int32_t *ptr) |
| 204 | { |
| 205 | int32_t prev, status; |
| 206 | android_memory_barrier(); |
| 207 | do { |
| 208 | prev = *ptr; |
| 209 | status = android_atomic_cas(prev, prev | value, ptr); |
| 210 | } while (__builtin_expect(status != 0, 0)); |
| 211 | return prev; |
| 212 | } |
| 213 | |
| 214 | #endif /* ANDROID_CUTILS_ATOMIC_AARCH64_H */ |