Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/hotspot/gtest/threadHelper.inline.hpp
41140 views
1
/*
2
* Copyright (c) 2018, 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
#ifndef GTEST_THREADHELPER_INLINE_HPP
25
#define GTEST_THREADHELPER_INLINE_HPP
26
27
#include "runtime/mutex.hpp"
28
#include "runtime/semaphore.hpp"
29
#include "runtime/thread.inline.hpp"
30
#include "runtime/vmThread.hpp"
31
#include "runtime/vmOperations.hpp"
32
#include "unittest.hpp"
33
34
class VM_StopSafepoint : public VM_Operation {
35
public:
36
Semaphore* _running;
37
Semaphore* _test_complete;
38
VM_StopSafepoint(Semaphore* running, Semaphore* wait_for) :
39
_running(running), _test_complete(wait_for) {}
40
VMOp_Type type() const { return VMOp_None; }
41
bool evaluate_at_safepoint() const { return false; }
42
void doit() { _running->signal(); _test_complete->wait(); }
43
};
44
45
// This class and thread keep the non-safepoint op running while we do our testing.
46
class VMThreadBlocker : public JavaThread {
47
public:
48
Semaphore _ready;
49
Semaphore _unblock;
50
VMThreadBlocker() {}
51
virtual ~VMThreadBlocker() {}
52
const char* get_thread_name_string(char* buf, int buflen) const {
53
return "VMThreadBlocker";
54
}
55
void run() {
56
this->set_thread_state(_thread_in_vm);
57
{
58
MutexLocker ml(Threads_lock);
59
Threads::add(this);
60
}
61
VM_StopSafepoint ss(&_ready, &_unblock);
62
VMThread::execute(&ss);
63
}
64
65
// Override as JavaThread::post_run() calls JavaThread::exit which
66
// expects a valid thread object oop.
67
virtual void post_run() {
68
Threads::remove(this, false);
69
this->smr_delete();
70
}
71
72
void doit() {
73
if (os::create_thread(this, os::os_thread)) {
74
os::start_thread(this);
75
} else {
76
ASSERT_TRUE(false);
77
}
78
}
79
void ready() {
80
_ready.wait();
81
}
82
void release() {
83
_unblock.signal();
84
}
85
};
86
87
// For testing in a real JavaThread.
88
class JavaTestThread : public JavaThread {
89
public:
90
Semaphore* _post;
91
JavaTestThread(Semaphore* post)
92
: _post(post) {
93
}
94
virtual ~JavaTestThread() {}
95
96
const char* get_thread_name_string(char* buf, int buflen) const {
97
return "JavaTestThread";
98
}
99
100
void pre_run() {
101
this->set_thread_state(_thread_in_vm);
102
{
103
MutexLocker ml(Threads_lock);
104
Threads::add(this);
105
}
106
}
107
108
virtual void main_run() = 0;
109
110
void run() {
111
main_run();
112
}
113
114
// Override as JavaThread::post_run() calls JavaThread::exit which
115
// expects a valid thread object oop. And we need to call signal.
116
void post_run() {
117
Threads::remove(this, false);
118
_post->signal();
119
this->smr_delete();
120
}
121
122
void doit() {
123
if (os::create_thread(this, os::os_thread)) {
124
os::start_thread(this);
125
} else {
126
ASSERT_TRUE(false);
127
}
128
}
129
};
130
131
template <typename FUNC>
132
class SingleTestThread : public JavaTestThread {
133
public:
134
FUNC& _f;
135
SingleTestThread(Semaphore* post, FUNC& f)
136
: JavaTestThread(post), _f(f) {
137
}
138
139
virtual ~SingleTestThread(){}
140
141
virtual void main_run() {
142
_f(this);
143
}
144
};
145
146
template <typename TESTFUNC>
147
static void nomt_test_doer(TESTFUNC &f) {
148
Semaphore post;
149
150
VMThreadBlocker* blocker = new VMThreadBlocker();
151
blocker->doit();
152
blocker->ready();
153
154
SingleTestThread<TESTFUNC>* stt = new SingleTestThread<TESTFUNC>(&post, f);
155
stt->doit();
156
post.wait();
157
158
blocker->release();
159
}
160
161
template <typename RUNNER>
162
static void mt_test_doer() {
163
Semaphore post;
164
165
VMThreadBlocker* blocker = new VMThreadBlocker();
166
blocker->doit();
167
blocker->ready();
168
169
RUNNER* runner = new RUNNER(&post);
170
runner->doit();
171
post.wait();
172
173
blocker->release();
174
}
175
176
#endif // include guard
177
178