Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/src/hotspot/os/posix/os_posix.hpp
40930 views
1
/*
2
* Copyright (c) 1999, 2021, 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_POSIX_OS_POSIX_HPP
26
#define OS_POSIX_OS_POSIX_HPP
27
28
// File conventions
29
static const char* file_separator() { return "/"; }
30
static const char* line_separator() { return "\n"; }
31
static const char* path_separator() { return ":"; }
32
33
class Posix {
34
friend class os;
35
36
protected:
37
static void print_distro_info(outputStream* st);
38
static void print_rlimit_info(outputStream* st);
39
static void print_uname_info(outputStream* st);
40
static void print_libversion_info(outputStream* st);
41
static void print_load_average(outputStream* st);
42
static void print_uptime_info(outputStream* st);
43
44
// Minimum stack size a thread can be created with (allowing
45
// the VM to completely create the thread and enter user code).
46
// The initial values exclude any guard pages (by HotSpot or libc).
47
// set_minimum_stack_sizes() will add the size required for
48
// HotSpot guard pages depending on page size and flag settings.
49
// Libc guard pages are never considered by these values.
50
static size_t _compiler_thread_min_stack_allowed;
51
static size_t _java_thread_min_stack_allowed;
52
static size_t _vm_internal_thread_min_stack_allowed;
53
54
public:
55
static void init(void); // early initialization - no logging available
56
static void init_2(void);// later initialization - logging available
57
58
// Return default stack size for the specified thread type
59
static size_t default_stack_size(os::ThreadType thr_type);
60
// Check and sets minimum stack sizes
61
static jint set_minimum_stack_sizes();
62
static size_t get_initial_stack_size(ThreadType thr_type, size_t req_stack_size);
63
64
// Helper function; describes pthread attributes as short string. String is written
65
// to buf with len buflen; buf is returned.
66
static char* describe_pthread_attr(char* buf, size_t buflen, const pthread_attr_t* attr);
67
68
// A safe implementation of realpath which will not cause a buffer overflow if the resolved path
69
// is longer than PATH_MAX.
70
// On success, returns 'outbuf', which now contains the path.
71
// On error, it will return NULL and set errno. The content of 'outbuf' is undefined.
72
// On truncation error ('outbuf' too small), it will return NULL and set errno to ENAMETOOLONG.
73
static char* realpath(const char* filename, char* outbuf, size_t outbuflen);
74
75
// Returns true if given uid is root.
76
static bool is_root(uid_t uid);
77
78
// Returns true if given uid is effective or root uid.
79
static bool matches_effective_uid_or_root(uid_t uid);
80
81
// Returns true if either given uid is effective uid and given gid is
82
// effective gid, or if given uid is root.
83
static bool matches_effective_uid_and_gid_or_root(uid_t uid, gid_t gid);
84
85
static void print_umask(outputStream* st, mode_t umsk);
86
87
static void print_user_info(outputStream* st);
88
89
// Set PC into context. Needed for continuation after signal.
90
static address ucontext_get_pc(const ucontext_t* ctx);
91
static void ucontext_set_pc(ucontext_t* ctx, address pc);
92
93
static void to_RTC_abstime(timespec* abstime, int64_t millis);
94
95
static bool handle_stack_overflow(JavaThread* thread, address addr, address pc,
96
const void* ucVoid,
97
address* stub);
98
};
99
100
/*
101
* Crash protection for the JfrSampler thread. Wrap the callback
102
* with a sigsetjmp and in case of a SIGSEGV/SIGBUS we siglongjmp
103
* back.
104
* To be able to use this - don't take locks, don't rely on destructors,
105
* don't make OS library calls, don't allocate memory, don't print,
106
* don't call code that could leave the heap / memory in an inconsistent state,
107
* or anything else where we are not in control if we suddenly jump out.
108
*/
109
class ThreadCrashProtection : public StackObj {
110
public:
111
static bool is_crash_protected(Thread* thr) {
112
return _crash_protection != NULL && _protected_thread == thr;
113
}
114
115
ThreadCrashProtection();
116
bool call(os::CrashProtectionCallback& cb);
117
118
static void check_crash_protection(int signal, Thread* thread);
119
private:
120
static Thread* _protected_thread;
121
static ThreadCrashProtection* _crash_protection;
122
void restore();
123
sigjmp_buf _jmpbuf;
124
};
125
126
/*
127
* This is the platform-specific implementation underpinning
128
* the ParkEvent class, which itself underpins Java-level monitor
129
* operations. See park.hpp for details.
130
* These event objects are type-stable and immortal - we never delete them.
131
* Events are associated with a thread for the lifetime of the thread.
132
*/
133
class PlatformEvent : public CHeapObj<mtSynchronizer> {
134
private:
135
double cachePad[4]; // Increase odds that _mutex is sole occupant of cache line
136
volatile int _event; // Event count/permit: -1, 0 or 1
137
volatile int _nParked; // Indicates if associated thread is blocked: 0 or 1
138
pthread_mutex_t _mutex[1]; // Native mutex for locking
139
pthread_cond_t _cond[1]; // Native condition variable for blocking
140
double postPad[2];
141
142
protected: // TODO-FIXME: make dtor private
143
~PlatformEvent() { guarantee(false, "invariant"); } // immortal so can't delete
144
145
public:
146
PlatformEvent();
147
void park();
148
int park(jlong millis);
149
void unpark();
150
151
// Use caution with reset() and fired() -- they may require MEMBARs
152
void reset() { _event = 0; }
153
int fired() { return _event; }
154
};
155
156
// JSR166 support
157
// PlatformParker provides the platform dependent base class for the
158
// Parker class. It basically provides the internal data structures:
159
// - mutex and convars
160
// which are then used directly by the Parker methods defined in the OS
161
// specific implementation files.
162
// There is significant overlap between the funcionality supported in the
163
// combination of Parker+PlatformParker and PlatformEvent (above). If Parker
164
// were more like ObjectMonitor we could use PlatformEvent in both (with some
165
// API updates of course). But Parker methods use fastpaths that break that
166
// level of encapsulation - so combining the two remains a future project.
167
168
class PlatformParker {
169
NONCOPYABLE(PlatformParker);
170
protected:
171
enum {
172
REL_INDEX = 0,
173
ABS_INDEX = 1
174
};
175
volatile int _counter;
176
int _cur_index; // which cond is in use: -1, 0, 1
177
pthread_mutex_t _mutex[1];
178
pthread_cond_t _cond[2]; // one for relative times and one for absolute
179
180
public:
181
PlatformParker();
182
~PlatformParker();
183
};
184
185
// Workaround for a bug in macOSX kernel's pthread support (fixed in Mojave?).
186
// Avoid ever allocating a pthread_mutex_t at the same address as one of our
187
// former pthread_cond_t, by using freelists of mutexes and condvars.
188
// Conditional to avoid extra indirection and padding loss on other platforms.
189
#ifdef __APPLE__
190
#define PLATFORM_MONITOR_IMPL_INDIRECT 1
191
#else
192
#define PLATFORM_MONITOR_IMPL_INDIRECT 0
193
#endif
194
195
// Platform specific implementations that underpin VM Mutex/Monitor classes.
196
// Note that we use "normal" pthread_mutex_t attributes so that recursive
197
// locking is not supported, which matches the expected semantics of the
198
// VM Mutex class.
199
200
class PlatformMutex : public CHeapObj<mtSynchronizer> {
201
#if PLATFORM_MONITOR_IMPL_INDIRECT
202
class Mutex : public CHeapObj<mtSynchronizer> {
203
public:
204
pthread_mutex_t _mutex;
205
Mutex* _next;
206
207
Mutex();
208
~Mutex();
209
};
210
211
Mutex* _impl;
212
213
static pthread_mutex_t _freelist_lock; // used for mutex and cond freelists
214
static Mutex* _mutex_freelist;
215
216
protected:
217
class WithFreeListLocked;
218
pthread_mutex_t* mutex() { return &(_impl->_mutex); }
219
220
public:
221
PlatformMutex(); // Use freelist allocation of impl.
222
~PlatformMutex();
223
224
static void init(); // Initialize the freelist.
225
226
#else
227
228
pthread_mutex_t _mutex;
229
230
protected:
231
pthread_mutex_t* mutex() { return &_mutex; }
232
233
public:
234
static void init() {} // Nothing needed for the non-indirect case.
235
236
PlatformMutex();
237
~PlatformMutex();
238
239
#endif // PLATFORM_MONITOR_IMPL_INDIRECT
240
241
private:
242
NONCOPYABLE(PlatformMutex);
243
244
public:
245
void lock();
246
void unlock();
247
bool try_lock();
248
};
249
250
class PlatformMonitor : public PlatformMutex {
251
#if PLATFORM_MONITOR_IMPL_INDIRECT
252
class Cond : public CHeapObj<mtSynchronizer> {
253
public:
254
pthread_cond_t _cond;
255
Cond* _next;
256
257
Cond();
258
~Cond();
259
};
260
261
Cond* _impl;
262
263
static Cond* _cond_freelist;
264
265
pthread_cond_t* cond() { return &(_impl->_cond); }
266
267
public:
268
PlatformMonitor(); // Use freelist allocation of impl.
269
~PlatformMonitor();
270
271
#else
272
273
pthread_cond_t _cond;
274
pthread_cond_t* cond() { return &_cond; }
275
276
public:
277
PlatformMonitor();
278
~PlatformMonitor();
279
280
#endif // PLATFORM_MONITOR_IMPL_INDIRECT
281
282
private:
283
NONCOPYABLE(PlatformMonitor);
284
285
public:
286
int wait(jlong millis);
287
void notify();
288
void notify_all();
289
};
290
291
#endif // OS_POSIX_OS_POSIX_HPP
292
293