Home | History | Annotate | Download | only in cutils
      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 /*
     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 
     34 void android_atomic_write(int32_t value, volatile int32_t* addr);
     35 
     36 /*
     37  * all these atomic operations return the previous value
     38  */
     39 
     40 
     41 int32_t android_atomic_inc(volatile int32_t* addr);
     42 int32_t android_atomic_dec(volatile int32_t* addr);
     43 
     44 int32_t android_atomic_add(int32_t value, volatile int32_t* addr);
     45 int32_t android_atomic_and(int32_t value, volatile int32_t* addr);
     46 int32_t android_atomic_or(int32_t value, volatile int32_t* addr);
     47 
     48 int32_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 
     59 int64_t android_quasiatomic_swap_64(int64_t value, volatile int64_t* addr);
     60 int64_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 
     67 int android_atomic_cmpxchg(int32_t oldvalue, int32_t newvalue,
     68         volatile int32_t* addr);
     69 
     70 int 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