Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/src/hotspot/os_cpu/linux_zero/atomic_linux_zero.hpp
40931 views
1
/*
2
* Copyright (c) 2003, 2020, Oracle and/or its affiliates. All rights reserved.
3
* Copyright 2007, 2008, 2011, 2015, Red Hat, Inc.
4
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5
*
6
* This code is free software; you can redistribute it and/or modify it
7
* under the terms of the GNU General Public License version 2 only, as
8
* published by the Free Software Foundation.
9
*
10
* This code is distributed in the hope that it will be useful, but WITHOUT
11
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13
* version 2 for more details (a copy is included in the LICENSE file that
14
* accompanied this code).
15
*
16
* You should have received a copy of the GNU General Public License version
17
* 2 along with this work; if not, write to the Free Software Foundation,
18
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
19
*
20
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
21
* or visit www.oracle.com if you need additional information or have any
22
* questions.
23
*
24
*/
25
26
#ifndef OS_CPU_LINUX_ZERO_ATOMIC_LINUX_ZERO_HPP
27
#define OS_CPU_LINUX_ZERO_ATOMIC_LINUX_ZERO_HPP
28
29
#include "orderAccess_linux_zero.hpp"
30
#include "runtime/os.hpp"
31
32
// Implementation of class atomic
33
34
template<size_t byte_size>
35
struct Atomic::PlatformAdd {
36
template<typename D, typename I>
37
D add_and_fetch(D volatile* dest, I add_value, atomic_memory_order order) const;
38
39
template<typename D, typename I>
40
D fetch_and_add(D volatile* dest, I add_value, atomic_memory_order order) const {
41
return add_and_fetch(dest, add_value, order) - add_value;
42
}
43
};
44
45
template<>
46
template<typename D, typename I>
47
inline D Atomic::PlatformAdd<4>::add_and_fetch(D volatile* dest, I add_value,
48
atomic_memory_order order) const {
49
STATIC_ASSERT(4 == sizeof(I));
50
STATIC_ASSERT(4 == sizeof(D));
51
52
D res = __atomic_add_fetch(dest, add_value, __ATOMIC_RELEASE);
53
FULL_MEM_BARRIER;
54
return res;
55
}
56
57
template<>
58
template<typename D, typename I>
59
inline D Atomic::PlatformAdd<8>::add_and_fetch(D volatile* dest, I add_value,
60
atomic_memory_order order) const {
61
STATIC_ASSERT(8 == sizeof(I));
62
STATIC_ASSERT(8 == sizeof(D));
63
64
D res = __atomic_add_fetch(dest, add_value, __ATOMIC_RELEASE);
65
FULL_MEM_BARRIER;
66
return res;
67
}
68
69
template<>
70
template<typename T>
71
inline T Atomic::PlatformXchg<4>::operator()(T volatile* dest,
72
T exchange_value,
73
atomic_memory_order order) const {
74
STATIC_ASSERT(4 == sizeof(T));
75
// __sync_lock_test_and_set is a bizarrely named atomic exchange
76
// operation. Note that some platforms only support this with the
77
// limitation that the only valid value to store is the immediate
78
// constant 1. There is a test for this in JNI_CreateJavaVM().
79
T result = __sync_lock_test_and_set (dest, exchange_value);
80
// All atomic operations are expected to be full memory barriers
81
// (see atomic.hpp). However, __sync_lock_test_and_set is not
82
// a full memory barrier, but an acquire barrier. Hence, this added
83
// barrier. Some platforms (notably ARM) have peculiarities with
84
// their barrier implementations, delegate it to OrderAccess.
85
OrderAccess::fence();
86
return result;
87
}
88
89
template<>
90
template<typename T>
91
inline T Atomic::PlatformXchg<8>::operator()(T volatile* dest,
92
T exchange_value,
93
atomic_memory_order order) const {
94
STATIC_ASSERT(8 == sizeof(T));
95
T result = __sync_lock_test_and_set (dest, exchange_value);
96
OrderAccess::fence();
97
return result;
98
}
99
100
// No direct support for cmpxchg of bytes; emulate using int.
101
template<>
102
struct Atomic::PlatformCmpxchg<1> : Atomic::CmpxchgByteUsingInt {};
103
104
template<>
105
template<typename T>
106
inline T Atomic::PlatformCmpxchg<4>::operator()(T volatile* dest,
107
T compare_value,
108
T exchange_value,
109
atomic_memory_order order) const {
110
STATIC_ASSERT(4 == sizeof(T));
111
112
T value = compare_value;
113
FULL_MEM_BARRIER;
114
__atomic_compare_exchange(dest, &value, &exchange_value, /*weak*/false,
115
__ATOMIC_RELAXED, __ATOMIC_RELAXED);
116
FULL_MEM_BARRIER;
117
return value;
118
}
119
120
template<>
121
template<typename T>
122
inline T Atomic::PlatformCmpxchg<8>::operator()(T volatile* dest,
123
T compare_value,
124
T exchange_value,
125
atomic_memory_order order) const {
126
STATIC_ASSERT(8 == sizeof(T));
127
128
FULL_MEM_BARRIER;
129
T value = compare_value;
130
__atomic_compare_exchange(dest, &value, &exchange_value, /*weak*/false,
131
__ATOMIC_RELAXED, __ATOMIC_RELAXED);
132
FULL_MEM_BARRIER;
133
return value;
134
}
135
136
template<>
137
template<typename T>
138
inline T Atomic::PlatformLoad<8>::operator()(T const volatile* src) const {
139
STATIC_ASSERT(8 == sizeof(T));
140
volatile int64_t dest;
141
os::atomic_copy64(reinterpret_cast<const volatile int64_t*>(src), reinterpret_cast<volatile int64_t*>(&dest));
142
return PrimitiveConversions::cast<T>(dest);
143
}
144
145
template<>
146
template<typename T>
147
inline void Atomic::PlatformStore<8>::operator()(T volatile* dest,
148
T store_value) const {
149
STATIC_ASSERT(8 == sizeof(T));
150
os::atomic_copy64(reinterpret_cast<const volatile int64_t*>(&store_value), reinterpret_cast<volatile int64_t*>(dest));
151
}
152
153
#endif // OS_CPU_LINUX_ZERO_ATOMIC_LINUX_ZERO_HPP
154
155