Path: blob/aarch64-shenandoah-jdk8u272-b10/hotspot/src/share/vm/runtime/atomic.cpp
32285 views
/*1* Copyright (c) 2001, 2013, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*22*/2324#include "precompiled.hpp"25#include "runtime/atomic.hpp"26#ifdef TARGET_OS_FAMILY_linux27# include "os_linux.inline.hpp"28#endif29#ifdef TARGET_OS_FAMILY_solaris30# include "os_solaris.inline.hpp"31#endif32#ifdef TARGET_OS_FAMILY_windows33# include "os_windows.inline.hpp"34#endif35#ifdef TARGET_OS_FAMILY_aix36# include "os_aix.inline.hpp"37#endif38#ifdef TARGET_OS_FAMILY_bsd39# include "os_bsd.inline.hpp"40#endif4142#include "runtime/atomic.inline.hpp"4344jbyte Atomic::cmpxchg(jbyte exchange_value, volatile jbyte* dest, jbyte compare_value) {45assert(sizeof(jbyte) == 1, "assumption.");46uintptr_t dest_addr = (uintptr_t)dest;47uintptr_t offset = dest_addr % sizeof(jint);48volatile jint* dest_int = (volatile jint*)(dest_addr - offset);49jint cur = *dest_int;50jbyte* cur_as_bytes = (jbyte*)(&cur);51jint new_val = cur;52jbyte* new_val_as_bytes = (jbyte*)(&new_val);53new_val_as_bytes[offset] = exchange_value;54while (cur_as_bytes[offset] == compare_value) {55jint res = cmpxchg(new_val, dest_int, cur);56if (res == cur) break;57cur = res;58new_val = cur;59new_val_as_bytes[offset] = exchange_value;60}61return cur_as_bytes[offset];62}6364unsigned Atomic::xchg(unsigned int exchange_value, volatile unsigned int* dest) {65assert(sizeof(unsigned int) == sizeof(jint), "more work to do");66return (unsigned int)Atomic::xchg((jint)exchange_value, (volatile jint*)dest);67}6869unsigned Atomic::cmpxchg(unsigned int exchange_value,70volatile unsigned int* dest, unsigned int compare_value) {71assert(sizeof(unsigned int) == sizeof(jint), "more work to do");72return (unsigned int)Atomic::cmpxchg((jint)exchange_value, (volatile jint*)dest,73(jint)compare_value);74}7576jlong Atomic::add(jlong add_value, volatile jlong* dest) {77jlong old = load(dest);78jlong new_value = old + add_value;79while (old != cmpxchg(new_value, dest, old)) {80old = load(dest);81new_value = old + add_value;82}83return old;84}8586void Atomic::inc(volatile short* dest) {87// Most platforms do not support atomic increment on a 2-byte value. However,88// if the value occupies the most significant 16 bits of an aligned 32-bit89// word, then we can do this with an atomic add of 0x10000 to the 32-bit word.90//91// The least significant parts of this 32-bit word will never be affected, even92// in case of overflow/underflow.93//94// Use the ATOMIC_SHORT_PAIR macro to get the desired alignment.95#ifdef VM_LITTLE_ENDIAN96assert((intx(dest) & 0x03) == 0x02, "wrong alignment");97(void)Atomic::add(0x10000, (volatile int*)(dest-1));98#else99assert((intx(dest) & 0x03) == 0x00, "wrong alignment");100(void)Atomic::add(0x10000, (volatile int*)(dest));101#endif102}103104void Atomic::dec(volatile short* dest) {105#ifdef VM_LITTLE_ENDIAN106assert((intx(dest) & 0x03) == 0x02, "wrong alignment");107(void)Atomic::add(-0x10000, (volatile int*)(dest-1));108#else109assert((intx(dest) & 0x03) == 0x00, "wrong alignment");110(void)Atomic::add(-0x10000, (volatile int*)(dest));111#endif112}113114115116