duke@435: /* stefank@2314: * Copyright (c) 1999, 2010, Oracle and/or its affiliates. All rights reserved. duke@435: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. duke@435: * duke@435: * This code is free software; you can redistribute it and/or modify it duke@435: * under the terms of the GNU General Public License version 2 only, as duke@435: * published by the Free Software Foundation. duke@435: * duke@435: * This code is distributed in the hope that it will be useful, but WITHOUT duke@435: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or duke@435: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License duke@435: * version 2 for more details (a copy is included in the LICENSE file that duke@435: * accompanied this code). duke@435: * duke@435: * You should have received a copy of the GNU General Public License version duke@435: * 2 along with this work; if not, write to the Free Software Foundation, duke@435: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. duke@435: * trims@1907: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA trims@1907: * or visit www.oracle.com if you need additional information or have any trims@1907: * questions. duke@435: * duke@435: */ duke@435: stefank@2314: #ifndef SHARE_VM_RUNTIME_ATOMIC_HPP stefank@2314: #define SHARE_VM_RUNTIME_ATOMIC_HPP stefank@2314: stefank@2314: #include "memory/allocation.hpp" stefank@2314: duke@435: class Atomic : AllStatic { duke@435: public: jwilhelm@4647: // Atomic operations on jlong types are not available on all 32-bit jwilhelm@4647: // platforms. If atomic ops on jlongs are defined here they must only jwilhelm@4647: // be used from code that verifies they are available at runtime and jwilhelm@4647: // can provide an alternative action if not - see supports_cx8() for jwilhelm@4647: // a means to test availability. jwilhelm@4647: duke@435: // Atomically store to a location twisti@4318: inline static void store (jbyte store_value, jbyte* dest); twisti@4318: inline static void store (jshort store_value, jshort* dest); twisti@4318: inline static void store (jint store_value, jint* dest); jwilhelm@4647: // See comment above about using jlong atomics on 32-bit platforms twisti@4318: inline static void store (jlong store_value, jlong* dest); twisti@4318: inline static void store_ptr(intptr_t store_value, intptr_t* dest); twisti@4318: inline static void store_ptr(void* store_value, void* dest); duke@435: twisti@4318: inline static void store (jbyte store_value, volatile jbyte* dest); twisti@4318: inline static void store (jshort store_value, volatile jshort* dest); twisti@4318: inline static void store (jint store_value, volatile jint* dest); jwilhelm@4647: // See comment above about using jlong atomics on 32-bit platforms twisti@4318: inline static void store (jlong store_value, volatile jlong* dest); twisti@4318: inline static void store_ptr(intptr_t store_value, volatile intptr_t* dest); twisti@4318: inline static void store_ptr(void* store_value, volatile void* dest); duke@435: jwilhelm@4647: // See comment above about using jlong atomics on 32-bit platforms twisti@4318: inline static jlong load(volatile jlong* src); kvn@1329: duke@435: // Atomically add to a location, return updated value twisti@4318: inline static jint add (jint add_value, volatile jint* dest); twisti@4318: inline static intptr_t add_ptr(intptr_t add_value, volatile intptr_t* dest); twisti@4318: inline static void* add_ptr(intptr_t add_value, volatile void* dest); jwilhelm@4647: // See comment above about using jlong atomics on 32-bit platforms twisti@4318: static jlong add (jlong add_value, volatile jlong* dest); minqi@2964: duke@435: // Atomically increment location twisti@4318: inline static void inc (volatile jint* dest); iklam@5306: static void inc (volatile jshort* dest); twisti@4318: inline static void inc_ptr(volatile intptr_t* dest); twisti@4318: inline static void inc_ptr(volatile void* dest); duke@435: duke@435: // Atomically decrement a location twisti@4318: inline static void dec (volatile jint* dest); iklam@5306: static void dec (volatile jshort* dest); twisti@4318: inline static void dec_ptr(volatile intptr_t* dest); twisti@4318: inline static void dec_ptr(volatile void* dest); duke@435: duke@435: // Performs atomic exchange of *dest with exchange_value. Returns old prior value of *dest. twisti@4318: inline static jint xchg(jint exchange_value, volatile jint* dest); twisti@4318: static unsigned int xchg(unsigned int exchange_value, volatile unsigned int* dest); coleenp@548: twisti@4318: inline static intptr_t xchg_ptr(intptr_t exchange_value, volatile intptr_t* dest); twisti@4318: inline static void* xchg_ptr(void* exchange_value, volatile void* dest); duke@435: duke@435: // Performs atomic compare of *dest and compare_value, and exchanges *dest with exchange_value duke@435: // if the comparison succeeded. Returns prior value of *dest. Guarantees a two-way memory duke@435: // barrier across the cmpxchg. I.e., it's really a 'fence_cmpxchg_acquire'. twisti@4318: static jbyte cmpxchg (jbyte exchange_value, volatile jbyte* dest, jbyte compare_value); twisti@4318: inline static jint cmpxchg (jint exchange_value, volatile jint* dest, jint compare_value); jwilhelm@4647: // See comment above about using jlong atomics on 32-bit platforms twisti@4318: inline static jlong cmpxchg (jlong exchange_value, volatile jlong* dest, jlong compare_value); coleenp@548: twisti@4318: static unsigned int cmpxchg(unsigned int exchange_value, twisti@4318: volatile unsigned int* dest, twisti@4318: unsigned int compare_value); coleenp@548: twisti@4318: inline static intptr_t cmpxchg_ptr(intptr_t exchange_value, volatile intptr_t* dest, intptr_t compare_value); twisti@4318: inline static void* cmpxchg_ptr(void* exchange_value, volatile void* dest, void* compare_value); duke@435: }; stefank@2314: iklam@5306: // To use Atomic::inc(jshort* dest) and Atomic::dec(jshort* dest), the address must be specially iklam@5306: // aligned, such that (*dest) occupies the upper 16 bits of an aligned 32-bit word. The best way to iklam@5306: // achieve is to place your short value next to another short value, which doesn't need atomic ops. iklam@5306: // iklam@5306: // Example iklam@5306: // ATOMIC_SHORT_PAIR( iklam@5306: // volatile short _refcount, // needs atomic operation iklam@5306: // unsigned short _length // number of UTF8 characters in the symbol (does not need atomic op) iklam@5306: // ); iklam@5306: iklam@5306: #ifdef VM_LITTLE_ENDIAN iklam@5306: #define ATOMIC_SHORT_PAIR(atomic_decl, non_atomic_decl) \ iklam@5306: non_atomic_decl; \ iklam@5306: atomic_decl iklam@5306: #else iklam@5306: #define ATOMIC_SHORT_PAIR(atomic_decl, non_atomic_decl) \ iklam@5306: atomic_decl ; \ iklam@5306: non_atomic_decl iklam@5306: #endif iklam@5306: stefank@2314: #endif // SHARE_VM_RUNTIME_ATOMIC_HPP