atomic_bsd_x86.hpp revision 13038:ae91ec8b554a
12786Ssos/*
22786Ssos * Copyright (c) 1999, 2017, Oracle and/or its affiliates. All rights reserved.
32786Ssos * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
42786Ssos *
52786Ssos * This code is free software; you can redistribute it and/or modify it
62786Ssos * under the terms of the GNU General Public License version 2 only, as
72786Ssos * published by the Free Software Foundation.
86851Ssos *
92786Ssos * This code is distributed in the hope that it will be useful, but WITHOUT
102786Ssos * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
112786Ssos * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
122786Ssos * version 2 for more details (a copy is included in the LICENSE file that
132786Ssos * accompanied this code).
142786Ssos *
152786Ssos * You should have received a copy of the GNU General Public License version
162786Ssos * 2 along with this work; if not, write to the Free Software Foundation,
172786Ssos * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
182786Ssos *
197420Ssos * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
204686Sache * or visit www.oracle.com if you need additional information or have any
212786Ssos * questions.
222786Ssos *
232786Ssos */
242786Ssos
252786Ssos#ifndef OS_CPU_BSD_X86_VM_ATOMIC_BSD_X86_HPP
262786Ssos#define OS_CPU_BSD_X86_VM_ATOMIC_BSD_X86_HPP
272786Ssos
282786Ssos#include "runtime/os.hpp"
292786Ssos
302786Ssos// Implementation of class atomic
312786Ssos
322786Ssosinline void Atomic::store    (jbyte    store_value, jbyte*    dest) { *dest = store_value; }
332786Ssosinline void Atomic::store    (jshort   store_value, jshort*   dest) { *dest = store_value; }
342786Ssosinline void Atomic::store    (jint     store_value, jint*     dest) { *dest = store_value; }
352786Ssosinline void Atomic::store_ptr(intptr_t store_value, intptr_t* dest) { *dest = store_value; }
362786Ssosinline void Atomic::store_ptr(void*    store_value, void*     dest) { *(void**)dest = store_value; }
372786Ssos
382786Ssosinline void Atomic::store    (jbyte    store_value, volatile jbyte*    dest) { *dest = store_value; }
392786Ssosinline void Atomic::store    (jshort   store_value, volatile jshort*   dest) { *dest = store_value; }
402786Ssosinline void Atomic::store    (jint     store_value, volatile jint*     dest) { *dest = store_value; }
412786Ssosinline void Atomic::store_ptr(intptr_t store_value, volatile intptr_t* dest) { *dest = store_value; }
422786Ssosinline void Atomic::store_ptr(void*    store_value, volatile void*     dest) { *(void* volatile *)dest = store_value; }
432786Ssos
442786Ssos
452786Ssosinline jint     Atomic::add    (jint     add_value, volatile jint*     dest) {
462786Ssos  jint addend = add_value;
472786Ssos  __asm__ volatile (  "lock xaddl %0,(%2)"
482786Ssos                    : "=r" (addend)
492786Ssos                    : "0" (addend), "r" (dest)
502786Ssos                    : "cc", "memory");
512786Ssos  return addend + add_value;
522786Ssos}
532786Ssos
542786Ssosinline void Atomic::inc    (volatile jint*     dest) {
552786Ssos  __asm__ volatile (  "lock addl $1,(%0)" :
562786Ssos                    : "r" (dest) : "cc", "memory");
572786Ssos}
582786Ssos
592786Ssosinline void Atomic::inc_ptr(volatile void*     dest) {
602786Ssos  inc_ptr((volatile intptr_t*)dest);
612786Ssos}
626851Ssos
632786Ssosinline void Atomic::dec    (volatile jint*     dest) {
642786Ssos  __asm__ volatile (  "lock subl $1,(%0)" :
652786Ssos                    : "r" (dest) : "cc", "memory");
662786Ssos}
672786Ssos
682786Ssosinline void Atomic::dec_ptr(volatile void*     dest) {
692786Ssos  dec_ptr((volatile intptr_t*)dest);
702786Ssos}
712786Ssos
722786Ssosinline jint     Atomic::xchg    (jint     exchange_value, volatile jint*     dest) {
732786Ssos  __asm__ volatile (  "xchgl (%2),%0"
742786Ssos                    : "=r" (exchange_value)
752786Ssos                    : "0" (exchange_value), "r" (dest)
762786Ssos                    : "memory");
772786Ssos  return exchange_value;
782786Ssos}
792786Ssos
802786Ssosinline void*    Atomic::xchg_ptr(void*    exchange_value, volatile void*     dest) {
815994Ssos  return (void*)xchg_ptr((intptr_t)exchange_value, (volatile intptr_t*)dest);
822786Ssos}
832786Ssos
842786Ssos#define VM_HAS_SPECIALIZED_CMPXCHG_BYTE
852786Ssosinline jbyte    Atomic::cmpxchg    (jbyte    exchange_value, volatile jbyte*    dest, jbyte    compare_value, cmpxchg_memory_order order) {
862786Ssos  __asm__ volatile (  "lock cmpxchgb %1,(%3)"
872786Ssos                    : "=a" (exchange_value)
886045Ssos                    : "q" (exchange_value), "a" (compare_value), "r" (dest)
892786Ssos                    : "cc", "memory");
902786Ssos  return exchange_value;
912786Ssos}
922786Ssos
932786Ssosinline jint     Atomic::cmpxchg    (jint     exchange_value, volatile jint*     dest, jint     compare_value, cmpxchg_memory_order order) {
942786Ssos  __asm__ volatile (  "lock cmpxchgl %1,(%3)"
952786Ssos                    : "=a" (exchange_value)
962786Ssos                    : "r" (exchange_value), "a" (compare_value), "r" (dest)
972786Ssos                    : "cc", "memory");
982786Ssos  return exchange_value;
992786Ssos}
1002786Ssos
1012786Ssos#ifdef AMD64
1022786Ssosinline void Atomic::store    (jlong    store_value, jlong*    dest) { *dest = store_value; }
1032786Ssosinline void Atomic::store    (jlong    store_value, volatile jlong*    dest) { *dest = store_value; }
1042786Ssos
1052786Ssosinline intptr_t Atomic::add_ptr(intptr_t add_value, volatile intptr_t* dest) {
1062786Ssos  intptr_t addend = add_value;
1072786Ssos  __asm__ __volatile__ (  "lock xaddq %0,(%2)"
1086851Ssos                        : "=r" (addend)
1092786Ssos                        : "0" (addend), "r" (dest)
1106851Ssos                        : "cc", "memory");
1116851Ssos  return addend + add_value;
1126851Ssos}
113
114inline void*    Atomic::add_ptr(intptr_t add_value, volatile void*     dest) {
115  return (void*)add_ptr(add_value, (volatile intptr_t*)dest);
116}
117
118inline void Atomic::inc_ptr(volatile intptr_t* dest) {
119  __asm__ __volatile__ (  "lock addq $1,(%0)"
120                        :
121                        : "r" (dest)
122                        : "cc", "memory");
123}
124
125inline void Atomic::dec_ptr(volatile intptr_t* dest) {
126  __asm__ __volatile__ (  "lock subq $1,(%0)"
127                        :
128                        : "r" (dest)
129                        : "cc", "memory");
130}
131
132inline intptr_t Atomic::xchg_ptr(intptr_t exchange_value, volatile intptr_t* dest) {
133  __asm__ __volatile__ ("xchgq (%2),%0"
134                        : "=r" (exchange_value)
135                        : "0" (exchange_value), "r" (dest)
136                        : "memory");
137  return exchange_value;
138}
139
140inline jlong    Atomic::cmpxchg    (jlong    exchange_value, volatile jlong*    dest, jlong    compare_value, cmpxchg_memory_order order) {
141  __asm__ __volatile__ (  "lock cmpxchgq %1,(%3)"
142                        : "=a" (exchange_value)
143                        : "r" (exchange_value), "a" (compare_value), "r" (dest)
144                        : "cc", "memory");
145  return exchange_value;
146}
147
148inline intptr_t Atomic::cmpxchg_ptr(intptr_t exchange_value, volatile intptr_t* dest, intptr_t compare_value, cmpxchg_memory_order order) {
149  return (intptr_t)cmpxchg((jlong)exchange_value, (volatile jlong*)dest, (jlong)compare_value, order);
150}
151
152inline void*    Atomic::cmpxchg_ptr(void*    exchange_value, volatile void*     dest, void*    compare_value, cmpxchg_memory_order order) {
153  return (void*)cmpxchg((jlong)exchange_value, (volatile jlong*)dest, (jlong)compare_value, order);
154}
155
156inline jlong Atomic::load(volatile jlong* src) { return *src; }
157
158#else // !AMD64
159
160inline intptr_t Atomic::add_ptr(intptr_t add_value, volatile intptr_t* dest) {
161  return (intptr_t)Atomic::add((jint)add_value, (volatile jint*)dest);
162}
163
164inline void*    Atomic::add_ptr(intptr_t add_value, volatile void*     dest) {
165  return (void*)Atomic::add((jint)add_value, (volatile jint*)dest);
166}
167
168
169inline void Atomic::inc_ptr(volatile intptr_t* dest) {
170  inc((volatile jint*)dest);
171}
172
173inline void Atomic::dec_ptr(volatile intptr_t* dest) {
174  dec((volatile jint*)dest);
175}
176
177inline intptr_t Atomic::xchg_ptr(intptr_t exchange_value, volatile intptr_t* dest) {
178  return (intptr_t)xchg((jint)exchange_value, (volatile jint*)dest);
179}
180
181extern "C" {
182  // defined in bsd_x86.s
183  jlong _Atomic_cmpxchg_long(jlong, volatile jlong*, jlong, bool);
184  void _Atomic_move_long(volatile jlong* src, volatile jlong* dst);
185}
186
187inline jlong    Atomic::cmpxchg    (jlong    exchange_value, volatile jlong*    dest, jlong    compare_value, cmpxchg_memory_order order) {
188  return _Atomic_cmpxchg_long(exchange_value, dest, compare_value, os::is_MP());
189}
190
191inline intptr_t Atomic::cmpxchg_ptr(intptr_t exchange_value, volatile intptr_t* dest, intptr_t compare_value, cmpxchg_memory_order order) {
192  return (intptr_t)cmpxchg((jint)exchange_value, (volatile jint*)dest, (jint)compare_value, order);
193}
194
195inline void*    Atomic::cmpxchg_ptr(void*    exchange_value, volatile void*     dest, void*    compare_value, cmpxchg_memory_order order) {
196  return (void*)cmpxchg((jint)exchange_value, (volatile jint*)dest, (jint)compare_value, order);
197}
198
199inline jlong Atomic::load(volatile jlong* src) {
200  volatile jlong dest;
201  _Atomic_move_long(src, &dest);
202  return dest;
203}
204
205inline void Atomic::store(jlong store_value, jlong* dest) {
206  _Atomic_move_long((volatile jlong*)&store_value, (volatile jlong*)dest);
207}
208
209inline void Atomic::store(jlong store_value, volatile jlong* dest) {
210  _Atomic_move_long((volatile jlong*)&store_value, dest);
211}
212
213#endif // AMD64
214
215#endif // OS_CPU_BSD_X86_VM_ATOMIC_BSD_X86_HPP
216