atomic-x86.h revision 5206d5971208fab8208669dabc98bbb9f7e4a45a
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 26extern ANDROID_ATOMIC_INLINE void android_compiler_barrier(void) 27{ 28 __asm__ __volatile__ ("" : : : "memory"); 29} 30 31#if ANDROID_SMP == 0 32extern ANDROID_ATOMIC_INLINE void android_memory_barrier(void) 33{ 34 android_compiler_barrier(); 35} 36extern ANDROID_ATOMIC_INLINE void android_memory_store_barrier(void) 37{ 38 android_compiler_barrier(); 39} 40#else 41extern ANDROID_ATOMIC_INLINE void android_memory_barrier(void) 42{ 43 __asm__ __volatile__ ("mfence" : : : "memory"); 44} 45extern ANDROID_ATOMIC_INLINE void android_memory_store_barrier(void) 46{ 47 android_compiler_barrier(); 48} 49#endif 50 51extern ANDROID_ATOMIC_INLINE int32_t 52android_atomic_acquire_load(volatile const int32_t *ptr) 53{ 54 int32_t value = *ptr; 55 android_compiler_barrier(); 56 return value; 57} 58 59extern ANDROID_ATOMIC_INLINE int32_t 60android_atomic_release_load(volatile const int32_t *ptr) 61{ 62 android_memory_barrier(); 63 return *ptr; 64} 65 66extern ANDROID_ATOMIC_INLINE void 67android_atomic_acquire_store(int32_t value, volatile int32_t *ptr) 68{ 69 *ptr = value; 70 android_memory_barrier(); 71} 72 73extern ANDROID_ATOMIC_INLINE void 74android_atomic_release_store(int32_t value, volatile int32_t *ptr) 75{ 76 android_compiler_barrier(); 77 *ptr = value; 78} 79 80extern ANDROID_ATOMIC_INLINE int 81android_atomic_cas(int32_t old_value, int32_t new_value, volatile int32_t *ptr) 82{ 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 91extern ANDROID_ATOMIC_INLINE int 92android_atomic_acquire_cas(int32_t old_value, 93 int32_t new_value, 94 volatile int32_t *ptr) 95{ 96 /* Loads are not reordered with other loads. */ 97 return android_atomic_cas(old_value, new_value, ptr); 98} 99 100extern ANDROID_ATOMIC_INLINE int 101android_atomic_release_cas(int32_t old_value, 102 int32_t new_value, 103 volatile int32_t *ptr) 104{ 105 /* Stores are not reordered with other stores. */ 106 return android_atomic_cas(old_value, new_value, ptr); 107} 108 109extern ANDROID_ATOMIC_INLINE int32_t 110android_atomic_add(int32_t increment, volatile int32_t *ptr) 111{ 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 119extern ANDROID_ATOMIC_INLINE int32_t 120android_atomic_inc(volatile int32_t *addr) 121{ 122 return android_atomic_add(1, addr); 123} 124 125extern ANDROID_ATOMIC_INLINE int32_t 126android_atomic_dec(volatile int32_t *addr) 127{ 128 return android_atomic_add(-1, addr); 129} 130 131extern ANDROID_ATOMIC_INLINE int32_t 132android_atomic_and(int32_t value, volatile int32_t *ptr) 133{ 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 142extern ANDROID_ATOMIC_INLINE int32_t 143android_atomic_or(int32_t value, volatile int32_t *ptr) 144{ 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 */ 154