atomic.h revision ac322da69ee48aa792baf5c48cfb719ce077f67e
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 * Unless otherwise noted, the operations below perform a full fence before
29 * the atomic operation on SMP systems ("release" semantics).
30 */
31
32void android_atomic_write(int32_t value, volatile int32_t* addr);
33
34/*
35 * all these atomic operations return the previous value
36 */
37
38int32_t android_atomic_inc(volatile int32_t* addr);
39int32_t android_atomic_dec(volatile int32_t* addr);
40
41int32_t android_atomic_add(int32_t value, volatile int32_t* addr);
42int32_t android_atomic_and(int32_t value, volatile int32_t* addr);
43int32_t android_atomic_or(int32_t value, volatile int32_t* addr);
44
45int32_t android_atomic_swap(int32_t value, volatile int32_t* addr);
46
47/*
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.)
55 */
56int android_atomic_cmpxchg(int32_t oldvalue, int32_t newvalue,
57        volatile int32_t* addr);
58
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 */
64int android_atomic_acquire_cmpxchg(int32_t oldvalue, int32_t newvalue,
65        volatile int32_t* addr);
66
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
73
74#ifdef __cplusplus
75} // extern "C"
76#endif
77
78#endif // ANDROID_CUTILS_ATOMIC_H
79