The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2007 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 | #include <cutils/atomic.h> |
Andy McFadden | ac322da | 2010-05-19 22:33:28 -0700 | [diff] [blame] | 18 | #include <cutils/atomic-inline.h> |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 19 | #ifdef HAVE_WIN32_THREADS |
| 20 | #include <windows.h> |
| 21 | #else |
| 22 | #include <sched.h> |
| 23 | #endif |
| 24 | |
| 25 | /*****************************************************************************/ |
| 26 | #if defined(HAVE_MACOSX_IPC) |
| 27 | |
| 28 | #include <libkern/OSAtomic.h> |
| 29 | |
Andy McFadden | 8dfa47d | 2010-05-27 10:10:18 -0700 | [diff] [blame^] | 30 | int32_t android_atomic_acquire_load(volatile int32_t* addr) { |
| 31 | int32_t value = *addr; |
| 32 | OSMemoryBarrier(); |
| 33 | return value; |
| 34 | } |
| 35 | |
| 36 | int32_t android_atomic_release_load(volatile int32_t* addr) { |
| 37 | OSMemoryBarrier(); |
| 38 | return *addr; |
| 39 | } |
| 40 | |
| 41 | void android_atomic_acquire_store(int32_t value, volatile int32_t* addr) { |
| 42 | *addr = value; |
| 43 | OSMemoryBarrier(); |
| 44 | } |
| 45 | |
| 46 | void android_atomic_release_store(int32_t value, volatile int32_t* addr) { |
| 47 | OSMemoryBarrier(); |
| 48 | *addr = value; |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | int32_t android_atomic_inc(volatile int32_t* addr) { |
| 52 | return OSAtomicIncrement32Barrier((int32_t*)addr)-1; |
| 53 | } |
| 54 | |
| 55 | int32_t android_atomic_dec(volatile int32_t* addr) { |
| 56 | return OSAtomicDecrement32Barrier((int32_t*)addr)+1; |
| 57 | } |
| 58 | |
| 59 | int32_t android_atomic_add(int32_t value, volatile int32_t* addr) { |
| 60 | return OSAtomicAdd32Barrier(value, (int32_t*)addr)-value; |
| 61 | } |
| 62 | |
| 63 | int32_t android_atomic_and(int32_t value, volatile int32_t* addr) { |
Andy McFadden | 8dfa47d | 2010-05-27 10:10:18 -0700 | [diff] [blame^] | 64 | return OSAtomicAnd32OrigBarrier(value, (int32_t*)addr); |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | int32_t android_atomic_or(int32_t value, volatile int32_t* addr) { |
Andy McFadden | 8dfa47d | 2010-05-27 10:10:18 -0700 | [diff] [blame^] | 68 | return OSAtomicOr32OrigBarrier(value, (int32_t*)addr); |
| 69 | } |
| 70 | |
| 71 | int32_t android_atomic_acquire_swap(int32_t value, volatile int32_t* addr) { |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 72 | int32_t oldValue; |
| 73 | do { |
| 74 | oldValue = *addr; |
Andy McFadden | 8dfa47d | 2010-05-27 10:10:18 -0700 | [diff] [blame^] | 75 | } while (android_atomic_acquire_cas(oldValue, value, addr)); |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 76 | return oldValue; |
| 77 | } |
| 78 | |
Andy McFadden | 8dfa47d | 2010-05-27 10:10:18 -0700 | [diff] [blame^] | 79 | int32_t android_atomic_release_swap(int32_t value, volatile int32_t* addr) { |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 80 | int32_t oldValue; |
| 81 | do { |
| 82 | oldValue = *addr; |
Andy McFadden | 8dfa47d | 2010-05-27 10:10:18 -0700 | [diff] [blame^] | 83 | } while (android_atomic_release_cas(oldValue, value, addr)); |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 84 | return oldValue; |
| 85 | } |
| 86 | |
Andy McFadden | 8dfa47d | 2010-05-27 10:10:18 -0700 | [diff] [blame^] | 87 | int android_atomic_release_cas(int32_t oldvalue, int32_t newvalue, volatile int32_t* addr) { |
Andy McFadden | ac322da | 2010-05-19 22:33:28 -0700 | [diff] [blame] | 88 | /* OS X CAS returns zero on failure; invert to return zero on success */ |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 89 | return OSAtomicCompareAndSwap32Barrier(oldvalue, newvalue, (int32_t*)addr) == 0; |
| 90 | } |
| 91 | |
Andy McFadden | 8dfa47d | 2010-05-27 10:10:18 -0700 | [diff] [blame^] | 92 | int android_atomic_acquire_cas(int32_t oldvalue, int32_t newvalue, |
Andy McFadden | ac322da | 2010-05-19 22:33:28 -0700 | [diff] [blame] | 93 | volatile int32_t* addr) { |
| 94 | int result = (OSAtomicCompareAndSwap32(oldvalue, newvalue, (int32_t*)addr) == 0); |
Andy McFadden | 8dfa47d | 2010-05-27 10:10:18 -0700 | [diff] [blame^] | 95 | if (result == 0) { |
Andy McFadden | ac322da | 2010-05-19 22:33:28 -0700 | [diff] [blame] | 96 | /* success, perform barrier */ |
| 97 | OSMemoryBarrier(); |
| 98 | } |
Andy McFadden | 8dfa47d | 2010-05-27 10:10:18 -0700 | [diff] [blame^] | 99 | return result; |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 100 | } |
| 101 | |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 102 | /*****************************************************************************/ |
| 103 | #elif defined(__i386__) || defined(__x86_64__) |
| 104 | |
Andy McFadden | 8dfa47d | 2010-05-27 10:10:18 -0700 | [diff] [blame^] | 105 | int32_t android_atomic_acquire_load(volatile int32_t* addr) { |
| 106 | int32_t value = *addr; |
| 107 | ANDROID_MEMBAR_FULL(); |
| 108 | return value; |
| 109 | } |
| 110 | |
| 111 | int32_t android_atomic_release_load(volatile int32_t* addr) { |
| 112 | ANDROID_MEMBAR_FULL(); |
| 113 | return *addr; |
| 114 | } |
| 115 | |
| 116 | void android_atomic_acquire_store(int32_t value, volatile int32_t* addr) { |
| 117 | *addr = value; |
| 118 | ANDROID_MEMBAR_FULL(); |
| 119 | } |
| 120 | |
| 121 | void android_atomic_release_store(int32_t value, volatile int32_t* addr) { |
| 122 | ANDROID_MEMBAR_FULL(); |
| 123 | *addr = value; |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | int32_t android_atomic_inc(volatile int32_t* addr) { |
Andy McFadden | 8dfa47d | 2010-05-27 10:10:18 -0700 | [diff] [blame^] | 127 | return android_atomic_add(1, addr); |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | int32_t android_atomic_dec(volatile int32_t* addr) { |
Andy McFadden | 8dfa47d | 2010-05-27 10:10:18 -0700 | [diff] [blame^] | 131 | return android_atomic_add(-1, addr); |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 132 | } |
| 133 | |
| 134 | int32_t android_atomic_add(int32_t value, volatile int32_t* addr) { |
| 135 | int32_t oldValue; |
| 136 | do { |
| 137 | oldValue = *addr; |
Andy McFadden | 8dfa47d | 2010-05-27 10:10:18 -0700 | [diff] [blame^] | 138 | } while (android_atomic_release_cas(oldValue, oldValue+value, addr)); |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 139 | return oldValue; |
| 140 | } |
| 141 | |
| 142 | int32_t android_atomic_and(int32_t value, volatile int32_t* addr) { |
| 143 | int32_t oldValue; |
| 144 | do { |
| 145 | oldValue = *addr; |
Andy McFadden | 8dfa47d | 2010-05-27 10:10:18 -0700 | [diff] [blame^] | 146 | } while (android_atomic_release_cas(oldValue, oldValue&value, addr)); |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 147 | return oldValue; |
| 148 | } |
| 149 | |
| 150 | int32_t android_atomic_or(int32_t value, volatile int32_t* addr) { |
| 151 | int32_t oldValue; |
| 152 | do { |
| 153 | oldValue = *addr; |
Andy McFadden | 8dfa47d | 2010-05-27 10:10:18 -0700 | [diff] [blame^] | 154 | } while (android_atomic_release_cas(oldValue, oldValue|value, addr)); |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 155 | return oldValue; |
| 156 | } |
| 157 | |
Andy McFadden | 8dfa47d | 2010-05-27 10:10:18 -0700 | [diff] [blame^] | 158 | /* returns 0 on successful swap */ |
| 159 | static inline int cas(int32_t oldvalue, int32_t newvalue, |
| 160 | volatile int32_t* addr) { |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 161 | int xchg; |
| 162 | asm volatile |
| 163 | ( |
| 164 | " lock; cmpxchg %%ecx, (%%edx);" |
| 165 | " setne %%al;" |
| 166 | " andl $1, %%eax" |
| 167 | : "=a" (xchg) |
| 168 | : "a" (oldvalue), "c" (newvalue), "d" (addr) |
| 169 | ); |
| 170 | return xchg; |
| 171 | } |
| 172 | |
Andy McFadden | 8dfa47d | 2010-05-27 10:10:18 -0700 | [diff] [blame^] | 173 | int32_t android_atomic_acquire_swap(int32_t value, volatile int32_t* addr) { |
| 174 | int32_t oldValue; |
| 175 | do { |
| 176 | oldValue = *addr; |
| 177 | } while (cas(oldValue, value, addr)); |
| 178 | ANDROID_MEMBAR_FULL(); |
| 179 | return oldValue; |
| 180 | } |
| 181 | |
| 182 | int32_t android_atomic_release_swap(int32_t value, volatile int32_t* addr) { |
| 183 | ANDROID_MEMBAR_FULL(); |
| 184 | int32_t oldValue; |
| 185 | do { |
| 186 | oldValue = *addr; |
| 187 | } while (cas(oldValue, value, addr)); |
| 188 | return oldValue; |
| 189 | } |
| 190 | |
| 191 | int android_atomic_acquire_cas(int32_t oldvalue, int32_t newvalue, |
Andy McFadden | ac322da | 2010-05-19 22:33:28 -0700 | [diff] [blame] | 192 | volatile int32_t* addr) { |
Andy McFadden | 8dfa47d | 2010-05-27 10:10:18 -0700 | [diff] [blame^] | 193 | int xchg = cas(oldvalue, newvalue, addr); |
| 194 | if (xchg == 0) |
| 195 | ANDROID_MEMBAR_FULL(); |
| 196 | return xchg; |
| 197 | } |
| 198 | |
| 199 | int android_atomic_release_cas(int32_t oldvalue, int32_t newvalue, |
| 200 | volatile int32_t* addr) { |
| 201 | ANDROID_MEMBAR_FULL(); |
| 202 | int xchg = cas(oldvalue, newvalue, addr); |
Andy McFadden | ac322da | 2010-05-19 22:33:28 -0700 | [diff] [blame] | 203 | return xchg; |
| 204 | } |
| 205 | |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 206 | |
| 207 | /*****************************************************************************/ |
| 208 | #elif __arm__ |
Andy McFadden | ac322da | 2010-05-19 22:33:28 -0700 | [diff] [blame] | 209 | // implementation for ARM is in atomic-android-arm.s. |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 210 | |
Shin-ichiro KAWASAKI | c6af911 | 2009-08-04 19:14:22 +0900 | [diff] [blame] | 211 | /*****************************************************************************/ |
| 212 | #elif __sh__ |
| 213 | // implementation for SuperH is in atomic-android-sh.c. |
| 214 | |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 215 | #else |
| 216 | |
| 217 | #error "Unsupported atomic operations for this platform" |
| 218 | |
| 219 | #endif |
| 220 | |