| 1 | #ifndef _URCU_ARCH_UATOMIC_SPARC64_H |
| 2 | #define _URCU_ARCH_UATOMIC_SPARC64_H |
| 3 | |
| 4 | /* |
| 5 | * Copyright (c) 1991-1994 by Xerox Corporation. All rights reserved. |
| 6 | * Copyright (c) 1996-1999 by Silicon Graphics. All rights reserved. |
| 7 | * Copyright (c) 1999-2003 by Hewlett-Packard Company. All rights reserved. |
| 8 | * Copyright (c) 2009 Mathieu Desnoyers |
| 9 | * |
| 10 | * THIS MATERIAL IS PROVIDED AS IS, WITH ABSOLUTELY NO WARRANTY EXPRESSED |
| 11 | * OR IMPLIED. ANY USE IS AT YOUR OWN RISK. |
| 12 | * |
| 13 | * Permission is hereby granted to use or copy this program |
| 14 | * for any purpose, provided the above notices are retained on all copies. |
| 15 | * Permission to modify the code and to distribute modified code is granted, |
| 16 | * provided the above notices are retained, and a notice that the code was |
| 17 | * modified is included with the above copyright notice. |
| 18 | * |
| 19 | * Code inspired from libuatomic_ops-1.2, inherited in part from the |
| 20 | * Boehm-Demers-Weiser conservative garbage collector. |
| 21 | */ |
| 22 | |
| 23 | #include <urcu/compiler.h> |
| 24 | #include <urcu/system.h> |
| 25 | |
| 26 | #ifdef __cplusplus |
| 27 | extern "C" { |
| 28 | #endif |
| 29 | |
| 30 | #ifndef __SIZEOF_LONG__ |
| 31 | #ifdef __LP64__ |
| 32 | #define __SIZEOF_LONG__ 8 |
| 33 | #else |
| 34 | #define __SIZEOF_LONG__ 4 |
| 35 | #endif |
| 36 | #endif |
| 37 | |
| 38 | #ifndef BITS_PER_LONG |
| 39 | #define BITS_PER_LONG (__SIZEOF_LONG__ * 8) |
| 40 | #endif |
| 41 | |
| 42 | /* cmpxchg */ |
| 43 | |
| 44 | static inline __attribute__((always_inline)) |
| 45 | unsigned long _uatomic_cmpxchg(void *addr, unsigned long old, |
| 46 | unsigned long _new, int len) |
| 47 | { |
| 48 | switch (len) { |
| 49 | case 4: |
| 50 | { |
| 51 | __asm__ __volatile__ ( |
| 52 | "membar #StoreLoad | #LoadLoad\n\t" |
| 53 | "cas [%1],%2,%0\n\t" |
| 54 | "membar #StoreLoad | #StoreStore\n\t" |
| 55 | : "+&r" (_new) |
| 56 | : "r" (addr), "r" (old) |
| 57 | : "memory"); |
| 58 | |
| 59 | return _new; |
| 60 | } |
| 61 | #if (BITS_PER_LONG == 64) |
| 62 | case 8: |
| 63 | { |
| 64 | __asm__ __volatile__ ( |
| 65 | "membar #StoreLoad | #LoadLoad\n\t" |
| 66 | "casx [%1],%2,%0\n\t" |
| 67 | "membar #StoreLoad | #StoreStore\n\t" |
| 68 | : "+&r" (_new) |
| 69 | : "r" (addr), "r" (old) |
| 70 | : "memory"); |
| 71 | |
| 72 | return _new; |
| 73 | } |
| 74 | #endif |
| 75 | } |
| 76 | __builtin_trap(); |
| 77 | return 0; |
| 78 | } |
| 79 | |
| 80 | |
| 81 | #define uatomic_cmpxchg(addr, old, _new) \ |
| 82 | ((__typeof__(*(addr))) _uatomic_cmpxchg((addr), (unsigned long)(old),\ |
| 83 | (unsigned long)(_new), \ |
| 84 | sizeof(*(addr)))) |
| 85 | |
| 86 | /* xchg */ |
| 87 | |
| 88 | static inline __attribute__((always_inline)) |
| 89 | unsigned long _uatomic_exchange(void *addr, unsigned long val, int len) |
| 90 | { |
| 91 | switch (len) { |
| 92 | case 4: |
| 93 | { |
| 94 | unsigned int old, oldt; |
| 95 | |
| 96 | oldt = uatomic_read((unsigned int *)addr); |
| 97 | do { |
| 98 | old = oldt; |
| 99 | oldt = _uatomic_cmpxchg(addr, old, val, 4); |
| 100 | } while (oldt != old); |
| 101 | |
| 102 | return old; |
| 103 | } |
| 104 | #if (BITS_PER_LONG == 64) |
| 105 | case 8: |
| 106 | { |
| 107 | unsigned long old, oldt; |
| 108 | |
| 109 | oldt = uatomic_read((unsigned long *)addr); |
| 110 | do { |
| 111 | old = oldt; |
| 112 | oldt = _uatomic_cmpxchg(addr, old, val, 8); |
| 113 | } while (oldt != old); |
| 114 | |
| 115 | return old; |
| 116 | } |
| 117 | #endif |
| 118 | } |
| 119 | __builtin_trap(); |
| 120 | return 0; |
| 121 | } |
| 122 | |
| 123 | #define uatomic_xchg(addr, v) \ |
| 124 | ((__typeof__(*(addr))) _uatomic_exchange((addr), (unsigned long)(v), \ |
| 125 | sizeof(*(addr)))) |
| 126 | |
| 127 | /* uatomic_add_return */ |
| 128 | |
| 129 | static inline __attribute__((always_inline)) |
| 130 | unsigned long _uatomic_add_return(void *addr, unsigned long val, int len) |
| 131 | { |
| 132 | switch (len) { |
| 133 | case 4: |
| 134 | { |
| 135 | unsigned int old, oldt; |
| 136 | |
| 137 | oldt = uatomic_read((unsigned int *)addr); |
| 138 | do { |
| 139 | old = oldt; |
| 140 | oldt = _uatomic_cmpxchg(addr, old, old + val, 4); |
| 141 | } while (oldt != old); |
| 142 | |
| 143 | return old + val; |
| 144 | } |
| 145 | #if (BITS_PER_LONG == 64) |
| 146 | case 8: |
| 147 | { |
| 148 | unsigned long old, oldt; |
| 149 | |
| 150 | oldt = uatomic_read((unsigned long *)addr); |
| 151 | do { |
| 152 | old = oldt; |
| 153 | oldt = _uatomic_cmpxchg(addr, old, old + val, 8); |
| 154 | } while (oldt != old); |
| 155 | |
| 156 | return old + val; |
| 157 | } |
| 158 | #endif |
| 159 | } |
| 160 | __builtin_trap(); |
| 161 | return 0; |
| 162 | } |
| 163 | |
| 164 | #define uatomic_add_return(addr, v) \ |
| 165 | ((__typeof__(*(addr))) _uatomic_add_return((addr), \ |
| 166 | (unsigned long)(v), \ |
| 167 | sizeof(*(addr)))) |
| 168 | |
| 169 | #ifdef __cplusplus |
| 170 | } |
| 171 | #endif |
| 172 | |
| 173 | #include <urcu/uatomic_generic.h> |
| 174 | |
| 175 | #endif /* _URCU_ARCH_UATOMIC_PPC_H */ |