1/*
2 * Copyright (c) 1999, 2017, 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 OS_CPU_LINUX_SPARC_VM_ATOMIC_LINUX_SPARC_INLINE_HPP
26#define OS_CPU_LINUX_SPARC_VM_ATOMIC_LINUX_SPARC_INLINE_HPP
27
28// Implementation of class atomic
29
30inline void Atomic::store    (jbyte    store_value, jbyte*    dest) { *dest = store_value; }
31inline void Atomic::store    (jshort   store_value, jshort*   dest) { *dest = store_value; }
32inline void Atomic::store    (jint     store_value, jint*     dest) { *dest = store_value; }
33inline void Atomic::store    (jlong    store_value, jlong*    dest) { *dest = store_value; }
34inline void Atomic::store_ptr(intptr_t store_value, intptr_t* dest) { *dest = store_value; }
35inline void Atomic::store_ptr(void*    store_value, void*     dest) { *(void**)dest = store_value; }
36
37inline void Atomic::store    (jbyte    store_value, volatile jbyte*    dest) { *dest = store_value; }
38inline void Atomic::store    (jshort   store_value, volatile jshort*   dest) { *dest = store_value; }
39inline void Atomic::store    (jint     store_value, volatile jint*     dest) { *dest = store_value; }
40inline void Atomic::store    (jlong    store_value, volatile jlong*    dest) { *dest = store_value; }
41inline void Atomic::store_ptr(intptr_t store_value, volatile intptr_t* dest) { *dest = store_value; }
42inline void Atomic::store_ptr(void*    store_value, volatile void*     dest) { *(void* volatile *)dest = store_value; }
43
44inline void Atomic::inc    (volatile jint*     dest) { (void)add    (1, dest); }
45inline void Atomic::inc_ptr(volatile intptr_t* dest) { (void)add_ptr(1, dest); }
46inline void Atomic::inc_ptr(volatile void*     dest) { (void)add_ptr(1, dest); }
47
48inline void Atomic::dec    (volatile jint*     dest) { (void)add    (-1, dest); }
49inline void Atomic::dec_ptr(volatile intptr_t* dest) { (void)add_ptr(-1, dest); }
50inline void Atomic::dec_ptr(volatile void*     dest) { (void)add_ptr(-1, dest); }
51
52inline jlong Atomic::load(const volatile jlong* src) { return *src; }
53
54template<size_t byte_size>
55struct Atomic::PlatformAdd
56  : Atomic::AddAndFetch<Atomic::PlatformAdd<byte_size> >
57{
58  template<typename I, typename D>
59  D add_and_fetch(I add_value, D volatile* dest) const;
60};
61
62template<>
63template<typename I, typename D>
64inline D Atomic::PlatformAdd<4>::add_and_fetch(I add_value, D volatile* dest) const {
65  STATIC_ASSERT(4 == sizeof(I));
66  STATIC_ASSERT(4 == sizeof(D));
67
68  D rv;
69  __asm__ volatile(
70    "1: \n\t"
71    " ld     [%2], %%o2\n\t"
72    " add    %1, %%o2, %%o3\n\t"
73    " cas    [%2], %%o2, %%o3\n\t"
74    " cmp    %%o2, %%o3\n\t"
75    " bne    1b\n\t"
76    "  nop\n\t"
77    " add    %1, %%o2, %0\n\t"
78    : "=r" (rv)
79    : "r" (add_value), "r" (dest)
80    : "memory", "o2", "o3");
81  return rv;
82}
83
84template<>
85template<typename I, typename D>
86inline D Atomic::PlatformAdd<8>::add_and_fetch(I add_value, D volatile* dest) const {
87  STATIC_ASSERT(8 == sizeof(I));
88  STATIC_ASSERT(8 == sizeof(D));
89
90  D rv;
91  __asm__ volatile(
92    "1: \n\t"
93    " ldx    [%2], %%o2\n\t"
94    " add    %1, %%o2, %%o3\n\t"
95    " casx   [%2], %%o2, %%o3\n\t"
96    " cmp    %%o2, %%o3\n\t"
97    " bne    %%xcc, 1b\n\t"
98    "  nop\n\t"
99    " add    %1, %%o2, %0\n\t"
100    : "=r" (rv)
101    : "r" (add_value), "r" (dest)
102    : "memory", "o2", "o3");
103  return rv;
104}
105
106
107inline jint     Atomic::xchg    (jint     exchange_value, volatile jint*     dest) {
108  intptr_t rv = exchange_value;
109  __asm__ volatile(
110    " swap   [%2],%1\n\t"
111    : "=r" (rv)
112    : "0" (exchange_value) /* we use same register as for return value */, "r" (dest)
113    : "memory");
114  return rv;
115}
116
117inline intptr_t Atomic::xchg_ptr(intptr_t exchange_value, volatile intptr_t* dest) {
118  intptr_t rv = exchange_value;
119  __asm__ volatile(
120    "1:\n\t"
121    " mov    %1, %%o3\n\t"
122    " ldx    [%2], %%o2\n\t"
123    " casx   [%2], %%o2, %%o3\n\t"
124    " cmp    %%o2, %%o3\n\t"
125    " bne    %%xcc, 1b\n\t"
126    "  nop\n\t"
127    " mov    %%o2, %0\n\t"
128    : "=r" (rv)
129    : "r" (exchange_value), "r" (dest)
130    : "memory", "o2", "o3");
131  return rv;
132}
133
134inline void*    Atomic::xchg_ptr(void*    exchange_value, volatile void*     dest) {
135  return (void*)xchg_ptr((intptr_t)exchange_value, (volatile intptr_t*)dest);
136}
137
138// No direct support for cmpxchg of bytes; emulate using int.
139template<>
140struct Atomic::PlatformCmpxchg<1> : Atomic::CmpxchgByteUsingInt {};
141
142template<>
143template<typename T>
144inline T Atomic::PlatformCmpxchg<4>::operator()(T exchange_value,
145                                                T volatile* dest,
146                                                T compare_value,
147                                                cmpxchg_memory_order order) const {
148  STATIC_ASSERT(4 == sizeof(T));
149  T rv;
150  __asm__ volatile(
151    " cas    [%2], %3, %0"
152    : "=r" (rv)
153    : "0" (exchange_value), "r" (dest), "r" (compare_value)
154    : "memory");
155  return rv;
156}
157
158template<>
159template<typename T>
160inline T Atomic::PlatformCmpxchg<8>::operator()(T exchange_value,
161                                                T volatile* dest,
162                                                T compare_value,
163                                                cmpxchg_memory_order order) const {
164  STATIC_ASSERT(8 == sizeof(T));
165  T rv;
166  __asm__ volatile(
167    " casx   [%2], %3, %0"
168    : "=r" (rv)
169    : "0" (exchange_value), "r" (dest), "r" (compare_value)
170    : "memory");
171  return rv;
172}
173
174#endif // OS_CPU_LINUX_SPARC_VM_ATOMIC_LINUX_SPARC_INLINE_HPP
175