OSDN Git Service

Atomic/SMP update, part 2. (manual to dalvik-dev)
[android-x86/dalvik.git] / vm / Atomic.c
1 /*
2  * Copyright (C) 2010 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 #include "Dalvik.h"
18
19 #include <cutils/atomic.h>
20
21 /*
22  * Quasi-atomic 64-bit operations, for platforms that lack the real thing.
23  *
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.
27  */
28
29 #if defined(HAVE_MACOSX_IPC)
30
31 #include <libkern/OSAtomic.h>
32
33 #if defined(__ppc__)        \
34     || defined(__PPC__)     \
35     || defined(__powerpc__) \
36     || defined(__powerpc)   \
37     || defined(__POWERPC__) \
38     || defined(_M_PPC)      \
39     || defined(__PPC)
40 #define NEED_QUASIATOMICS 1
41 #else
42
43 int android_quasiatomic_cmpxchg_64(int64_t oldvalue, int64_t newvalue,
44         volatile int64_t* addr) {
45     return OSAtomicCompareAndSwap64Barrier(oldvalue, newvalue,
46             (int64_t*)addr) == 0;
47 }
48
49 int64_t android_quasiatomic_swap_64(int64_t value, volatile int64_t* addr) {
50     int64_t oldValue;
51     do {
52         oldValue = *addr;
53     } while (android_quasiatomic_cmpxchg_64(oldValue, value, addr));
54     return oldValue;
55 }
56
57 int64_t android_quasiatomic_read_64(volatile int64_t* addr) {
58     return OSAtomicAdd64Barrier(0, addr);
59 }
60 #endif
61
62 #elif defined(__i386__) || defined(__x86_64__)
63 #define NEED_QUASIATOMICS 1
64
65 #elif __arm__
66 // Most of the implementation is in atomic-android-arm.s.
67
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...
73 //
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 !
78 //
79 // another twist is that we use a small array of mutexes to dispatch
80 // the contention locks from different memory addresses
81 //
82
83 #include <pthread.h>
84
85 #define  SWAP_LOCK_COUNT  32U
86 static pthread_mutex_t  _swap_locks[SWAP_LOCK_COUNT];
87
88 #define  SWAP_LOCK(addr)   \
89    &_swap_locks[((unsigned)(void*)(addr) >> 3U) % SWAP_LOCK_COUNT]
90
91
92 int64_t android_quasiatomic_swap_64(int64_t value, volatile int64_t* addr) {
93     int64_t oldValue;
94     pthread_mutex_t*  lock = SWAP_LOCK(addr);
95
96     pthread_mutex_lock(lock);
97
98     oldValue = *addr;
99     *addr    = value;
100
101     pthread_mutex_unlock(lock);
102     return oldValue;
103 }
104
105 int android_quasiatomic_cmpxchg_64(int64_t oldvalue, int64_t newvalue,
106         volatile int64_t* addr) {
107     int result;
108     pthread_mutex_t*  lock = SWAP_LOCK(addr);
109
110     pthread_mutex_lock(lock);
111
112     if (*addr == oldvalue) {
113         *addr  = newvalue;
114         result = 0;
115     } else {
116         result = 1;
117     }
118     pthread_mutex_unlock(lock);
119     return result;
120 }
121
122 int64_t android_quasiatomic_read_64(volatile int64_t* addr) {
123     int64_t result;
124     pthread_mutex_t*  lock = SWAP_LOCK(addr);
125
126     pthread_mutex_lock(lock);
127     result = *addr;
128     pthread_mutex_unlock(lock);
129     return result;
130 }
131
132 /*****************************************************************************/
133 #elif __sh__
134 #define NEED_QUASIATOMICS 1
135
136 #else
137 #error "Unsupported atomic operations for this platform"
138 #endif
139
140
141 #if NEED_QUASIATOMICS
142
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!
148  *
149  * However, this code is only used on the Linux simulator
150  * so it's probably ok for us.
151  *
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.
156  *
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.
160  */
161
162 /* global spinlock for all 64-bit quasiatomic operations */
163 static int32_t quasiatomic_spinlock = 0;
164
165 int android_quasiatomic_cmpxchg_64(int64_t oldvalue, int64_t newvalue,
166         volatile int64_t* addr) {
167     int result;
168
169     while (android_atomic_acquire_cas(0, 1, &quasiatomic_spinlock)) {
170 #ifdef HAVE_WIN32_THREADS
171         Sleep(0);
172 #else
173         sched_yield();
174 #endif
175     }
176
177     if (*addr == oldvalue) {
178         *addr = newvalue;
179         result = 0;
180     } else {
181         result = 1;
182     }
183
184     android_atomic_release_store(0, &quasiatomic_spinlock);
185
186     return result;
187 }
188
189 int64_t android_quasiatomic_read_64(volatile int64_t* addr) {
190     int64_t result;
191
192     while (android_atomic_acquire_cas(0, 1, &quasiatomic_spinlock)) {
193 #ifdef HAVE_WIN32_THREADS
194         Sleep(0);
195 #else
196         sched_yield();
197 #endif
198     }
199
200     result = *addr;
201     android_atomic_release_store(0, &quasiatomic_spinlock);
202
203     return result;
204 }
205
206 int64_t android_quasiatomic_swap_64(int64_t value, volatile int64_t* addr) {
207     int64_t result;
208
209     while (android_atomic_acquire_cas(0, 1, &quasiatomic_spinlock)) {
210 #ifdef HAVE_WIN32_THREADS
211         Sleep(0);
212 #else
213         sched_yield();
214 #endif
215     }
216
217     result = *addr;
218     *addr = value;
219     android_atomic_release_store(0, &quasiatomic_spinlock);
220
221     return result;
222 }
223
224 #endif
225