2018-04-18 07:25:51 +00:00
|
|
|
/*
|
2023-04-13 23:10:18 +00:00
|
|
|
* Copyright (c) 2018, 2023, Oracle and/or its affiliates. All rights reserved.
|
2018-04-18 07:25:51 +00:00
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2018-07-20 18:52:11 +00:00
|
|
|
#ifndef GTEST_THREADHELPER_INLINE_HPP
|
|
|
|
#define GTEST_THREADHELPER_INLINE_HPP
|
2018-04-18 07:25:51 +00:00
|
|
|
|
2022-08-24 15:50:21 +00:00
|
|
|
#include "memory/allocation.hpp"
|
2022-11-24 15:06:19 +00:00
|
|
|
#include "runtime/interfaceSupport.inline.hpp"
|
2018-04-18 07:25:51 +00:00
|
|
|
#include "runtime/mutex.hpp"
|
|
|
|
#include "runtime/semaphore.hpp"
|
2019-01-14 08:26:34 +00:00
|
|
|
#include "runtime/thread.inline.hpp"
|
2018-12-06 14:44:40 +00:00
|
|
|
#include "runtime/vmOperations.hpp"
|
2022-11-24 15:06:19 +00:00
|
|
|
#include "runtime/vmThread.hpp"
|
2018-04-18 07:25:51 +00:00
|
|
|
#include "unittest.hpp"
|
|
|
|
|
2021-07-14 01:05:10 +00:00
|
|
|
static void startTestThread(JavaThread* thread, const char* name) {
|
|
|
|
EXCEPTION_MARK;
|
|
|
|
HandleMark hm(THREAD);
|
|
|
|
Handle thread_oop;
|
|
|
|
|
|
|
|
// This code can be called from the main thread, which is _thread_in_native,
|
|
|
|
// or by an existing JavaTestThread, which is _thread_in_vm.
|
|
|
|
if (THREAD->thread_state() == _thread_in_native) {
|
|
|
|
ThreadInVMfromNative tivfn(THREAD);
|
2023-04-13 23:10:18 +00:00
|
|
|
thread_oop = JavaThread::create_system_thread_object(name, CHECK);
|
2021-07-14 01:05:10 +00:00
|
|
|
JavaThread::start_internal_daemon(THREAD, thread, thread_oop, NoPriority);
|
|
|
|
} else {
|
2023-04-13 23:10:18 +00:00
|
|
|
thread_oop = JavaThread::create_system_thread_object(name, CHECK);
|
2021-07-14 01:05:10 +00:00
|
|
|
JavaThread::start_internal_daemon(THREAD, thread, thread_oop, NoPriority);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-31 12:30:09 +00:00
|
|
|
class VM_GTestStopSafepoint : public VM_Operation {
|
2018-04-18 07:25:51 +00:00
|
|
|
public:
|
2018-11-20 08:35:15 +00:00
|
|
|
Semaphore* _running;
|
2018-04-18 07:25:51 +00:00
|
|
|
Semaphore* _test_complete;
|
2022-01-31 12:30:09 +00:00
|
|
|
VM_GTestStopSafepoint(Semaphore* running, Semaphore* wait_for) :
|
2018-11-20 08:35:15 +00:00
|
|
|
_running(running), _test_complete(wait_for) {}
|
2022-01-31 12:30:09 +00:00
|
|
|
VMOp_Type type() const { return VMOp_GTestStopSafepoint; }
|
2019-11-29 11:09:25 +00:00
|
|
|
bool evaluate_at_safepoint() const { return false; }
|
2018-11-20 08:35:15 +00:00
|
|
|
void doit() { _running->signal(); _test_complete->wait(); }
|
2018-04-18 07:25:51 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// This class and thread keep the non-safepoint op running while we do our testing.
|
|
|
|
class VMThreadBlocker : public JavaThread {
|
2018-11-20 08:35:15 +00:00
|
|
|
Semaphore _ready;
|
|
|
|
Semaphore _unblock;
|
2021-07-14 01:05:10 +00:00
|
|
|
|
|
|
|
static void blocker_thread_entry(JavaThread* thread, TRAPS) {
|
|
|
|
VMThreadBlocker* t = static_cast<VMThreadBlocker*>(thread);
|
2022-01-31 12:30:09 +00:00
|
|
|
VM_GTestStopSafepoint ss(&t->_ready, &t->_unblock);
|
2018-04-18 07:25:51 +00:00
|
|
|
VMThread::execute(&ss);
|
2018-12-28 02:17:11 +00:00
|
|
|
}
|
|
|
|
|
2021-07-14 01:05:10 +00:00
|
|
|
VMThreadBlocker() : JavaThread(&blocker_thread_entry) {};
|
2018-12-28 02:17:11 +00:00
|
|
|
|
2021-07-14 01:05:10 +00:00
|
|
|
virtual ~VMThreadBlocker() {}
|
|
|
|
|
|
|
|
public:
|
|
|
|
// Convenience method for client code
|
|
|
|
static VMThreadBlocker* start() {
|
|
|
|
const char* name = "VMThreadBlocker";
|
|
|
|
VMThreadBlocker* thread = new VMThreadBlocker();
|
|
|
|
JavaThread::vm_exit_on_osthread_failure(thread);
|
|
|
|
startTestThread(thread, name);
|
|
|
|
return thread;
|
2018-04-18 07:25:51 +00:00
|
|
|
}
|
2021-07-14 01:05:10 +00:00
|
|
|
|
2018-11-20 08:35:15 +00:00
|
|
|
void ready() {
|
|
|
|
_ready.wait();
|
|
|
|
}
|
|
|
|
void release() {
|
|
|
|
_unblock.signal();
|
|
|
|
}
|
2018-04-18 07:25:51 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// For testing in a real JavaThread.
|
|
|
|
class JavaTestThread : public JavaThread {
|
|
|
|
Semaphore* _post;
|
2021-07-14 01:05:10 +00:00
|
|
|
|
|
|
|
protected:
|
2018-04-18 07:25:51 +00:00
|
|
|
JavaTestThread(Semaphore* post)
|
2021-07-14 01:05:10 +00:00
|
|
|
: JavaThread(&test_thread_entry), _post(post) {
|
|
|
|
JavaThread::vm_exit_on_osthread_failure(this);
|
2018-04-18 07:25:51 +00:00
|
|
|
}
|
|
|
|
virtual ~JavaTestThread() {}
|
|
|
|
|
2021-07-14 01:05:10 +00:00
|
|
|
public:
|
|
|
|
// simplified starting for callers and subclasses
|
|
|
|
void doit() {
|
|
|
|
startTestThread(this, "JavaTestThread");
|
2018-04-18 07:25:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
virtual void main_run() = 0;
|
|
|
|
|
2021-07-14 01:05:10 +00:00
|
|
|
static void test_thread_entry(JavaThread* thread, TRAPS) {
|
|
|
|
JavaTestThread* t = static_cast<JavaTestThread*>(thread);
|
|
|
|
t->main_run();
|
|
|
|
t->_post->signal();
|
2018-04-18 07:25:51 +00:00
|
|
|
}
|
2022-08-24 15:50:21 +00:00
|
|
|
|
|
|
|
void join() {
|
|
|
|
_post->wait();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// Calls a single-argument function of type F with the current thread (this)
|
|
|
|
// and a self-assigned thread id as its input in a new thread when doit() is run.
|
|
|
|
template<typename F>
|
|
|
|
class BasicTestThread : public JavaTestThread {
|
|
|
|
private:
|
|
|
|
F _fun;
|
|
|
|
const int _id;
|
|
|
|
public:
|
|
|
|
BasicTestThread(F fun, int id, Semaphore* sem)
|
|
|
|
: JavaTestThread(sem),
|
|
|
|
_fun(fun),
|
|
|
|
_id(id) {
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual ~BasicTestThread(){};
|
|
|
|
|
|
|
|
void main_run() override {
|
|
|
|
_fun(this, _id);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// A TestThreadGroup starts and tracks N threads running the same callable F.
|
|
|
|
// The callable F should have the signature void(Thread*,int) where Thread*
|
|
|
|
// is the current thread and int is an id in the range [0,N).
|
|
|
|
template<typename F>
|
|
|
|
class TestThreadGroup {
|
|
|
|
private:
|
|
|
|
VMThreadBlocker* _blocker;
|
|
|
|
BasicTestThread<F>** _threads;
|
|
|
|
const int _length;
|
|
|
|
Semaphore _sem;
|
|
|
|
|
|
|
|
public:
|
|
|
|
NONCOPYABLE(TestThreadGroup);
|
|
|
|
|
|
|
|
TestThreadGroup(F fun, const int number_of_threads)
|
|
|
|
:
|
|
|
|
_threads(NEW_C_HEAP_ARRAY(BasicTestThread<F>*, number_of_threads, mtTest)),
|
|
|
|
_length(number_of_threads),
|
|
|
|
_sem() {
|
|
|
|
for (int i = 0; i < _length; i++) {
|
|
|
|
_threads[i] = new BasicTestThread<F>(fun, i, &_sem);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
~TestThreadGroup() {
|
|
|
|
FREE_C_HEAP_ARRAY(BasicTestThread<F>*, _threads);
|
|
|
|
}
|
|
|
|
|
|
|
|
void doit() {
|
|
|
|
_blocker = VMThreadBlocker::start();
|
|
|
|
for (int i = 0; i < _length; i++) {
|
|
|
|
_threads[i]->doit();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
void join() {
|
|
|
|
for (int i = 0; i < _length; i++) {
|
|
|
|
_sem.wait();
|
|
|
|
}
|
|
|
|
_blocker->release();
|
|
|
|
}
|
2018-04-18 07:25:51 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
template <typename FUNC>
|
|
|
|
class SingleTestThread : public JavaTestThread {
|
|
|
|
FUNC& _f;
|
2021-07-14 01:05:10 +00:00
|
|
|
public:
|
2018-04-18 07:25:51 +00:00
|
|
|
SingleTestThread(Semaphore* post, FUNC& f)
|
|
|
|
: JavaTestThread(post), _f(f) {
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual ~SingleTestThread(){}
|
|
|
|
|
|
|
|
virtual void main_run() {
|
|
|
|
_f(this);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template <typename TESTFUNC>
|
|
|
|
static void nomt_test_doer(TESTFUNC &f) {
|
2018-11-20 08:35:15 +00:00
|
|
|
Semaphore post;
|
|
|
|
|
2021-07-14 01:05:10 +00:00
|
|
|
VMThreadBlocker* blocker = VMThreadBlocker::start();
|
|
|
|
|
2018-11-20 08:35:15 +00:00
|
|
|
blocker->ready();
|
|
|
|
|
2018-04-18 07:25:51 +00:00
|
|
|
SingleTestThread<TESTFUNC>* stt = new SingleTestThread<TESTFUNC>(&post, f);
|
|
|
|
stt->doit();
|
|
|
|
post.wait();
|
2018-11-20 08:35:15 +00:00
|
|
|
|
|
|
|
blocker->release();
|
2018-04-18 07:25:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template <typename RUNNER>
|
|
|
|
static void mt_test_doer() {
|
2018-11-20 08:35:15 +00:00
|
|
|
Semaphore post;
|
|
|
|
|
2021-07-14 01:05:10 +00:00
|
|
|
VMThreadBlocker* blocker = VMThreadBlocker::start();
|
|
|
|
|
2018-11-20 08:35:15 +00:00
|
|
|
blocker->ready();
|
|
|
|
|
2018-04-18 07:25:51 +00:00
|
|
|
RUNNER* runner = new RUNNER(&post);
|
|
|
|
runner->doit();
|
|
|
|
post.wait();
|
2018-11-20 08:35:15 +00:00
|
|
|
|
|
|
|
blocker->release();
|
2018-04-18 07:25:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif // include guard
|