2
* Copyright © 2013 Canonical Ltd.
4
* This program is free software: you can redistribute it and/or modify it
5
* under the terms of the GNU Lesser General Public License version 3,
6
* as published by the Free Software Foundation.
8
* This program is distributed in the hope that it will be useful,
9
* but WITHOUT ANY WARRANTY; without even the implied warranty of
10
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
* GNU General Public License for more details.
13
* You should have received a copy of the GNU Lesser General Public License
14
* along with this program. If not, see <http://www.gnu.org/licenses/>.
16
* Authored by: Alan Griffiths <alan@octopull.co.uk>
19
#ifndef MIR_ANDROID_UBUNTU_ATOMIC_H_
20
#define MIR_ANDROID_UBUNTU_ATOMIC_H_
29
* A handful of basic atomic operations. The appropriate <atomic>
30
* functions should be used instead of these whenever possible.
31
* Only those functions needed by the input code are actually supported.
32
* Others from the corresponding android cutils header are commented out.
35
typedef std::atomic<int32_t> android_atomic_int32_t;
38
* Basic arithmetic and bitwise operations. These all provide a
39
* barrier with "release" ordering, and return the previous value.
41
* These have the same characteristics (e.g. what happens on overflow)
42
* as the equivalent non-atomic C operations.
44
inline int32_t android_atomic_inc(android_atomic_int32_t* addr) { return addr->fetch_add(1); }
45
inline int32_t android_atomic_dec(android_atomic_int32_t* addr) { return addr->fetch_add(-1); }
46
inline int32_t android_atomic_add(int32_t value, android_atomic_int32_t* addr) { return addr->fetch_add(value); }
47
inline int32_t android_atomic_or(int32_t value, android_atomic_int32_t* addr) { return addr->fetch_or(value); }
50
* Compare-and-set operation with "acquire" or "release" ordering.
52
* This returns zero if the new value was successfully stored, which will
53
* only happen when *addr == oldvalue.
55
* (The return value is inverted from implementations on other platforms,
56
* but matches the ARM ldrex/strex result.)
58
* Implementations that use the release CAS in a loop may be less efficient
59
* than possible, because we re-issue the memory barrier on each iteration.
61
inline int android_atomic_release_cas(int32_t oldvalue, int32_t newvalue,
62
android_atomic_int32_t* addr) { return !addr->compare_exchange_strong(oldvalue, newvalue); }
66
* Aliases for code using an older version of this header. These are now
67
* deprecated and should not be used. The definitions will be removed
68
* in a future release.
70
#define android_atomic_cmpxchg android_atomic_release_cas
72
using mir_input::android_atomic_int32_t;
73
using mir_input::android_atomic_inc;
74
using mir_input::android_atomic_dec;
75
using mir_input::android_atomic_add;
76
using mir_input::android_atomic_or;
77
using mir_input::android_atomic_release_cas;
79
#endif /* MIR_ANDROID_UBUNTU_ATOMIC_H_ */