blob: 9480f57fedb9e4a6febcd0dc3d84f6bb10ededbd [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}
Ben Cheng5206d592012-12-07 10:54:09 -080036extern ANDROID_ATOMIC_INLINE void android_memory_store_barrier(void)
Brian Carlstrom464431e2010-09-24 10:56:43 -070037{
38 android_compiler_barrier();
39}
Carl Shapiro93b0cb42010-06-03 17:05:15 -070040#else
Ben Cheng5206d592012-12-07 10:54:09 -080041extern ANDROID_ATOMIC_INLINE void android_memory_barrier(void)
Carl Shapiro93b0cb42010-06-03 17:05:15 -070042{
43 __asm__ __volatile__ ("mfence" : : : "memory");
44}
Ben Cheng5206d592012-12-07 10:54:09 -080045extern ANDROID_ATOMIC_INLINE void android_memory_store_barrier(void)
Brian Carlstrom464431e2010-09-24 10:56:43 -070046{
47 android_compiler_barrier();
48}
Carl Shapiro93b0cb42010-06-03 17:05:15 -070049#endif
50
Ben Cheng5206d592012-12-07 10:54:09 -080051extern ANDROID_ATOMIC_INLINE int32_t
52android_atomic_acquire_load(volatile const int32_t *ptr)
Carl Shapirod55f0ad2010-09-28 13:47:03 -070053{
Carl Shapiro93b0cb42010-06-03 17:05:15 -070054 int32_t value = *ptr;
55 android_compiler_barrier();
56 return value;
57}
58
Ben Cheng5206d592012-12-07 10:54:09 -080059extern ANDROID_ATOMIC_INLINE int32_t
60android_atomic_release_load(volatile const int32_t *ptr)
Carl Shapirod55f0ad2010-09-28 13:47:03 -070061{
Carl Shapiro93b0cb42010-06-03 17:05:15 -070062 android_memory_barrier();
63 return *ptr;
64}
65
Ben Cheng5206d592012-12-07 10:54:09 -080066extern ANDROID_ATOMIC_INLINE void
67android_atomic_acquire_store(int32_t value, volatile int32_t *ptr)
Carl Shapirod55f0ad2010-09-28 13:47:03 -070068{
Carl Shapiro93b0cb42010-06-03 17:05:15 -070069 *ptr = value;
70 android_memory_barrier();
71}
72
Ben Cheng5206d592012-12-07 10:54:09 -080073extern ANDROID_ATOMIC_INLINE void
74android_atomic_release_store(int32_t value, volatile int32_t *ptr)
Carl Shapirod55f0ad2010-09-28 13:47:03 -070075{
Carl Shapiro93b0cb42010-06-03 17:05:15 -070076 android_compiler_barrier();
77 *ptr = value;
78}
79
Ben Cheng5206d592012-12-07 10:54:09 -080080extern ANDROID_ATOMIC_INLINE int
81android_atomic_cas(int32_t old_value, int32_t new_value, volatile int32_t *ptr)
Carl Shapiro93b0cb42010-06-03 17:05:15 -070082{
83 int32_t prev;
84 __asm__ __volatile__ ("lock; cmpxchgl %1, %2"
85 : "=a" (prev)
86 : "q" (new_value), "m" (*ptr), "0" (old_value)
87 : "memory");
88 return prev != old_value;
89}
90
Ben Cheng5206d592012-12-07 10:54:09 -080091extern ANDROID_ATOMIC_INLINE int
92android_atomic_acquire_cas(int32_t old_value,
93 int32_t new_value,
94 volatile int32_t *ptr)
Carl Shapiro93b0cb42010-06-03 17:05:15 -070095{
96 /* Loads are not reordered with other loads. */
97 return android_atomic_cas(old_value, new_value, ptr);
98}
99
Ben Cheng5206d592012-12-07 10:54:09 -0800100extern ANDROID_ATOMIC_INLINE int
101android_atomic_release_cas(int32_t old_value,
102 int32_t new_value,
103 volatile int32_t *ptr)
Carl Shapiro93b0cb42010-06-03 17:05:15 -0700104{
105 /* Stores are not reordered with other stores. */
106 return android_atomic_cas(old_value, new_value, ptr);
107}
108
Ben Cheng5206d592012-12-07 10:54:09 -0800109extern ANDROID_ATOMIC_INLINE int32_t
110android_atomic_add(int32_t increment, volatile int32_t *ptr)
Carl Shapiro93b0cb42010-06-03 17:05:15 -0700111{
112 __asm__ __volatile__ ("lock; xaddl %0, %1"
113 : "+r" (increment), "+m" (*ptr)
114 : : "memory");
115 /* increment now holds the old value of *ptr */
116 return increment;
117}
118
Ben Cheng5206d592012-12-07 10:54:09 -0800119extern ANDROID_ATOMIC_INLINE int32_t
120android_atomic_inc(volatile int32_t *addr)
Carl Shapirod55f0ad2010-09-28 13:47:03 -0700121{
Carl Shapiro93b0cb42010-06-03 17:05:15 -0700122 return android_atomic_add(1, addr);
123}
124
Ben Cheng5206d592012-12-07 10:54:09 -0800125extern ANDROID_ATOMIC_INLINE int32_t
126android_atomic_dec(volatile int32_t *addr)
Carl Shapirod55f0ad2010-09-28 13:47:03 -0700127{
Carl Shapiro93b0cb42010-06-03 17:05:15 -0700128 return android_atomic_add(-1, addr);
129}
130
Ben Cheng5206d592012-12-07 10:54:09 -0800131extern ANDROID_ATOMIC_INLINE int32_t
132android_atomic_and(int32_t value, volatile int32_t *ptr)
Carl Shapiro93b0cb42010-06-03 17:05:15 -0700133{
134 int32_t prev, status;
135 do {
136 prev = *ptr;
137 status = android_atomic_cas(prev, prev & value, ptr);
138 } while (__builtin_expect(status != 0, 0));
139 return prev;
140}
141
Ben Cheng5206d592012-12-07 10:54:09 -0800142extern ANDROID_ATOMIC_INLINE int32_t
143android_atomic_or(int32_t value, volatile int32_t *ptr)
Carl Shapiro93b0cb42010-06-03 17:05:15 -0700144{
145 int32_t prev, status;
146 do {
147 prev = *ptr;
148 status = android_atomic_cas(prev, prev | value, ptr);
149 } while (__builtin_expect(status != 0, 0));
150 return prev;
151}
152
153#endif /* ANDROID_CUTILS_ATOMIC_X86_H */