Duane Sand | 84c3e99 | 2014-04-29 11:19:26 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2010 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #ifndef ANDROID_CUTILS_ATOMIC_MIPS64_H |
| 18 | #define ANDROID_CUTILS_ATOMIC_MIPS64_H |
| 19 | |
| 20 | #include <stdint.h> |
| 21 | |
| 22 | #ifndef ANDROID_ATOMIC_INLINE |
| 23 | #define ANDROID_ATOMIC_INLINE inline __attribute__((always_inline)) |
| 24 | #endif |
| 25 | |
| 26 | extern ANDROID_ATOMIC_INLINE void android_compiler_barrier(void) |
| 27 | { |
| 28 | __asm__ __volatile__ ("" : : : "memory"); |
| 29 | } |
| 30 | |
| 31 | #if ANDROID_SMP == 0 |
| 32 | extern ANDROID_ATOMIC_INLINE void android_memory_barrier(void) |
| 33 | { |
| 34 | android_compiler_barrier(); |
| 35 | } |
| 36 | extern ANDROID_ATOMIC_INLINE void android_memory_store_barrier(void) |
| 37 | { |
| 38 | android_compiler_barrier(); |
| 39 | } |
| 40 | #else |
| 41 | extern ANDROID_ATOMIC_INLINE void android_memory_barrier(void) |
| 42 | { |
| 43 | __asm__ __volatile__ ("sync" : : : "memory"); |
| 44 | } |
| 45 | extern ANDROID_ATOMIC_INLINE void android_memory_store_barrier(void) |
| 46 | { |
| 47 | __asm__ __volatile__ ("sync" : : : "memory"); |
| 48 | } |
| 49 | #endif |
| 50 | |
| 51 | extern ANDROID_ATOMIC_INLINE |
| 52 | int32_t android_atomic_acquire_load(volatile const int32_t *ptr) |
| 53 | { |
| 54 | int32_t value = *ptr; |
| 55 | android_memory_barrier(); |
| 56 | return value; |
| 57 | } |
| 58 | |
| 59 | extern ANDROID_ATOMIC_INLINE |
| 60 | int64_t android_atomic_acquire_load64(volatile const int64_t *ptr) |
| 61 | { |
| 62 | int64_t value = *ptr; |
| 63 | android_memory_barrier(); |
| 64 | return value; |
| 65 | } |
| 66 | |
| 67 | extern ANDROID_ATOMIC_INLINE |
| 68 | int32_t android_atomic_release_load(volatile const int32_t *ptr) |
| 69 | { |
| 70 | android_memory_barrier(); |
| 71 | return *ptr; |
| 72 | } |
| 73 | |
| 74 | extern ANDROID_ATOMIC_INLINE |
| 75 | int64_t android_atomic_release_load64(volatile const int64_t *ptr) |
| 76 | { |
| 77 | android_memory_barrier(); |
| 78 | return *ptr; |
| 79 | } |
| 80 | |
| 81 | extern ANDROID_ATOMIC_INLINE |
| 82 | void android_atomic_acquire_store(int32_t value, volatile int32_t *ptr) |
| 83 | { |
| 84 | *ptr = value; |
| 85 | android_memory_barrier(); |
| 86 | } |
| 87 | |
| 88 | extern ANDROID_ATOMIC_INLINE |
| 89 | void android_atomic_acquire_store64(int64_t value, volatile int64_t *ptr) |
| 90 | { |
| 91 | *ptr = value; |
| 92 | android_memory_barrier(); |
| 93 | } |
| 94 | |
| 95 | extern ANDROID_ATOMIC_INLINE |
| 96 | void android_atomic_release_store(int32_t value, volatile int32_t *ptr) |
| 97 | { |
| 98 | android_memory_barrier(); |
| 99 | *ptr = value; |
| 100 | } |
| 101 | |
| 102 | extern ANDROID_ATOMIC_INLINE |
| 103 | void android_atomic_release_store64(int64_t value, volatile int64_t *ptr) |
| 104 | { |
| 105 | android_memory_barrier(); |
| 106 | *ptr = value; |
| 107 | } |
| 108 | |
| 109 | extern ANDROID_ATOMIC_INLINE |
| 110 | int android_atomic_cas(int32_t old_value, int32_t new_value, volatile int32_t *ptr) |
| 111 | { |
| 112 | int32_t prev, status; |
| 113 | do { |
| 114 | __asm__ __volatile__ ( |
| 115 | " ll %[prev], (%[ptr])\n" |
| 116 | " li %[status], 1\n" |
| 117 | " bne %[prev], %[old], 9f\n" |
| 118 | " move %[status], %[new_value]\n" |
| 119 | " sc %[status], (%[ptr])\n" |
| 120 | "9:\n" |
| 121 | : [prev] "=&r" (prev), [status] "=&r" (status) |
| 122 | : [ptr] "r" (ptr), [old] "r" (old_value), [new_value] "r" (new_value) |
| 123 | ); |
| 124 | } while (__builtin_expect(status == 0, 0)); |
| 125 | return prev != old_value; |
| 126 | } |
| 127 | |
| 128 | extern ANDROID_ATOMIC_INLINE |
| 129 | int64_t android_atomic_cas64(int64_t old_value, int64_t new_value, |
| 130 | volatile int64_t *ptr) |
| 131 | { |
| 132 | return __sync_val_compare_and_swap(ptr, old_value, new_value) != old_value; |
| 133 | } |
| 134 | |
| 135 | extern ANDROID_ATOMIC_INLINE |
| 136 | int android_atomic_acquire_cas(int32_t old_value, |
| 137 | int32_t new_value, |
| 138 | volatile int32_t *ptr) |
| 139 | { |
| 140 | int status = android_atomic_cas(old_value, new_value, ptr); |
| 141 | android_memory_barrier(); |
| 142 | return status; |
| 143 | } |
| 144 | |
| 145 | extern ANDROID_ATOMIC_INLINE |
| 146 | int64_t android_atomic_acquire_cas64(int64_t old_value, int64_t new_value, |
| 147 | volatile int64_t *ptr) |
| 148 | { |
| 149 | int status = android_atomic_cas64(old_value, new_value, ptr); |
| 150 | android_memory_barrier(); |
| 151 | return status; |
| 152 | } |
| 153 | |
| 154 | extern ANDROID_ATOMIC_INLINE |
| 155 | int android_atomic_release_cas(int32_t old_value, |
| 156 | int32_t new_value, |
| 157 | volatile int32_t *ptr) |
| 158 | { |
| 159 | android_memory_barrier(); |
| 160 | return android_atomic_cas(old_value, new_value, ptr); |
| 161 | } |
| 162 | |
| 163 | extern ANDROID_ATOMIC_INLINE |
| 164 | int64_t android_atomic_release_cas64(int64_t old_value, int64_t new_value, |
| 165 | volatile int64_t *ptr) |
| 166 | { |
| 167 | android_memory_barrier(); |
| 168 | return android_atomic_cas64(old_value, new_value, ptr); |
| 169 | } |
| 170 | |
| 171 | extern ANDROID_ATOMIC_INLINE |
Duane Sand | 84c3e99 | 2014-04-29 11:19:26 -0700 | [diff] [blame] | 172 | int32_t android_atomic_add(int32_t increment, volatile int32_t *ptr) |
| 173 | { |
| 174 | int32_t prev, status; |
| 175 | android_memory_barrier(); |
| 176 | do { |
| 177 | __asm__ __volatile__ ( |
| 178 | " ll %[prev], (%[ptr])\n" |
| 179 | " addu %[status], %[prev], %[inc]\n" |
| 180 | " sc %[status], (%[ptr])\n" |
| 181 | : [status] "=&r" (status), [prev] "=&r" (prev) |
| 182 | : [ptr] "r" (ptr), [inc] "Ir" (increment) |
| 183 | ); |
| 184 | } while (__builtin_expect(status == 0, 0)); |
| 185 | return prev; |
| 186 | } |
| 187 | |
| 188 | extern ANDROID_ATOMIC_INLINE int32_t |
| 189 | android_atomic_inc(volatile int32_t *addr) |
| 190 | { |
| 191 | return android_atomic_add(1, addr); |
| 192 | } |
| 193 | |
| 194 | extern ANDROID_ATOMIC_INLINE int32_t |
| 195 | android_atomic_dec(volatile int32_t *addr) |
| 196 | { |
| 197 | return android_atomic_add(-1, addr); |
| 198 | } |
| 199 | |
| 200 | extern ANDROID_ATOMIC_INLINE int32_t |
| 201 | android_atomic_and(int32_t value, volatile int32_t *ptr) |
| 202 | { |
| 203 | int32_t prev, status; |
| 204 | android_memory_barrier(); |
| 205 | do { |
| 206 | __asm__ __volatile__ ( |
| 207 | " ll %[prev], (%[ptr])\n" |
| 208 | " and %[status], %[prev], %[value]\n" |
| 209 | " sc %[status], (%[ptr])\n" |
| 210 | : [prev] "=&r" (prev), [status] "=&r" (status) |
| 211 | : [ptr] "r" (ptr), [value] "Ir" (value) |
| 212 | ); |
| 213 | } while (__builtin_expect(status == 0, 0)); |
| 214 | return prev; |
| 215 | } |
| 216 | |
| 217 | extern ANDROID_ATOMIC_INLINE int32_t |
| 218 | android_atomic_or(int32_t value, volatile int32_t *ptr) |
| 219 | { |
| 220 | int32_t prev, status; |
| 221 | android_memory_barrier(); |
| 222 | do { |
| 223 | __asm__ __volatile__ ( |
| 224 | " ll %[prev], (%[ptr])\n" |
| 225 | " or %[status], %[prev], %[value]\n" |
| 226 | " sc %[status], (%[ptr])\n" |
| 227 | : [prev] "=&r" (prev), [status] "=&r" (status) |
| 228 | : [ptr] "r" (ptr), [value] "Ir" (value) |
| 229 | ); |
| 230 | } while (__builtin_expect(status == 0, 0)); |
| 231 | return prev; |
| 232 | } |
| 233 | |
| 234 | #endif /* ANDROID_CUTILS_ATOMIC_MIPS_H */ |