The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [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 | #ifndef ANDROID_CUTILS_ATOMIC_H |
| 18 | #define ANDROID_CUTILS_ATOMIC_H |
| 19 | |
| 20 | #include <stdint.h> |
| 21 | #include <sys/types.h> |
| 22 | |
| 23 | #ifdef __cplusplus |
| 24 | extern "C" { |
| 25 | #endif |
| 26 | |
| 27 | /* |
Andy McFadden | ac322da | 2010-05-19 22:33:28 -0700 | [diff] [blame^] | 28 | * Unless otherwise noted, the operations below perform a full fence before |
| 29 | * the atomic operation on SMP systems ("release" semantics). |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 30 | */ |
| 31 | |
| 32 | void android_atomic_write(int32_t value, volatile int32_t* addr); |
| 33 | |
| 34 | /* |
| 35 | * all these atomic operations return the previous value |
| 36 | */ |
| 37 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 38 | int32_t android_atomic_inc(volatile int32_t* addr); |
| 39 | int32_t android_atomic_dec(volatile int32_t* addr); |
| 40 | |
| 41 | int32_t android_atomic_add(int32_t value, volatile int32_t* addr); |
| 42 | int32_t android_atomic_and(int32_t value, volatile int32_t* addr); |
| 43 | int32_t android_atomic_or(int32_t value, volatile int32_t* addr); |
| 44 | |
| 45 | int32_t android_atomic_swap(int32_t value, volatile int32_t* addr); |
| 46 | |
| 47 | /* |
Andy McFadden | ac322da | 2010-05-19 22:33:28 -0700 | [diff] [blame^] | 48 | * cmpxchg returns zero if the new value was successfully written. This |
| 49 | * will only happen when *addr == oldvalue. |
| 50 | * |
| 51 | * (The return value is inverted from implementations on other platforms, but |
| 52 | * matches the ARM ldrex/strex sematics. Note also this is a compare-and-set |
| 53 | * operation, not a compare-and-exchange operation, since we don't return |
| 54 | * the original value.) |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 55 | */ |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 56 | int android_atomic_cmpxchg(int32_t oldvalue, int32_t newvalue, |
| 57 | volatile int32_t* addr); |
| 58 | |
Andy McFadden | ac322da | 2010-05-19 22:33:28 -0700 | [diff] [blame^] | 59 | /* |
| 60 | * Same basic operation as android_atomic_cmpxchg, but with "acquire" |
| 61 | * semantics. The memory barrier, if required, is performed after the |
| 62 | * new value is stored. Useful for acquiring a spin lock. |
| 63 | */ |
| 64 | int android_atomic_acquire_cmpxchg(int32_t oldvalue, int32_t newvalue, |
| 65 | volatile int32_t* addr); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 66 | |
Andy McFadden | ac322da | 2010-05-19 22:33:28 -0700 | [diff] [blame^] | 67 | /* |
| 68 | * Perform an atomic store with "release" semantics. The memory barrier, |
| 69 | * if required, is performed before the store instruction. Useful for |
| 70 | * releasing a spin lock. |
| 71 | */ |
| 72 | #define android_atomic_release_store android_atomic_write |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 73 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 74 | #ifdef __cplusplus |
| 75 | } // extern "C" |
| 76 | #endif |
| 77 | |
| 78 | #endif // ANDROID_CUTILS_ATOMIC_H |