1 /* 2 * Copyright (c) 2012, 2016, 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 SHARE_VM_RUNTIME_ATOMIC_INLINE_HPP 26 #define SHARE_VM_RUNTIME_ATOMIC_INLINE_HPP 27 28 #include "runtime/atomic.hpp" 29 #include "utilities/macros.hpp" 30 31 #include OS_CPU_HEADER_INLINE(atomic) 32 33 // size_t casts... 34 #if (SIZE_MAX != UINTPTR_MAX) 35 #error size_t is not WORD_SIZE, interesting platform, but missing implementation here 36 #endif 37 38 inline size_t Atomic::add(size_t add_value, volatile size_t* dest) { 39 return (size_t) add_ptr((intptr_t) add_value, (volatile intptr_t*) dest); 40 } 41 42 inline void Atomic::inc(volatile size_t* dest) { 43 inc_ptr((volatile intptr_t*) dest); 44 } 45 46 inline void Atomic::dec(volatile size_t* dest) { 47 dec_ptr((volatile intptr_t*) dest); 48 } 49 50 #ifndef VM_HAS_SPECIALIZED_CMPXCHG_BYTE 51 /* 52 * This is the default implementation of byte-sized cmpxchg. It emulates jbyte-sized cmpxchg 53 * in terms of jint-sized cmpxchg. Platforms may override this by defining their own inline definition 54 * as well as defining VM_HAS_SPECIALIZED_CMPXCHG_BYTE. This will cause the platform specific 55 * implementation to be used instead. 56 */ 57 inline jbyte Atomic::cmpxchg(jbyte exchange_value, volatile jbyte *dest, jbyte comparand, cmpxchg_memory_order order) 58 { 59 assert(sizeof(jbyte) == 1, "assumption."); 60 uintptr_t dest_addr = (uintptr_t)dest; 61 uintptr_t offset = dest_addr % sizeof(jint); 62 volatile jint* dest_int = (volatile jint*)(dest_addr - offset); 63 jint cur = *dest_int; 64 jbyte* cur_as_bytes = (jbyte*)(&cur); 65 jint new_val = cur; 66 jbyte* new_val_as_bytes = (jbyte*)(&new_val); 67 new_val_as_bytes[offset] = exchange_value; 68 while (cur_as_bytes[offset] == comparand) { 69 jint res = cmpxchg(new_val, dest_int, cur, order); 70 if (res == cur) break; 71 cur = res; 72 new_val = cur; 73 new_val_as_bytes[offset] = exchange_value; 74 } 75 return cur_as_bytes[offset]; 76 } 77 #endif // VM_HAS_SPECIALIZED_CMPXCHG_BYTE 78 79 inline unsigned Atomic::xchg(unsigned int exchange_value, volatile unsigned int* dest) { 80 assert(sizeof(unsigned int) == sizeof(jint), "more work to do"); 81 return (unsigned int)Atomic::xchg((jint)exchange_value, (volatile jint*)dest); 82 } 83 84 inline unsigned Atomic::cmpxchg(unsigned int exchange_value, 85 volatile unsigned int* dest, unsigned int compare_value, 86 cmpxchg_memory_order order) { 87 assert(sizeof(unsigned int) == sizeof(jint), "more work to do"); 88 return (unsigned int)Atomic::cmpxchg((jint)exchange_value, (volatile jint*)dest, 89 (jint)compare_value, order); 90 } 91 92 inline jlong Atomic::add(jlong add_value, volatile jlong* dest) { 93 jlong old = load(dest); 94 jlong new_value = old + add_value; 95 while (old != cmpxchg(new_value, dest, old)) { 96 old = load(dest); 97 new_value = old + add_value; 98 } 99 return old; 100 } 101 102 inline void Atomic::inc(volatile short* dest) { 103 // Most platforms do not support atomic increment on a 2-byte value. However, 104 // if the value occupies the most significant 16 bits of an aligned 32-bit 105 // word, then we can do this with an atomic add of 0x10000 to the 32-bit word. 106 // 107 // The least significant parts of this 32-bit word will never be affected, even 108 // in case of overflow/underflow. 109 // 110 // Use the ATOMIC_SHORT_PAIR macro to get the desired alignment. 111 #ifdef VM_LITTLE_ENDIAN 112 assert((intx(dest) & 0x03) == 0x02, "wrong alignment"); 113 (void)Atomic::add(0x10000, (volatile int*)(dest-1)); 114 #else 115 assert((intx(dest) & 0x03) == 0x00, "wrong alignment"); 116 (void)Atomic::add(0x10000, (volatile int*)(dest)); 117 #endif 118 } 119 120 inline void Atomic::dec(volatile short* dest) { 121 #ifdef VM_LITTLE_ENDIAN 122 assert((intx(dest) & 0x03) == 0x02, "wrong alignment"); 123 (void)Atomic::add(-0x10000, (volatile int*)(dest-1)); 124 #else 125 assert((intx(dest) & 0x03) == 0x00, "wrong alignment"); 126 (void)Atomic::add(-0x10000, (volatile int*)(dest)); 127 #endif 128 } 129 130 #endif // SHARE_VM_RUNTIME_ATOMIC_INLINE_HPP