blob: 8bac68ad394f30a041eefdc503ae0fdefc32b544 [file] [log] [blame]
Shin-ichiro KAWASAKIc6af9112009-08-04 19:14:22 +09001/*
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#include <cutils/atomic.h>
18#ifdef HAVE_WIN32_THREADS
19#include <windows.h>
20#else
21#include <sched.h>
22#endif
23
24/*
25 * Note :
26 *
27 * (1) SuperH does not have CMPXCHG. It has only TAS for atomic
28 * operations. It does not seem a good idea to implement CMPXCHG,
29 * with TAS. So, we choose to implemnt these operations with
30 * posix mutexes. Please be sure that this might cause performance
31 * problem for Android-SH. Using LL/SC instructions supported in SH-X3,
32 * best performnace would be realized.
33 *
34 * (2) Mutex initialization problem happens, which is commented for
35 * ARM implementation, in this file above.
36 * We follow the fact that the initializer for mutex is a simple zero
37 * value.
Andy McFadden8dfa47d2010-05-27 10:10:18 -070038 *
39 * (3) These operations are NOT safe for SMP, as there is no currently
40 * no definition for a memory barrier operation.
Shin-ichiro KAWASAKIc6af9112009-08-04 19:14:22 +090041 */
42
43#include <pthread.h>
44
45#define SWAP_LOCK_COUNT 32U
46static pthread_mutex_t _swap_locks[SWAP_LOCK_COUNT];
47
48#define SWAP_LOCK(addr) \
49 &_swap_locks[((unsigned)(void*)(addr) >> 3U) % SWAP_LOCK_COUNT]
50
51
Carl Shapirod55f0ad2010-09-28 13:47:03 -070052int32_t android_atomic_acquire_load(volatile const int32_t* addr)
Andy McFadden8dfa47d2010-05-27 10:10:18 -070053{
54 return *addr;
55}
56
Carl Shapirod55f0ad2010-09-28 13:47:03 -070057int32_t android_atomic_release_load(volatile const int32_t* addr)
Andy McFadden8dfa47d2010-05-27 10:10:18 -070058{
59 return *addr;
60}
61
62void android_atomic_acquire_store(int32_t value, volatile int32_t* addr) {
Shin-ichiro KAWASAKIc6af9112009-08-04 19:14:22 +090063 int32_t oldValue;
64 do {
65 oldValue = *addr;
Andy McFadden8dfa47d2010-05-27 10:10:18 -070066 } while (android_atomic_release_cas(oldValue, value, addr));
67}
68
69void android_atomic_release_store(int32_t value, volatile int32_t* addr) {
70 int32_t oldValue;
71 do {
72 oldValue = *addr;
73 } while (android_atomic_release_cas(oldValue, value, addr));
Shin-ichiro KAWASAKIc6af9112009-08-04 19:14:22 +090074}
75
76int32_t android_atomic_inc(volatile int32_t* addr) {
77 int32_t oldValue;
78 do {
79 oldValue = *addr;
Andy McFadden8dfa47d2010-05-27 10:10:18 -070080 } while (android_atomic_release_cas(oldValue, oldValue+1, addr));
Shin-ichiro KAWASAKIc6af9112009-08-04 19:14:22 +090081 return oldValue;
82}
83
84int32_t android_atomic_dec(volatile int32_t* addr) {
85 int32_t oldValue;
86 do {
87 oldValue = *addr;
Andy McFadden8dfa47d2010-05-27 10:10:18 -070088 } while (android_atomic_release_cas(oldValue, oldValue-1, addr));
Shin-ichiro KAWASAKIc6af9112009-08-04 19:14:22 +090089 return oldValue;
90}
91
92int32_t android_atomic_add(int32_t value, volatile int32_t* addr) {
93 int32_t oldValue;
94 do {
95 oldValue = *addr;
Andy McFadden8dfa47d2010-05-27 10:10:18 -070096 } while (android_atomic_release_cas(oldValue, oldValue+value, addr));
Shin-ichiro KAWASAKIc6af9112009-08-04 19:14:22 +090097 return oldValue;
98}
99
100int32_t android_atomic_and(int32_t value, volatile int32_t* addr) {
101 int32_t oldValue;
102 do {
103 oldValue = *addr;
Andy McFadden8dfa47d2010-05-27 10:10:18 -0700104 } while (android_atomic_release_cas(oldValue, oldValue&value, addr));
Shin-ichiro KAWASAKIc6af9112009-08-04 19:14:22 +0900105 return oldValue;
106}
107
108int32_t android_atomic_or(int32_t value, volatile int32_t* addr) {
109 int32_t oldValue;
110 do {
111 oldValue = *addr;
Andy McFadden8dfa47d2010-05-27 10:10:18 -0700112 } while (android_atomic_release_cas(oldValue, oldValue|value, addr));
Shin-ichiro KAWASAKIc6af9112009-08-04 19:14:22 +0900113 return oldValue;
114}
115
Andy McFadden8dfa47d2010-05-27 10:10:18 -0700116int android_atomic_acquire_cmpxchg(int32_t oldvalue, int32_t newvalue,
117 volatile int32_t* addr) {
118 return android_atomic_release_cmpxchg(oldValue, newValue, addr);
119}
120
121int android_atomic_release_cmpxchg(int32_t oldvalue, int32_t newvalue,
Shin-ichiro KAWASAKIc6af9112009-08-04 19:14:22 +0900122 volatile int32_t* addr) {
123 int result;
124 pthread_mutex_t* lock = SWAP_LOCK(addr);
125
126 pthread_mutex_lock(lock);
127
128 if (*addr == oldvalue) {
129 *addr = newvalue;
130 result = 0;
131 } else {
132 result = 1;
133 }
134 pthread_mutex_unlock(lock);
135 return result;
136}
137