< prev index next >

src/hotspot/os_cpu/bsd_zero/atomic_bsd_zero.hpp

Print this page
rev 49898 : 8202080: Introduce ordering semantics for Atomic::add and other RMW atomics
Reviewed-by:
   1 /*
   2  * Copyright (c) 2003, 2017, Oracle and/or its affiliates. All rights reserved.
   3  * Copyright 2007, 2008, 2011, 2015, Red Hat, Inc.
   4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   5  *
   6  * This code is free software; you can redistribute it and/or modify it
   7  * under the terms of the GNU General Public License version 2 only, as
   8  * published by the Free Software Foundation.
   9  *
  10  * This code is distributed in the hope that it will be useful, but WITHOUT
  11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  12  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  13  * version 2 for more details (a copy is included in the LICENSE file that
  14  * accompanied this code).
  15  *
  16  * You should have received a copy of the GNU General Public License version
  17  * 2 along with this work; if not, write to the Free Software Foundation,
  18  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  19  *
  20  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  21  * or visit www.oracle.com if you need additional information or have any
  22  * questions.


 147 }
 148 
 149 /* Atomically write VALUE into `*PTR' and returns the previous
 150    contents of `*PTR'.  */
 151 static inline int arm_lock_test_and_set(int newval, volatile int *ptr) {
 152   for (;;) {
 153       // Loop until a __kernel_cmpxchg succeeds.
 154       int prev = *ptr;
 155 
 156       if (__kernel_cmpxchg (prev, newval, ptr) == 0)
 157         return prev;
 158     }
 159 }
 160 #endif // ARM
 161 
 162 template<size_t byte_size>
 163 struct Atomic::PlatformAdd
 164   : Atomic::AddAndFetch<Atomic::PlatformAdd<byte_size> >
 165 {
 166   template<typename I, typename D>
 167   D add_and_fetch(I add_value, D volatile* dest) const;
 168 };
 169 
 170 template<>
 171 template<typename I, typename D>
 172 inline D Atomic::PlatformAdd<4>::add_and_fetch(I add_value, D volatile* dest) const {

 173   STATIC_ASSERT(4 == sizeof(I));
 174   STATIC_ASSERT(4 == sizeof(D));
 175 
 176 #ifdef ARM
 177   return add_using_helper<int>(arm_add_and_fetch, add_value, dest);
 178 #else
 179 #ifdef M68K
 180   return add_using_helper<int>(m68k_add_and_fetch, add_value, dest);
 181 #else
 182   return __sync_add_and_fetch(dest, add_value);
 183 #endif // M68K
 184 #endif // ARM
 185 }
 186 
 187 template<>
 188 template<typename I, typename D>
 189 inline D Atomic::PlatformAdd<8>::add_and_fetch(I add_value, D volatile* dest) const {

 190   STATIC_ASSERT(8 == sizeof(I));
 191   STATIC_ASSERT(8 == sizeof(D));
 192 
 193   return __sync_add_and_fetch(dest, add_value);
 194 }
 195 
 196 template<>
 197 template<typename T>
 198 inline T Atomic::PlatformXchg<4>::operator()(T exchange_value,
 199                                              T volatile* dest) const {

 200   STATIC_ASSERT(4 == sizeof(T));
 201 #ifdef ARM
 202   return xchg_using_helper<int>(arm_lock_test_and_set, exchange_value, dest);
 203 #else
 204 #ifdef M68K
 205   return xchg_using_helper<int>(m68k_lock_test_and_set, exchange_value, dest);
 206 #else
 207   // __sync_lock_test_and_set is a bizarrely named atomic exchange
 208   // operation.  Note that some platforms only support this with the
 209   // limitation that the only valid value to store is the immediate
 210   // constant 1.  There is a test for this in JNI_CreateJavaVM().
 211   T result = __sync_lock_test_and_set (dest, exchange_value);
 212   // All atomic operations are expected to be full memory barriers
 213   // (see atomic.hpp). However, __sync_lock_test_and_set is not
 214   // a full memory barrier, but an acquire barrier. Hence, this added
 215   // barrier.
 216   __sync_synchronize();
 217   return result;
 218 #endif // M68K
 219 #endif // ARM
 220 }
 221 
 222 template<>
 223 template<typename T>
 224 inline T Atomic::PlatformXchg<8>::operator()(T exchange_value,
 225                                              T volatile* dest) const {

 226   STATIC_ASSERT(8 == sizeof(T));
 227   T result = __sync_lock_test_and_set (dest, exchange_value);
 228   __sync_synchronize();
 229   return result;
 230 }
 231 
 232 // No direct support for cmpxchg of bytes; emulate using int.
 233 template<>
 234 struct Atomic::PlatformCmpxchg<1> : Atomic::CmpxchgByteUsingInt {};
 235 
 236 template<>
 237 template<typename T>
 238 inline T Atomic::PlatformCmpxchg<4>::operator()(T exchange_value,
 239                                                 T volatile* dest,
 240                                                 T compare_value,
 241                                                 cmpxchg_memory_order order) const {
 242   STATIC_ASSERT(4 == sizeof(T));
 243 #ifdef ARM
 244   return cmpxchg_using_helper<int>(arm_compare_and_swap, exchange_value, dest, compare_value);
 245 #else
 246 #ifdef M68K
 247   return cmpxchg_using_helper<int>(m68k_compare_and_swap, exchange_value, dest, compare_value);
 248 #else
 249   return __sync_val_compare_and_swap(dest, compare_value, exchange_value);
 250 #endif // M68K
 251 #endif // ARM
 252 }
 253 
 254 template<>
 255 template<typename T>
 256 inline T Atomic::PlatformCmpxchg<8>::operator()(T exchange_value,
 257                                                 T volatile* dest,
 258                                                 T compare_value,
 259                                                 cmpxchg_memory_order order) const {
 260   STATIC_ASSERT(8 == sizeof(T));
 261   return __sync_val_compare_and_swap(dest, compare_value, exchange_value);
 262 }
 263 
 264 template<>
 265 template<typename T>
 266 inline T Atomic::PlatformLoad<8>::operator()(T const volatile* src) const {
 267   STATIC_ASSERT(8 == sizeof(T));
 268   volatile int64_t dest;
 269   os::atomic_copy64(reinterpret_cast<const volatile int64_t*>(src), reinterpret_cast<volatile int64_t*>(&dest));
 270   return PrimitiveConversions::cast<T>(dest);
 271 }
 272 
 273 template<>
 274 template<typename T>
 275 inline void Atomic::PlatformStore<8>::operator()(T store_value,
 276                                                  T volatile* dest) const {
 277   STATIC_ASSERT(8 == sizeof(T));
 278   os::atomic_copy64(reinterpret_cast<const volatile int64_t*>(&store_value), reinterpret_cast<volatile int64_t*>(dest));
 279 }
   1 /*
   2  * Copyright (c) 2003, 2018, Oracle and/or its affiliates. All rights reserved.
   3  * Copyright 2007, 2008, 2011, 2015, Red Hat, Inc.
   4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   5  *
   6  * This code is free software; you can redistribute it and/or modify it
   7  * under the terms of the GNU General Public License version 2 only, as
   8  * published by the Free Software Foundation.
   9  *
  10  * This code is distributed in the hope that it will be useful, but WITHOUT
  11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  12  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  13  * version 2 for more details (a copy is included in the LICENSE file that
  14  * accompanied this code).
  15  *
  16  * You should have received a copy of the GNU General Public License version
  17  * 2 along with this work; if not, write to the Free Software Foundation,
  18  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  19  *
  20  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  21  * or visit www.oracle.com if you need additional information or have any
  22  * questions.


 147 }
 148 
 149 /* Atomically write VALUE into `*PTR' and returns the previous
 150    contents of `*PTR'.  */
 151 static inline int arm_lock_test_and_set(int newval, volatile int *ptr) {
 152   for (;;) {
 153       // Loop until a __kernel_cmpxchg succeeds.
 154       int prev = *ptr;
 155 
 156       if (__kernel_cmpxchg (prev, newval, ptr) == 0)
 157         return prev;
 158     }
 159 }
 160 #endif // ARM
 161 
 162 template<size_t byte_size>
 163 struct Atomic::PlatformAdd
 164   : Atomic::AddAndFetch<Atomic::PlatformAdd<byte_size> >
 165 {
 166   template<typename I, typename D>
 167   D add_and_fetch(I add_value, D volatile* dest, atomic_memory_order order) const;
 168 };
 169 
 170 template<>
 171 template<typename I, typename D>
 172 inline D Atomic::PlatformAdd<4>::add_and_fetch(I add_value, D volatile* dest,
 173                                                atomic_memory_order order) const {
 174   STATIC_ASSERT(4 == sizeof(I));
 175   STATIC_ASSERT(4 == sizeof(D));
 176 
 177 #ifdef ARM
 178   return add_using_helper<int>(arm_add_and_fetch, add_value, dest);
 179 #else
 180 #ifdef M68K
 181   return add_using_helper<int>(m68k_add_and_fetch, add_value, dest);
 182 #else
 183   return __sync_add_and_fetch(dest, add_value);
 184 #endif // M68K
 185 #endif // ARM
 186 }
 187 
 188 template<>
 189 template<typename I, typename D>
 190 inline D Atomic::PlatformAdd<8>::add_and_fetch(I add_value, D volatile* dest,
 191                                                atomic_memory_order order) const {
 192   STATIC_ASSERT(8 == sizeof(I));
 193   STATIC_ASSERT(8 == sizeof(D));
 194 
 195   return __sync_add_and_fetch(dest, add_value);
 196 }
 197 
 198 template<>
 199 template<typename T>
 200 inline T Atomic::PlatformXchg<4>::operator()(T exchange_value,
 201                                              T volatile* dest,
 202                                              atomic_memory_order order) const {
 203   STATIC_ASSERT(4 == sizeof(T));
 204 #ifdef ARM
 205   return xchg_using_helper<int>(arm_lock_test_and_set, exchange_value, dest);
 206 #else
 207 #ifdef M68K
 208   return xchg_using_helper<int>(m68k_lock_test_and_set, exchange_value, dest);
 209 #else
 210   // __sync_lock_test_and_set is a bizarrely named atomic exchange
 211   // operation.  Note that some platforms only support this with the
 212   // limitation that the only valid value to store is the immediate
 213   // constant 1.  There is a test for this in JNI_CreateJavaVM().
 214   T result = __sync_lock_test_and_set (dest, exchange_value);
 215   // All atomic operations are expected to be full memory barriers
 216   // (see atomic.hpp). However, __sync_lock_test_and_set is not
 217   // a full memory barrier, but an acquire barrier. Hence, this added
 218   // barrier.
 219   __sync_synchronize();
 220   return result;
 221 #endif // M68K
 222 #endif // ARM
 223 }
 224 
 225 template<>
 226 template<typename T>
 227 inline T Atomic::PlatformXchg<8>::operator()(T exchange_value,
 228                                              T volatile* dest,
 229                                              atomic_memory_order order) const {
 230   STATIC_ASSERT(8 == sizeof(T));
 231   T result = __sync_lock_test_and_set (dest, exchange_value);
 232   __sync_synchronize();
 233   return result;
 234 }
 235 
 236 // No direct support for cmpxchg of bytes; emulate using int.
 237 template<>
 238 struct Atomic::PlatformCmpxchg<1> : Atomic::CmpxchgByteUsingInt {};
 239 
 240 template<>
 241 template<typename T>
 242 inline T Atomic::PlatformCmpxchg<4>::operator()(T exchange_value,
 243                                                 T volatile* dest,
 244                                                 T compare_value,
 245                                                 atomic_memory_order order) const {
 246   STATIC_ASSERT(4 == sizeof(T));
 247 #ifdef ARM
 248   return cmpxchg_using_helper<int>(arm_compare_and_swap, exchange_value, dest, compare_value);
 249 #else
 250 #ifdef M68K
 251   return cmpxchg_using_helper<int>(m68k_compare_and_swap, exchange_value, dest, compare_value);
 252 #else
 253   return __sync_val_compare_and_swap(dest, compare_value, exchange_value);
 254 #endif // M68K
 255 #endif // ARM
 256 }
 257 
 258 template<>
 259 template<typename T>
 260 inline T Atomic::PlatformCmpxchg<8>::operator()(T exchange_value,
 261                                                 T volatile* dest,
 262                                                 T compare_value,
 263                                                 atomic_memory_order order) const {
 264   STATIC_ASSERT(8 == sizeof(T));
 265   return __sync_val_compare_and_swap(dest, compare_value, exchange_value);
 266 }
 267 
 268 template<>
 269 template<typename T>
 270 inline T Atomic::PlatformLoad<8>::operator()(T const volatile* src) const {
 271   STATIC_ASSERT(8 == sizeof(T));
 272   volatile int64_t dest;
 273   os::atomic_copy64(reinterpret_cast<const volatile int64_t*>(src), reinterpret_cast<volatile int64_t*>(&dest));
 274   return PrimitiveConversions::cast<T>(dest);
 275 }
 276 
 277 template<>
 278 template<typename T>
 279 inline void Atomic::PlatformStore<8>::operator()(T store_value,
 280                                                  T volatile* dest) const {
 281   STATIC_ASSERT(8 == sizeof(T));
 282   os::atomic_copy64(reinterpret_cast<const volatile int64_t*>(&store_value), reinterpret_cast<volatile int64_t*>(dest));
 283 }
< prev index next >