src/share/vm/runtime/atomic.hpp

changeset 0
f90c822e73f8
child 6876
710a3c8b516e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/share/vm/runtime/atomic.hpp	Wed Apr 27 01:25:04 2016 +0800
     1.3 @@ -0,0 +1,120 @@
     1.4 +/*
     1.5 + * Copyright (c) 1999, 2013, Oracle and/or its affiliates. All rights reserved.
     1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     1.7 + *
     1.8 + * This code is free software; you can redistribute it and/or modify it
     1.9 + * under the terms of the GNU General Public License version 2 only, as
    1.10 + * published by the Free Software Foundation.
    1.11 + *
    1.12 + * This code is distributed in the hope that it will be useful, but WITHOUT
    1.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    1.14 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    1.15 + * version 2 for more details (a copy is included in the LICENSE file that
    1.16 + * accompanied this code).
    1.17 + *
    1.18 + * You should have received a copy of the GNU General Public License version
    1.19 + * 2 along with this work; if not, write to the Free Software Foundation,
    1.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    1.21 + *
    1.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    1.23 + * or visit www.oracle.com if you need additional information or have any
    1.24 + * questions.
    1.25 + *
    1.26 + */
    1.27 +
    1.28 +#ifndef SHARE_VM_RUNTIME_ATOMIC_HPP
    1.29 +#define SHARE_VM_RUNTIME_ATOMIC_HPP
    1.30 +
    1.31 +#include "memory/allocation.hpp"
    1.32 +
    1.33 +class Atomic : AllStatic {
    1.34 + public:
    1.35 +  // Atomic operations on jlong types are not available on all 32-bit
    1.36 +  // platforms. If atomic ops on jlongs are defined here they must only
    1.37 +  // be used from code that verifies they are available at runtime and
    1.38 +  // can provide an alternative action if not - see supports_cx8() for
    1.39 +  // a means to test availability.
    1.40 +
    1.41 +  // Atomically store to a location
    1.42 +  inline static void store    (jbyte    store_value, jbyte*    dest);
    1.43 +  inline static void store    (jshort   store_value, jshort*   dest);
    1.44 +  inline static void store    (jint     store_value, jint*     dest);
    1.45 +  // See comment above about using jlong atomics on 32-bit platforms
    1.46 +  inline static void store    (jlong    store_value, jlong*    dest);
    1.47 +  inline static void store_ptr(intptr_t store_value, intptr_t* dest);
    1.48 +  inline static void store_ptr(void*    store_value, void*     dest);
    1.49 +
    1.50 +  inline static void store    (jbyte    store_value, volatile jbyte*    dest);
    1.51 +  inline static void store    (jshort   store_value, volatile jshort*   dest);
    1.52 +  inline static void store    (jint     store_value, volatile jint*     dest);
    1.53 +  // See comment above about using jlong atomics on 32-bit platforms
    1.54 +  inline static void store    (jlong    store_value, volatile jlong*    dest);
    1.55 +  inline static void store_ptr(intptr_t store_value, volatile intptr_t* dest);
    1.56 +  inline static void store_ptr(void*    store_value, volatile void*     dest);
    1.57 +
    1.58 +  // See comment above about using jlong atomics on 32-bit platforms
    1.59 +  inline static jlong load(volatile jlong* src);
    1.60 +
    1.61 +  // Atomically add to a location, return updated value
    1.62 +  inline static jint     add    (jint     add_value, volatile jint*     dest);
    1.63 +  inline static intptr_t add_ptr(intptr_t add_value, volatile intptr_t* dest);
    1.64 +  inline static void*    add_ptr(intptr_t add_value, volatile void*     dest);
    1.65 +  // See comment above about using jlong atomics on 32-bit platforms
    1.66 +         static jlong    add    (jlong    add_value, volatile jlong*    dest);
    1.67 +
    1.68 +  // Atomically increment location
    1.69 +  inline static void inc    (volatile jint*     dest);
    1.70 +         static void inc    (volatile jshort*   dest);
    1.71 +  inline static void inc_ptr(volatile intptr_t* dest);
    1.72 +  inline static void inc_ptr(volatile void*     dest);
    1.73 +
    1.74 +  // Atomically decrement a location
    1.75 +  inline static void dec    (volatile jint*     dest);
    1.76 +         static void dec    (volatile jshort*    dest);
    1.77 +  inline static void dec_ptr(volatile intptr_t* dest);
    1.78 +  inline static void dec_ptr(volatile void*     dest);
    1.79 +
    1.80 +  // Performs atomic exchange of *dest with exchange_value.  Returns old prior value of *dest.
    1.81 +  inline static jint         xchg(jint         exchange_value, volatile jint*         dest);
    1.82 +         static unsigned int xchg(unsigned int exchange_value, volatile unsigned int* dest);
    1.83 +
    1.84 +  inline static intptr_t xchg_ptr(intptr_t exchange_value, volatile intptr_t* dest);
    1.85 +  inline static void*    xchg_ptr(void*    exchange_value, volatile void*   dest);
    1.86 +
    1.87 +  // Performs atomic compare of *dest and compare_value, and exchanges *dest with exchange_value
    1.88 +  // if the comparison succeeded.  Returns prior value of *dest.  Guarantees a two-way memory
    1.89 +  // barrier across the cmpxchg.  I.e., it's really a 'fence_cmpxchg_acquire'.
    1.90 +         static jbyte    cmpxchg    (jbyte    exchange_value, volatile jbyte*    dest, jbyte    compare_value);
    1.91 +  inline static jint     cmpxchg    (jint     exchange_value, volatile jint*     dest, jint     compare_value);
    1.92 +  // See comment above about using jlong atomics on 32-bit platforms
    1.93 +  inline static jlong    cmpxchg    (jlong    exchange_value, volatile jlong*    dest, jlong    compare_value);
    1.94 +
    1.95 +         static unsigned int cmpxchg(unsigned int exchange_value,
    1.96 +                                     volatile unsigned int* dest,
    1.97 +                                     unsigned int compare_value);
    1.98 +
    1.99 +  inline static intptr_t cmpxchg_ptr(intptr_t exchange_value, volatile intptr_t* dest, intptr_t compare_value);
   1.100 +  inline static void*    cmpxchg_ptr(void*    exchange_value, volatile void*     dest, void*    compare_value);
   1.101 +};
   1.102 +
   1.103 +// To use Atomic::inc(jshort* dest) and Atomic::dec(jshort* dest), the address must be specially
   1.104 +// aligned, such that (*dest) occupies the upper 16 bits of an aligned 32-bit word. The best way to
   1.105 +// achieve is to place your short value next to another short value, which doesn't need atomic ops.
   1.106 +//
   1.107 +// Example
   1.108 +//  ATOMIC_SHORT_PAIR(
   1.109 +//    volatile short _refcount,  // needs atomic operation
   1.110 +//    unsigned short _length     // number of UTF8 characters in the symbol (does not need atomic op)
   1.111 +//  );
   1.112 +
   1.113 +#ifdef VM_LITTLE_ENDIAN
   1.114 +#define ATOMIC_SHORT_PAIR(atomic_decl, non_atomic_decl) \
   1.115 +    non_atomic_decl; \
   1.116 +    atomic_decl
   1.117 +#else
   1.118 +#define ATOMIC_SHORT_PAIR(atomic_decl, non_atomic_decl) \
   1.119 +    atomic_decl ; \
   1.120 +    non_atomic_decl
   1.121 +#endif
   1.122 +
   1.123 +#endif // SHARE_VM_RUNTIME_ATOMIC_HPP

mercurial