summaryrefslogtreecommitdiffstats
path: root/include/cutils/atomic.h
blob: 8e12902b00d964c74d1b5632e7978b5fe1088f63 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
/*
 * Copyright (C) 2007 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#ifndef ANDROID_CUTILS_ATOMIC_H
#define ANDROID_CUTILS_ATOMIC_H

#include <stdint.h>
#include <sys/types.h>

#ifdef __cplusplus
extern "C" {
#endif

/*
 * Unless otherwise noted, the operations below perform a full fence before
 * the atomic operation on SMP systems ("release" semantics).
 */

void android_atomic_write(int32_t value, volatile int32_t* addr);

/*
 * all these atomic operations return the previous value
 */

int32_t android_atomic_inc(volatile int32_t* addr);
int32_t android_atomic_dec(volatile int32_t* addr);

int32_t android_atomic_add(int32_t value, volatile int32_t* addr);
int32_t android_atomic_and(int32_t value, volatile int32_t* addr);
int32_t android_atomic_or(int32_t value, volatile int32_t* addr);

int32_t android_atomic_swap(int32_t value, volatile int32_t* addr);

/*
 * cmpxchg returns zero if the new value was successfully written.  This
 * will only happen when *addr == oldvalue.
 *
 * (The return value is inverted from implementations on other platforms, but
 * matches the ARM ldrex/strex sematics.  Note also this is a compare-and-set
 * operation, not a compare-and-exchange operation, since we don't return
 * the original value.)
 */
int android_atomic_cmpxchg(int32_t oldvalue, int32_t newvalue,
        volatile int32_t* addr);

/*
 * Same basic operation as android_atomic_cmpxchg, but with "acquire"
 * semantics.  The memory barrier, if required, is performed after the
 * new value is stored.  Useful for acquiring a spin lock.
 */
int android_atomic_acquire_cmpxchg(int32_t oldvalue, int32_t newvalue,
        volatile int32_t* addr);

/*
 * Perform an atomic store with "release" semantics.  The memory barrier,
 * if required, is performed before the store instruction.  Useful for
 * releasing a spin lock.
 */
#define android_atomic_release_store android_atomic_write

#ifdef __cplusplus
} // extern "C"
#endif

#endif // ANDROID_CUTILS_ATOMIC_H