atomic.h revision dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0
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 24extern "C" { 25#endif 26 27/* 28 * NOTE: memory shared between threads is synchronized by all atomic operations 29 * below, this means that no explicit memory barrier is required: all reads or 30 * writes issued before android_atomic_* operations are guaranteed to complete 31 * before the atomic operation takes place. 32 */ 33 34void android_atomic_write(int32_t value, volatile int32_t* addr); 35 36/* 37 * all these atomic operations return the previous value 38 */ 39 40 41int32_t android_atomic_inc(volatile int32_t* addr); 42int32_t android_atomic_dec(volatile int32_t* addr); 43 44int32_t android_atomic_add(int32_t value, volatile int32_t* addr); 45int32_t android_atomic_and(int32_t value, volatile int32_t* addr); 46int32_t android_atomic_or(int32_t value, volatile int32_t* addr); 47 48int32_t android_atomic_swap(int32_t value, volatile int32_t* addr); 49 50/* 51 * NOTE: Two "quasiatomic" operations on the exact same memory address 52 * are guaranteed to operate atomically with respect to each other, 53 * but no guarantees are made about quasiatomic operations mixed with 54 * non-quasiatomic operations on the same address, nor about 55 * quasiatomic operations that are performed on partially-overlapping 56 * memory. 57 */ 58 59int64_t android_quasiatomic_swap_64(int64_t value, volatile int64_t* addr); 60int64_t android_quasiatomic_read_64(volatile int64_t* addr); 61 62/* 63 * cmpxchg return a non zero value if the exchange was NOT performed, 64 * in other words if oldvalue != *addr 65 */ 66 67int android_atomic_cmpxchg(int32_t oldvalue, int32_t newvalue, 68 volatile int32_t* addr); 69 70int android_quasiatomic_cmpxchg_64(int64_t oldvalue, int64_t newvalue, 71 volatile int64_t* addr); 72 73 74 75#ifdef __cplusplus 76} // extern "C" 77#endif 78 79#endif // ANDROID_CUTILS_ATOMIC_H 80