/* * Copyright (c) 2003, 2022, Oracle and/or its affiliates. All rights reserved. * Copyright 2007, 2008, 2011, 2015, Red Hat, Inc. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions. *
*/
/* * __m68k_cmpxchg * * Atomically store newval in *ptr if *ptr is equal to oldval for user space. * Returns newval on success and oldval if no exchange happened. * This implementation is processor specific and works on * 68020 68030 68040 and 68060. * * It will not work on ColdFire, 68000 and 68010 since they lack the CAS * instruction. * Using a kernelhelper would be better for arch complete implementation. *
*/
/* Perform an atomic compare and swap: if the current value of `*PTR' is OLDVAL, then write NEWVAL into `*PTR'. Return the contents of
`*PTR' before the operation.*/ staticinlineint m68k_compare_and_swap(int newval, volatileint *ptr, int oldval) { for (;;) { int prev = *ptr; if (prev != oldval) return prev;
/* Atomically write VALUE into `*PTR' and returns the previous
contents of `*PTR'. */ staticinlineint m68k_lock_test_and_set(int newval, volatileint *ptr) { for (;;) { // Loop until success. int prev = *ptr;
/* * __kernel_cmpxchg * * Atomically store newval in *ptr if *ptr is equal to oldval for user space. * Return zero if *ptr was changed or non-zero if no exchange happened. * The C flag is also set if *ptr was changed to allow for assembly * optimization in the calling code. *
*/
/* Perform an atomic compare and swap: if the current value of `*PTR' is OLDVAL, then write NEWVAL into `*PTR'. Return the contents of
`*PTR' before the operation.*/ staticinlineint arm_compare_and_swap(int newval, volatileint *ptr, int oldval) { for (;;) { int prev = *ptr; if (prev != oldval) return prev;
// We failed even though prev == oldval. Try again.
}
}
/* Atomically add an int to memory. */ staticinlineint arm_add_and_fetch(int add_value, volatileint *ptr) { for (;;) { // Loop until a __kernel_cmpxchg succeeds.
/* Atomically write VALUE into `*PTR' and returns the previous
contents of `*PTR'. */ staticinlineint arm_lock_test_and_set(int newval, volatileint *ptr) { for (;;) { // Loop until a __kernel_cmpxchg succeeds. int prev = *ptr;
if (__kernel_cmpxchg (prev, newval, ptr) == 0) return prev;
}
} #endif// ARM
template<size_t byte_size> struct Atomic::PlatformAdd { template<typename D, typename I>
D add_and_fetch(D volatile* dest, I add_value, atomic_memory_order order) const;
D res = __atomic_add_fetch(dest, add_value, __ATOMIC_RELEASE);
FULL_MEM_BARRIER; return res;
}
template<> template<typename T> inline T Atomic::PlatformXchg<4>::operator()(T volatile* dest,
T exchange_value,
atomic_memory_order order) const {
STATIC_ASSERT(4 == sizeof(T)); #ifdef ARM return xchg_using_helper<int>(arm_lock_test_and_set, dest, exchange_value); #else #ifdef M68K return xchg_using_helper<int>(m68k_lock_test_and_set, dest, exchange_value); #else // __sync_lock_test_and_set is a bizarrely named atomic exchange // operation. Note that some platforms only support this with the // limitation that the only valid value to store is the immediate // constant 1. There is a test for this in JNI_CreateJavaVM().
T result = __sync_lock_test_and_set (dest, exchange_value); // All atomic operations are expected to be full memory barriers // (see atomic.hpp). However, __sync_lock_test_and_set is not // a full memory barrier, but an acquire barrier. Hence, this added // barrier. Some platforms (notably ARM) have peculiarities with // their barrier implementations, delegate it to OrderAccess.
OrderAccess::fence(); return result; #endif// M68K #endif// ARM
}
template<> template<typename T> inline T Atomic::PlatformXchg<8>::operator()(T volatile* dest,
T exchange_value,
atomic_memory_order order) const {
STATIC_ASSERT(8 == sizeof(T));
T result = __sync_lock_test_and_set (dest, exchange_value);
OrderAccess::fence(); return result;
}
// No direct support for cmpxchg of bytes; emulate using int. template<> struct Atomic::PlatformCmpxchg<1> : Atomic::CmpxchgByteUsingInt {};
template<> template<typename T> inline T Atomic::PlatformCmpxchg<4>::operator()(T volatile* dest,
T compare_value,
T exchange_value,
atomic_memory_order order) const {
STATIC_ASSERT(4 == sizeof(T)); #ifdef ARM return cmpxchg_using_helper<int>(arm_compare_and_swap, dest, compare_value, exchange_value); #else #ifdef M68K return cmpxchg_using_helper<int>(m68k_compare_and_swap, dest, compare_value, exchange_value); #else
T value = compare_value;
FULL_MEM_BARRIER;
__atomic_compare_exchange(dest, &value, &exchange_value, /*weak*/false,
__ATOMIC_RELAXED, __ATOMIC_RELAXED);
FULL_MEM_BARRIER; return value; #endif// M68K #endif// ARM
}
template<> template<typename T> inline T Atomic::PlatformCmpxchg<8>::operator()(T volatile* dest,
T compare_value,
T exchange_value,
atomic_memory_order order) const {
STATIC_ASSERT(8 == sizeof(T));
T value = compare_value;
FULL_MEM_BARRIER;
__atomic_compare_exchange(dest, &value, &exchange_value, /*weak*/false,
__ATOMIC_RELAXED, __ATOMIC_RELAXED);
FULL_MEM_BARRIER; return value;
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung ist noch experimentell.