1 /* 2 * Copyright (c) 1999, 2019, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 * 23 */ 24 25 #ifndef OS_CPU_SOLARIS_SPARC_ATOMIC_SOLARIS_SPARC_HPP 26 #define OS_CPU_SOLARIS_SPARC_ATOMIC_SOLARIS_SPARC_HPP 27 28 // Implementation of class atomic 29 30 // Implement ADD using a CAS loop. 31 template<size_t byte_size> 32 struct Atomic::PlatformAdd { 33 template<typename D, typename I> 34 inline D add_and_fetch(D volatile* dest, I add_value, atomic_memory_order order) const { 35 D old_value = *dest; 36 while (true) { 37 D new_value = old_value + add_value; 38 D result = cmpxchg(dest, old_value, new_value); 39 if (result == old_value) break; 40 old_value = result; 41 } 42 return old_value + add_value; 43 } 44 45 template<typename D, typename I> 46 inline D fetch_and_add(D volatile* dest, I add_value, atomic_memory_order order) const { 47 return add_and_fetch(dest, add_value, order) - add_value; 48 } 49 }; 50 51 template<> 52 template<typename T> 53 inline T Atomic::PlatformXchg<4>::operator()(T volatile* dest, 54 T exchange_value, 55 atomic_memory_order order) const { 56 STATIC_ASSERT(4 == sizeof(T)); 57 __asm__ volatile ( "swap [%2],%0" 58 : "=r" (exchange_value) 59 : "0" (exchange_value), "r" (dest) 60 : "memory"); 61 return exchange_value; 62 } 63 64 template<> 65 template<typename T> 66 inline T Atomic::PlatformXchg<8>::operator()(T volatile* dest, 67 T exchange_value, 68 atomic_memory_order order) const { 69 STATIC_ASSERT(8 == sizeof(T)); 70 T old_value = *dest; 71 while (true) { 72 T result = cmpxchg(dest, old_value, exchange_value); 73 if (result == old_value) break; 74 old_value = result; 75 } 76 return old_value; 77 } 78 79 // No direct support for cmpxchg of bytes; emulate using int. 80 template<> 81 struct Atomic::PlatformCmpxchg<1> : Atomic::CmpxchgByteUsingInt {}; 82 83 template<> 84 template<typename T> 85 inline T Atomic::PlatformCmpxchg<4>::operator()(T volatile* dest, 86 T compare_value, 87 T exchange_value, 88 atomic_memory_order order) const { 89 STATIC_ASSERT(4 == sizeof(T)); 90 T rv; 91 __asm__ volatile( 92 " cas [%2], %3, %0" 93 : "=r" (rv) 94 : "0" (exchange_value), "r" (dest), "r" (compare_value) 95 : "memory"); 96 return rv; 97 } 98 99 template<> 100 template<typename T> 101 inline T Atomic::PlatformCmpxchg<8>::operator()(T volatile* dest, 102 T compare_value, 103 T exchange_value, 104 atomic_memory_order order) const { 105 STATIC_ASSERT(8 == sizeof(T)); 106 T rv; 107 __asm__ volatile( 108 " casx [%2], %3, %0" 109 : "=r" (rv) 110 : "0" (exchange_value), "r" (dest), "r" (compare_value) 111 : "memory"); 112 return rv; 113 } 114 115 #endif // OS_CPU_SOLARIS_SPARC_ATOMIC_SOLARIS_SPARC_HPP