| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2008 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 | */ |
| Andy McFadden | 6a87708 | 2010-05-19 22:36:33 -0700 | [diff] [blame] | 16 | |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 17 | /* |
| 18 | * Atomic operations |
| 19 | */ |
| 20 | #ifndef _DALVIK_ATOMIC |
| 21 | #define _DALVIK_ATOMIC |
| 22 | |
| Andy McFadden | 6a87708 | 2010-05-19 22:36:33 -0700 | [diff] [blame] | 23 | #include <cutils/atomic.h> /* use common Android atomic ops */ |
| 24 | #include <cutils/atomic-inline.h> /* and some uncommon ones */ |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 25 | |
| 26 | /* |
| Andy McFadden | 6a87708 | 2010-05-19 22:36:33 -0700 | [diff] [blame] | 27 | * Full memory barrier. Ensures compiler ordering and SMP behavior. |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 28 | */ |
| Andy McFadden | fb07cb6 | 2010-05-27 10:23:41 -0700 | [diff] [blame] | 29 | #define MEM_BARRIER() ANDROID_MEMBAR_FULL() |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 30 | |
| 31 | /* |
| Andy McFadden | 6a87708 | 2010-05-19 22:36:33 -0700 | [diff] [blame] | 32 | * 32-bit atomic compare-and-swap macro. Performs a memory barrier |
| 33 | * before the swap (store-release). |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 34 | * |
| Andy McFadden | fb07cb6 | 2010-05-27 10:23:41 -0700 | [diff] [blame] | 35 | * If *_addr equals "_old", replace it with "_new" and return nonzero |
| 36 | * (i.e. returns "false" if the operation fails). |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 37 | * |
| 38 | * Underlying function is currently declared: |
| Andy McFadden | fb07cb6 | 2010-05-27 10:23:41 -0700 | [diff] [blame] | 39 | * int release_cas(int32_t old, int32_t new, volatile int32_t* addr) |
| 40 | * |
| 41 | * TODO: rename macro to ATOMIC_RELEASE_CAS |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 42 | */ |
| 43 | #define ATOMIC_CMP_SWAP(_addr, _old, _new) \ |
| Andy McFadden | fb07cb6 | 2010-05-27 10:23:41 -0700 | [diff] [blame] | 44 | (android_atomic_release_cas((_old), (_new), (_addr)) == 0) |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 45 | |
| Andy McFadden | 6a87708 | 2010-05-19 22:36:33 -0700 | [diff] [blame] | 46 | |
| 47 | /* |
| 48 | * NOTE: Two "quasiatomic" operations on the exact same memory address |
| 49 | * are guaranteed to operate atomically with respect to each other, |
| 50 | * but no guarantees are made about quasiatomic operations mixed with |
| 51 | * non-quasiatomic operations on the same address, nor about |
| 52 | * quasiatomic operations that are performed on partially-overlapping |
| 53 | * memory. |
| 54 | */ |
| 55 | |
| 56 | /* |
| 57 | * TODO: rename android_quasiatomic_* to dvmQuasiatomic*. Don't want to do |
| 58 | * that yet due to branch merge issues. |
| 59 | */ |
| 60 | int64_t android_quasiatomic_swap_64(int64_t value, volatile int64_t* addr); |
| 61 | int64_t android_quasiatomic_read_64(volatile int64_t* addr); |
| 62 | int android_quasiatomic_cmpxchg_64(int64_t oldvalue, int64_t newvalue, |
| 63 | volatile int64_t* addr); |
| 64 | |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 65 | #endif /*_DALVIK_ATOMIC*/ |