michael@0: /* michael@0: * Copyright (C) 2007 The Android Open Source Project michael@0: * michael@0: * Licensed under the Apache License, Version 2.0 (the "License"); michael@0: * you may not use this file except in compliance with the License. michael@0: * You may obtain a copy of the License at michael@0: * michael@0: * http://www.apache.org/licenses/LICENSE-2.0 michael@0: * michael@0: * Unless required by applicable law or agreed to in writing, software michael@0: * distributed under the License is distributed on an "AS IS" BASIS, michael@0: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. michael@0: * See the License for the specific language governing permissions and michael@0: * limitations under the License. michael@0: */ michael@0: michael@0: #ifndef ANDROID_CUTILS_ATOMIC_H michael@0: #define ANDROID_CUTILS_ATOMIC_H michael@0: michael@0: #include michael@0: #include michael@0: michael@0: #ifdef __cplusplus michael@0: extern "C" { michael@0: #endif michael@0: michael@0: /* michael@0: * NOTE: memory shared between threads is synchronized by all atomic operations michael@0: * below, this means that no explicit memory barrier is required: all reads or michael@0: * writes issued before android_atomic_* operations are guaranteed to complete michael@0: * before the atomic operation takes place. michael@0: */ michael@0: michael@0: void android_atomic_write(int32_t value, volatile int32_t* addr); michael@0: michael@0: /* michael@0: * all these atomic operations return the previous value michael@0: */ michael@0: michael@0: michael@0: int32_t android_atomic_inc(volatile int32_t* addr); michael@0: int32_t android_atomic_dec(volatile int32_t* addr); michael@0: michael@0: int32_t android_atomic_add(int32_t value, volatile int32_t* addr); michael@0: int32_t android_atomic_and(int32_t value, volatile int32_t* addr); michael@0: int32_t android_atomic_or(int32_t value, volatile int32_t* addr); michael@0: michael@0: int32_t android_atomic_swap(int32_t value, volatile int32_t* addr); michael@0: michael@0: /* michael@0: * NOTE: Two "quasiatomic" operations on the exact same memory address michael@0: * are guaranteed to operate atomically with respect to each other, michael@0: * but no guarantees are made about quasiatomic operations mixed with michael@0: * non-quasiatomic operations on the same address, nor about michael@0: * quasiatomic operations that are performed on partially-overlapping michael@0: * memory. michael@0: */ michael@0: michael@0: int64_t android_quasiatomic_swap_64(int64_t value, volatile int64_t* addr); michael@0: int64_t android_quasiatomic_read_64(volatile int64_t* addr); michael@0: michael@0: /* michael@0: * cmpxchg return a non zero value if the exchange was NOT performed, michael@0: * in other words if oldvalue != *addr michael@0: */ michael@0: michael@0: int android_atomic_cmpxchg(int32_t oldvalue, int32_t newvalue, michael@0: volatile int32_t* addr); michael@0: michael@0: int android_quasiatomic_cmpxchg_64(int64_t oldvalue, int64_t newvalue, michael@0: volatile int64_t* addr); michael@0: michael@0: michael@0: michael@0: #ifdef __cplusplus michael@0: } // extern "C" michael@0: #endif michael@0: michael@0: #endif // ANDROID_CUTILS_ATOMIC_H