blob: 06bf1a3a1c9c3893b3ad563717446e0bdb96903c [file] [log] [blame]
Carl Shapiro93b0cb42010-06-03 17:05:15 -07001/*
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#ifndef ANDROID_CUTILS_ATOMIC_X86_H
18#define ANDROID_CUTILS_ATOMIC_X86_H
19
20#include <stdint.h>
21
Ben Cheng5206d592012-12-07 10:54:09 -080022#ifndef ANDROID_ATOMIC_INLINE
23#define ANDROID_ATOMIC_INLINE inline __attribute__((always_inline))
24#endif
25
26extern ANDROID_ATOMIC_INLINE void android_compiler_barrier(void)
Carl Shapiro93b0cb42010-06-03 17:05:15 -070027{
28 __asm__ __volatile__ ("" : : : "memory");
29}
30
31#if ANDROID_SMP == 0
Ben Cheng5206d592012-12-07 10:54:09 -080032extern ANDROID_ATOMIC_INLINE void android_memory_barrier(void)
Carl Shapiro93b0cb42010-06-03 17:05:15 -070033{
34 android_compiler_barrier();
35}
36#else
Ben Cheng5206d592012-12-07 10:54:09 -080037extern ANDROID_ATOMIC_INLINE void android_memory_barrier(void)
Carl Shapiro93b0cb42010-06-03 17:05:15 -070038{
39 __asm__ __volatile__ ("mfence" : : : "memory");
40}
Carl Shapiro93b0cb42010-06-03 17:05:15 -070041#endif
42
Ben Cheng5206d592012-12-07 10:54:09 -080043extern ANDROID_ATOMIC_INLINE int32_t
44android_atomic_acquire_load(volatile const int32_t *ptr)
Carl Shapirod55f0ad2010-09-28 13:47:03 -070045{
Carl Shapiro93b0cb42010-06-03 17:05:15 -070046 int32_t value = *ptr;
47 android_compiler_barrier();
48 return value;
49}
50
Ben Cheng5206d592012-12-07 10:54:09 -080051extern ANDROID_ATOMIC_INLINE int32_t
52android_atomic_release_load(volatile const int32_t *ptr)
Carl Shapirod55f0ad2010-09-28 13:47:03 -070053{
Carl Shapiro93b0cb42010-06-03 17:05:15 -070054 android_memory_barrier();
55 return *ptr;
56}
57
Ben Cheng5206d592012-12-07 10:54:09 -080058extern ANDROID_ATOMIC_INLINE void
59android_atomic_acquire_store(int32_t value, volatile int32_t *ptr)
Carl Shapirod55f0ad2010-09-28 13:47:03 -070060{
Carl Shapiro93b0cb42010-06-03 17:05:15 -070061 *ptr = value;
62 android_memory_barrier();
63}
64
Ben Cheng5206d592012-12-07 10:54:09 -080065extern ANDROID_ATOMIC_INLINE void
66android_atomic_release_store(int32_t value, volatile int32_t *ptr)
Carl Shapirod55f0ad2010-09-28 13:47:03 -070067{
Carl Shapiro93b0cb42010-06-03 17:05:15 -070068 android_compiler_barrier();
69 *ptr = value;
70}
71
Ben Cheng5206d592012-12-07 10:54:09 -080072extern ANDROID_ATOMIC_INLINE int
73android_atomic_cas(int32_t old_value, int32_t new_value, volatile int32_t *ptr)
Carl Shapiro93b0cb42010-06-03 17:05:15 -070074{
75 int32_t prev;
76 __asm__ __volatile__ ("lock; cmpxchgl %1, %2"
77 : "=a" (prev)
78 : "q" (new_value), "m" (*ptr), "0" (old_value)
79 : "memory");
80 return prev != old_value;
81}
82
Ben Cheng5206d592012-12-07 10:54:09 -080083extern ANDROID_ATOMIC_INLINE int
84android_atomic_acquire_cas(int32_t old_value,
85 int32_t new_value,
86 volatile int32_t *ptr)
Carl Shapiro93b0cb42010-06-03 17:05:15 -070087{
88 /* Loads are not reordered with other loads. */
89 return android_atomic_cas(old_value, new_value, ptr);
90}
91
Ben Cheng5206d592012-12-07 10:54:09 -080092extern ANDROID_ATOMIC_INLINE int
93android_atomic_release_cas(int32_t old_value,
94 int32_t new_value,
95 volatile int32_t *ptr)
Carl Shapiro93b0cb42010-06-03 17:05:15 -070096{
97 /* Stores are not reordered with other stores. */
98 return android_atomic_cas(old_value, new_value, ptr);
99}
100
Ben Cheng5206d592012-12-07 10:54:09 -0800101extern ANDROID_ATOMIC_INLINE int32_t
102android_atomic_add(int32_t increment, volatile int32_t *ptr)
Carl Shapiro93b0cb42010-06-03 17:05:15 -0700103{
104 __asm__ __volatile__ ("lock; xaddl %0, %1"
105 : "+r" (increment), "+m" (*ptr)
106 : : "memory");
107 /* increment now holds the old value of *ptr */
108 return increment;
109}
110
Ben Cheng5206d592012-12-07 10:54:09 -0800111extern ANDROID_ATOMIC_INLINE int32_t
112android_atomic_inc(volatile int32_t *addr)
Carl Shapirod55f0ad2010-09-28 13:47:03 -0700113{
Carl Shapiro93b0cb42010-06-03 17:05:15 -0700114 return android_atomic_add(1, addr);
115}
116
Ben Cheng5206d592012-12-07 10:54:09 -0800117extern ANDROID_ATOMIC_INLINE int32_t
118android_atomic_dec(volatile int32_t *addr)
Carl Shapirod55f0ad2010-09-28 13:47:03 -0700119{
Carl Shapiro93b0cb42010-06-03 17:05:15 -0700120 return android_atomic_add(-1, addr);
121}
122
Ben Cheng5206d592012-12-07 10:54:09 -0800123extern ANDROID_ATOMIC_INLINE int32_t
124android_atomic_and(int32_t value, volatile int32_t *ptr)
Carl Shapiro93b0cb42010-06-03 17:05:15 -0700125{
126 int32_t prev, status;
127 do {
128 prev = *ptr;
129 status = android_atomic_cas(prev, prev & value, ptr);
130 } while (__builtin_expect(status != 0, 0));
131 return prev;
132}
133
Ben Cheng5206d592012-12-07 10:54:09 -0800134extern ANDROID_ATOMIC_INLINE int32_t
135android_atomic_or(int32_t value, volatile int32_t *ptr)
Carl Shapiro93b0cb42010-06-03 17:05:15 -0700136{
137 int32_t prev, status;
138 do {
139 prev = *ptr;
140 status = android_atomic_cas(prev, prev | value, ptr);
141 } while (__builtin_expect(status != 0, 0));
142 return prev;
143}
144
145#endif /* ANDROID_CUTILS_ATOMIC_X86_H */