• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #ifndef ANDROID_CUTILS_ATOMIC_X86_H
18 #define ANDROID_CUTILS_ATOMIC_X86_H
19 
20 #include <stdint.h>
21 
22 #ifndef ANDROID_ATOMIC_INLINE
23 #define ANDROID_ATOMIC_INLINE inline __attribute__((always_inline))
24 #endif
25 
android_compiler_barrier(void)26 extern ANDROID_ATOMIC_INLINE void android_compiler_barrier(void)
27 {
28     __asm__ __volatile__ ("" : : : "memory");
29 }
30 
31 #if ANDROID_SMP == 0
android_memory_barrier(void)32 extern ANDROID_ATOMIC_INLINE void android_memory_barrier(void)
33 {
34     android_compiler_barrier();
35 }
36 #else
android_memory_barrier(void)37 extern ANDROID_ATOMIC_INLINE void android_memory_barrier(void)
38 {
39     __asm__ __volatile__ ("mfence" : : : "memory");
40 }
41 #endif
42 
43 extern ANDROID_ATOMIC_INLINE int32_t
android_atomic_acquire_load(volatile const int32_t * ptr)44 android_atomic_acquire_load(volatile const int32_t *ptr)
45 {
46     int32_t value = *ptr;
47     android_compiler_barrier();
48     return value;
49 }
50 
51 extern ANDROID_ATOMIC_INLINE int32_t
android_atomic_release_load(volatile const int32_t * ptr)52 android_atomic_release_load(volatile const int32_t *ptr)
53 {
54     android_memory_barrier();
55     return *ptr;
56 }
57 
58 extern ANDROID_ATOMIC_INLINE void
android_atomic_acquire_store(int32_t value,volatile int32_t * ptr)59 android_atomic_acquire_store(int32_t value, volatile int32_t *ptr)
60 {
61     *ptr = value;
62     android_memory_barrier();
63 }
64 
65 extern ANDROID_ATOMIC_INLINE void
android_atomic_release_store(int32_t value,volatile int32_t * ptr)66 android_atomic_release_store(int32_t value, volatile int32_t *ptr)
67 {
68     android_compiler_barrier();
69     *ptr = value;
70 }
71 
72 extern ANDROID_ATOMIC_INLINE int
android_atomic_cas(int32_t old_value,int32_t new_value,volatile int32_t * ptr)73 android_atomic_cas(int32_t old_value, int32_t new_value, volatile int32_t *ptr)
74 {
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 
83 extern ANDROID_ATOMIC_INLINE int
android_atomic_acquire_cas(int32_t old_value,int32_t new_value,volatile int32_t * ptr)84 android_atomic_acquire_cas(int32_t old_value,
85                            int32_t new_value,
86                            volatile int32_t *ptr)
87 {
88     /* Loads are not reordered with other loads. */
89     return android_atomic_cas(old_value, new_value, ptr);
90 }
91 
92 extern ANDROID_ATOMIC_INLINE int
android_atomic_release_cas(int32_t old_value,int32_t new_value,volatile int32_t * ptr)93 android_atomic_release_cas(int32_t old_value,
94                            int32_t new_value,
95                            volatile int32_t *ptr)
96 {
97     /* Stores are not reordered with other stores. */
98     return android_atomic_cas(old_value, new_value, ptr);
99 }
100 
101 extern ANDROID_ATOMIC_INLINE int32_t
android_atomic_add(int32_t increment,volatile int32_t * ptr)102 android_atomic_add(int32_t increment, volatile int32_t *ptr)
103 {
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 
111 extern ANDROID_ATOMIC_INLINE int32_t
android_atomic_inc(volatile int32_t * addr)112 android_atomic_inc(volatile int32_t *addr)
113 {
114     return android_atomic_add(1, addr);
115 }
116 
117 extern ANDROID_ATOMIC_INLINE int32_t
android_atomic_dec(volatile int32_t * addr)118 android_atomic_dec(volatile int32_t *addr)
119 {
120     return android_atomic_add(-1, addr);
121 }
122 
123 extern ANDROID_ATOMIC_INLINE int32_t
android_atomic_and(int32_t value,volatile int32_t * ptr)124 android_atomic_and(int32_t value, volatile int32_t *ptr)
125 {
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 
134 extern ANDROID_ATOMIC_INLINE int32_t
android_atomic_or(int32_t value,volatile int32_t * ptr)135 android_atomic_or(int32_t value, volatile int32_t *ptr)
136 {
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 */
146