1 /*
   2  * Copyright (c) 1999, 2018, 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_VM_ATOMIC_SOLARIS_SPARC_HPP
  26 #define OS_CPU_SOLARIS_SPARC_VM_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 I, typename D>
  34   inline D operator()(I add_value, D volatile* dest, 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(new_value, dest, old_value);
  39       if (result == old_value) break;
  40       old_value = result;
  41     }
  42     return old_value + add_value;
  43   }
  44 };
  45 
  46 template<>
  47 template<typename T>
  48 inline T Atomic::PlatformXchg<4>::operator()(T exchange_value,
  49                                              T volatile* dest,
  50                                              atomic_memory_order order) const {
  51   STATIC_ASSERT(4 == sizeof(T));
  52   __asm__ volatile (  "swap [%2],%0"
  53                     : "=r" (exchange_value)
  54                     : "0" (exchange_value), "r" (dest)
  55                     : "memory");
  56   return exchange_value;
  57 }
  58 
  59 template<>
  60 template<typename T>
  61 inline T Atomic::PlatformXchg<8>::operator()(T exchange_value,
  62                                              T volatile* dest,
  63                                              atomic_memory_order order) const {
  64   STATIC_ASSERT(8 == sizeof(T));
  65   T old_value = *dest;
  66   while (true) {
  67     T result = cmpxchg(exchange_value, dest, old_value);
  68     if (result == old_value) break;
  69     old_value = result;
  70   }
  71   return old_value;
  72 }
  73 
  74 // No direct support for cmpxchg of bytes; emulate using int.
  75 template<>
  76 struct Atomic::PlatformCmpxchg<1> : Atomic::CmpxchgByteUsingInt {};
  77 
  78 template<>
  79 template<typename T>
  80 inline T Atomic::PlatformCmpxchg<4>::operator()(T exchange_value,
  81                                                 T volatile* dest,
  82                                                 T compare_value,
  83                                                 atomic_memory_order order) const {
  84   STATIC_ASSERT(4 == sizeof(T));
  85   T rv;
  86   __asm__ volatile(
  87     " cas    [%2], %3, %0"
  88     : "=r" (rv)
  89     : "0" (exchange_value), "r" (dest), "r" (compare_value)
  90     : "memory");
  91   return rv;
  92 }
  93 
  94 template<>
  95 template<typename T>
  96 inline T Atomic::PlatformCmpxchg<8>::operator()(T exchange_value,
  97                                                 T volatile* dest,
  98                                                 T compare_value,
  99                                                 atomic_memory_order order) const {
 100   STATIC_ASSERT(8 == sizeof(T));
 101   T rv;
 102   __asm__ volatile(
 103     " casx   [%2], %3, %0"
 104     : "=r" (rv)
 105     : "0" (exchange_value), "r" (dest), "r" (compare_value)
 106     : "memory");
 107   return rv;
 108 }
 109 
 110 #endif // OS_CPU_SOLARIS_SPARC_VM_ATOMIC_SOLARIS_SPARC_HPP