src/share/vm/runtime/atomic.cpp

Sat, 07 Nov 2020 10:30:02 +0800

author
aoqi
date
Sat, 07 Nov 2020 10:30:02 +0800
changeset 10026
8c95980d0b66
parent 6876
710a3c8b516e
permissions
-rw-r--r--

Added tag mips-jdk8u275-b01 for changeset d3b4d62f391f

aoqi@0 1 /*
aoqi@0 2 * Copyright (c) 2001, 2013, Oracle and/or its affiliates. All rights reserved.
aoqi@0 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
aoqi@0 4 *
aoqi@0 5 * This code is free software; you can redistribute it and/or modify it
aoqi@0 6 * under the terms of the GNU General Public License version 2 only, as
aoqi@0 7 * published by the Free Software Foundation.
aoqi@0 8 *
aoqi@0 9 * This code is distributed in the hope that it will be useful, but WITHOUT
aoqi@0 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
aoqi@0 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
aoqi@0 12 * version 2 for more details (a copy is included in the LICENSE file that
aoqi@0 13 * accompanied this code).
aoqi@0 14 *
aoqi@0 15 * You should have received a copy of the GNU General Public License version
aoqi@0 16 * 2 along with this work; if not, write to the Free Software Foundation,
aoqi@0 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
aoqi@0 18 *
aoqi@0 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
aoqi@0 20 * or visit www.oracle.com if you need additional information or have any
aoqi@0 21 * questions.
aoqi@0 22 *
aoqi@0 23 */
aoqi@0 24
aoqi@0 25 #include "precompiled.hpp"
aoqi@0 26 #include "runtime/atomic.hpp"
aoqi@0 27 #ifdef TARGET_OS_FAMILY_linux
aoqi@0 28 # include "os_linux.inline.hpp"
aoqi@0 29 #endif
aoqi@0 30 #ifdef TARGET_OS_FAMILY_solaris
aoqi@0 31 # include "os_solaris.inline.hpp"
aoqi@0 32 #endif
aoqi@0 33 #ifdef TARGET_OS_FAMILY_windows
aoqi@0 34 # include "os_windows.inline.hpp"
aoqi@0 35 #endif
aoqi@0 36 #ifdef TARGET_OS_FAMILY_aix
aoqi@0 37 # include "os_aix.inline.hpp"
aoqi@0 38 #endif
aoqi@0 39 #ifdef TARGET_OS_FAMILY_bsd
aoqi@0 40 # include "os_bsd.inline.hpp"
aoqi@0 41 #endif
aoqi@0 42
aoqi@0 43 #include "runtime/atomic.inline.hpp"
aoqi@0 44
aoqi@0 45 jbyte Atomic::cmpxchg(jbyte exchange_value, volatile jbyte* dest, jbyte compare_value) {
aoqi@0 46 assert(sizeof(jbyte) == 1, "assumption.");
aoqi@0 47 uintptr_t dest_addr = (uintptr_t)dest;
aoqi@0 48 uintptr_t offset = dest_addr % sizeof(jint);
aoqi@0 49 volatile jint* dest_int = (volatile jint*)(dest_addr - offset);
aoqi@0 50 jint cur = *dest_int;
aoqi@0 51 jbyte* cur_as_bytes = (jbyte*)(&cur);
aoqi@0 52 jint new_val = cur;
aoqi@0 53 jbyte* new_val_as_bytes = (jbyte*)(&new_val);
aoqi@0 54 new_val_as_bytes[offset] = exchange_value;
aoqi@0 55 while (cur_as_bytes[offset] == compare_value) {
aoqi@0 56 jint res = cmpxchg(new_val, dest_int, cur);
aoqi@0 57 if (res == cur) break;
aoqi@0 58 cur = res;
aoqi@0 59 new_val = cur;
aoqi@0 60 new_val_as_bytes[offset] = exchange_value;
aoqi@0 61 }
aoqi@0 62 return cur_as_bytes[offset];
aoqi@0 63 }
aoqi@0 64
aoqi@0 65 unsigned Atomic::xchg(unsigned int exchange_value, volatile unsigned int* dest) {
aoqi@0 66 assert(sizeof(unsigned int) == sizeof(jint), "more work to do");
aoqi@0 67 return (unsigned int)Atomic::xchg((jint)exchange_value, (volatile jint*)dest);
aoqi@0 68 }
aoqi@0 69
aoqi@0 70 unsigned Atomic::cmpxchg(unsigned int exchange_value,
aoqi@0 71 volatile unsigned int* dest, unsigned int compare_value) {
aoqi@0 72 assert(sizeof(unsigned int) == sizeof(jint), "more work to do");
aoqi@0 73 return (unsigned int)Atomic::cmpxchg((jint)exchange_value, (volatile jint*)dest,
aoqi@0 74 (jint)compare_value);
aoqi@0 75 }
aoqi@0 76
aoqi@0 77 jlong Atomic::add(jlong add_value, volatile jlong* dest) {
aoqi@0 78 jlong old = load(dest);
aoqi@0 79 jlong new_value = old + add_value;
aoqi@0 80 while (old != cmpxchg(new_value, dest, old)) {
aoqi@0 81 old = load(dest);
aoqi@0 82 new_value = old + add_value;
aoqi@0 83 }
aoqi@0 84 return old;
aoqi@0 85 }
aoqi@0 86
aoqi@0 87 void Atomic::inc(volatile short* dest) {
aoqi@0 88 // Most platforms do not support atomic increment on a 2-byte value. However,
aoqi@0 89 // if the value occupies the most significant 16 bits of an aligned 32-bit
aoqi@0 90 // word, then we can do this with an atomic add of 0x10000 to the 32-bit word.
aoqi@0 91 //
aoqi@0 92 // The least significant parts of this 32-bit word will never be affected, even
aoqi@0 93 // in case of overflow/underflow.
aoqi@0 94 //
aoqi@0 95 // Use the ATOMIC_SHORT_PAIR macro to get the desired alignment.
aoqi@0 96 #ifdef VM_LITTLE_ENDIAN
aoqi@0 97 assert((intx(dest) & 0x03) == 0x02, "wrong alignment");
aoqi@0 98 (void)Atomic::add(0x10000, (volatile int*)(dest-1));
aoqi@0 99 #else
aoqi@0 100 assert((intx(dest) & 0x03) == 0x00, "wrong alignment");
aoqi@0 101 (void)Atomic::add(0x10000, (volatile int*)(dest));
aoqi@0 102 #endif
aoqi@0 103 }
aoqi@0 104
aoqi@0 105 void Atomic::dec(volatile short* dest) {
aoqi@0 106 #ifdef VM_LITTLE_ENDIAN
aoqi@0 107 assert((intx(dest) & 0x03) == 0x02, "wrong alignment");
aoqi@0 108 (void)Atomic::add(-0x10000, (volatile int*)(dest-1));
aoqi@0 109 #else
aoqi@0 110 assert((intx(dest) & 0x03) == 0x00, "wrong alignment");
aoqi@0 111 (void)Atomic::add(-0x10000, (volatile int*)(dest));
aoqi@0 112 #endif
aoqi@0 113 }
aoqi@0 114

mercurial