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