2 * Copyright (C) 2010 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 #include <cutils/atomic.h>
22 * Quasi-atomic 64-bit operations, for platforms that lack the real thing.
24 * TODO: unify ARM/x86/sh implementations using the to-be-written
25 * spin lock implementation. We don't want to rely on mutex innards,
26 * and it would be great if all platforms were running the same code.
29 #if defined(HAVE_MACOSX_IPC)
31 #include <libkern/OSAtomic.h>
33 #if defined(__ppc__) \
35 || defined(__powerpc__) \
36 || defined(__powerpc) \
37 || defined(__POWERPC__) \
40 #define NEED_QUASIATOMICS 1
43 int android_quasiatomic_cmpxchg_64(int64_t oldvalue, int64_t newvalue,
44 volatile int64_t* addr) {
45 return OSAtomicCompareAndSwap64Barrier(oldvalue, newvalue,
49 int64_t android_quasiatomic_swap_64(int64_t value, volatile int64_t* addr) {
53 } while (android_quasiatomic_cmpxchg_64(oldValue, value, addr));
57 int64_t android_quasiatomic_read_64(volatile int64_t* addr) {
58 return OSAtomicAdd64Barrier(0, addr);
62 #elif defined(__i386__) || defined(__x86_64__)
63 #define NEED_QUASIATOMICS 1
66 // Most of the implementation is in atomic-android-arm.s.
68 // on the device, we implement the 64-bit atomic operations through
69 // mutex locking. normally, this is bad because we must initialize
70 // a pthread_mutex_t before being able to use it, and this means
71 // having to do an initialization check on each function call, and
72 // that's where really ugly things begin...
74 // BUT, as a special twist, we take advantage of the fact that in our
75 // pthread library, a mutex is simply a volatile word whose value is always
76 // initialized to 0. In other words, simply declaring a static mutex
77 // object initializes it !
79 // another twist is that we use a small array of mutexes to dispatch
80 // the contention locks from different memory addresses
85 #define SWAP_LOCK_COUNT 32U
86 static pthread_mutex_t _swap_locks[SWAP_LOCK_COUNT];
88 #define SWAP_LOCK(addr) \
89 &_swap_locks[((unsigned)(void*)(addr) >> 3U) % SWAP_LOCK_COUNT]
92 int64_t android_quasiatomic_swap_64(int64_t value, volatile int64_t* addr) {
94 pthread_mutex_t* lock = SWAP_LOCK(addr);
96 pthread_mutex_lock(lock);
101 pthread_mutex_unlock(lock);
105 int android_quasiatomic_cmpxchg_64(int64_t oldvalue, int64_t newvalue,
106 volatile int64_t* addr) {
108 pthread_mutex_t* lock = SWAP_LOCK(addr);
110 pthread_mutex_lock(lock);
112 if (*addr == oldvalue) {
118 pthread_mutex_unlock(lock);
122 int64_t android_quasiatomic_read_64(volatile int64_t* addr) {
124 pthread_mutex_t* lock = SWAP_LOCK(addr);
126 pthread_mutex_lock(lock);
128 pthread_mutex_unlock(lock);
132 /*****************************************************************************/
134 #define NEED_QUASIATOMICS 1
137 #error "Unsupported atomic operations for this platform"
141 #if NEED_QUASIATOMICS
143 /* Note that a spinlock is *not* a good idea in general
144 * since they can introduce subtle issues. For example,
145 * a real-time thread trying to acquire a spinlock already
146 * acquired by another thread will never yeld, making the
147 * CPU loop endlessly!
149 * However, this code is only used on the Linux simulator
150 * so it's probably ok for us.
152 * The alternative is to use a pthread mutex, but
153 * these must be initialized before being used, and
154 * then you have the problem of lazily initializing
155 * a mutex without any other synchronization primitive.
157 * TODO: these currently use sched_yield(), which is not guaranteed to
158 * do anything at all. We need to use dvmIterativeSleep or a wait /
159 * notify mechanism if the initial attempt fails.
162 /* global spinlock for all 64-bit quasiatomic operations */
163 static int32_t quasiatomic_spinlock = 0;
165 int android_quasiatomic_cmpxchg_64(int64_t oldvalue, int64_t newvalue,
166 volatile int64_t* addr) {
169 while (android_atomic_acquire_cas(0, 1, &quasiatomic_spinlock)) {
170 #ifdef HAVE_WIN32_THREADS
177 if (*addr == oldvalue) {
184 android_atomic_release_store(0, &quasiatomic_spinlock);
189 int64_t android_quasiatomic_read_64(volatile int64_t* addr) {
192 while (android_atomic_acquire_cas(0, 1, &quasiatomic_spinlock)) {
193 #ifdef HAVE_WIN32_THREADS
201 android_atomic_release_store(0, &quasiatomic_spinlock);
206 int64_t android_quasiatomic_swap_64(int64_t value, volatile int64_t* addr) {
209 while (android_atomic_acquire_cas(0, 1, &quasiatomic_spinlock)) {
210 #ifdef HAVE_WIN32_THREADS
219 android_atomic_release_store(0, &quasiatomic_spinlock);