2018-04-18 07:25:51 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
|
|
|
|
* 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
|
|
|
|
|
|
|
#include "runtime/mutex.hpp"
|
|
|
|
#include "runtime/semaphore.hpp"
|
|
|
|
#include "runtime/thread.hpp"
|
|
|
|
#include "runtime/vmThread.hpp"
|
2018-12-06 14:44:40 +00:00
|
|
|
#include "runtime/vmOperations.hpp"
|
2018-04-18 07:25:51 +00:00
|
|
|
#include "unittest.hpp"
|
|
|
|
|
|
|
|
class VM_StopSafepoint : public VM_Operation {
|
|
|
|
public:
|
2018-11-20 08:35:15 +00:00
|
|
|
Semaphore* _running;
|
2018-04-18 07:25:51 +00:00
|
|
|
Semaphore* _test_complete;
|
2018-11-20 08:35:15 +00:00
|
|
|
VM_StopSafepoint(Semaphore* running, Semaphore* wait_for) :
|
|
|
|
_running(running), _test_complete(wait_for) {}
|
2018-07-11 18:44:05 +00:00
|
|
|
VMOp_Type type() const { return VMOp_None; }
|
2018-04-18 07:25:51 +00:00
|
|
|
Mode evaluation_mode() const { return _no_safepoint; }
|
|
|
|
bool is_cheap_allocated() 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 {
|
|
|
|
public:
|
2018-11-20 08:35:15 +00:00
|
|
|
Semaphore _ready;
|
|
|
|
Semaphore _unblock;
|
|
|
|
VMThreadBlocker() {}
|
2018-04-18 07:25:51 +00:00
|
|
|
virtual ~VMThreadBlocker() {}
|
2018-12-28 02:17:11 +00:00
|
|
|
const char* get_thread_name_string(char* buf, int buflen) const {
|
2018-12-28 21:31:11 +00:00
|
|
|
return "VMThreadBlocker";
|
2018-12-28 02:17:11 +00:00
|
|
|
}
|
2018-04-18 07:25:51 +00:00
|
|
|
void run() {
|
|
|
|
this->set_thread_state(_thread_in_vm);
|
|
|
|
{
|
|
|
|
MutexLocker ml(Threads_lock);
|
|
|
|
Threads::add(this);
|
|
|
|
}
|
2018-11-20 08:35:15 +00:00
|
|
|
VM_StopSafepoint ss(&_ready, &_unblock);
|
2018-04-18 07:25:51 +00:00
|
|
|
VMThread::execute(&ss);
|
2018-12-28 02:17:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Override as JavaThread::post_run() calls JavaThread::exit which
|
|
|
|
// expects a valid thread object oop.
|
|
|
|
virtual void post_run() {
|
2018-04-18 07:25:51 +00:00
|
|
|
Threads::remove(this);
|
|
|
|
this->smr_delete();
|
|
|
|
}
|
2018-12-28 02:17:11 +00:00
|
|
|
|
2018-04-18 07:25:51 +00:00
|
|
|
void doit() {
|
|
|
|
if (os::create_thread(this, os::os_thread)) {
|
|
|
|
os::start_thread(this);
|
|
|
|
} else {
|
|
|
|
ASSERT_TRUE(false);
|
|
|
|
}
|
|
|
|
}
|
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 {
|
|
|
|
public:
|
|
|
|
Semaphore* _post;
|
|
|
|
JavaTestThread(Semaphore* post)
|
|
|
|
: _post(post) {
|
|
|
|
}
|
|
|
|
virtual ~JavaTestThread() {}
|
|
|
|
|
2018-12-28 02:17:11 +00:00
|
|
|
const char* get_thread_name_string(char* buf, int buflen) const {
|
2018-12-28 21:31:11 +00:00
|
|
|
return "JavaTestThread";
|
2018-12-28 02:17:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void pre_run() {
|
2018-04-18 07:25:51 +00:00
|
|
|
this->set_thread_state(_thread_in_vm);
|
|
|
|
{
|
|
|
|
MutexLocker ml(Threads_lock);
|
|
|
|
Threads::add(this);
|
|
|
|
}
|
|
|
|
{
|
|
|
|
MutexLockerEx ml(SR_lock(), Mutex::_no_safepoint_check_flag);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void main_run() = 0;
|
|
|
|
|
|
|
|
void run() {
|
|
|
|
main_run();
|
|
|
|
}
|
|
|
|
|
2018-12-28 02:17:11 +00:00
|
|
|
// Override as JavaThread::post_run() calls JavaThread::exit which
|
|
|
|
// expects a valid thread object oop. And we need to call signal.
|
|
|
|
void post_run() {
|
2018-04-18 07:25:51 +00:00
|
|
|
Threads::remove(this);
|
|
|
|
_post->signal();
|
|
|
|
this->smr_delete();
|
|
|
|
}
|
|
|
|
|
|
|
|
void doit() {
|
|
|
|
if (os::create_thread(this, os::os_thread)) {
|
|
|
|
os::start_thread(this);
|
|
|
|
} else {
|
|
|
|
ASSERT_TRUE(false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template <typename FUNC>
|
|
|
|
class SingleTestThread : public JavaTestThread {
|
|
|
|
public:
|
|
|
|
FUNC& _f;
|
|
|
|
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;
|
|
|
|
|
|
|
|
VMThreadBlocker* blocker = new VMThreadBlocker();
|
2018-04-18 07:25:51 +00:00
|
|
|
blocker->doit();
|
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;
|
|
|
|
|
|
|
|
VMThreadBlocker* blocker = new VMThreadBlocker();
|
2018-04-18 07:25:51 +00:00
|
|
|
blocker->doit();
|
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
|