Path: blob/master/test/hotspot/gtest/threadHelper.inline.hpp
41140 views
/*1* Copyright (c) 2018, 2021, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223#ifndef GTEST_THREADHELPER_INLINE_HPP24#define GTEST_THREADHELPER_INLINE_HPP2526#include "runtime/mutex.hpp"27#include "runtime/semaphore.hpp"28#include "runtime/thread.inline.hpp"29#include "runtime/vmThread.hpp"30#include "runtime/vmOperations.hpp"31#include "unittest.hpp"3233class VM_StopSafepoint : public VM_Operation {34public:35Semaphore* _running;36Semaphore* _test_complete;37VM_StopSafepoint(Semaphore* running, Semaphore* wait_for) :38_running(running), _test_complete(wait_for) {}39VMOp_Type type() const { return VMOp_None; }40bool evaluate_at_safepoint() const { return false; }41void doit() { _running->signal(); _test_complete->wait(); }42};4344// This class and thread keep the non-safepoint op running while we do our testing.45class VMThreadBlocker : public JavaThread {46public:47Semaphore _ready;48Semaphore _unblock;49VMThreadBlocker() {}50virtual ~VMThreadBlocker() {}51const char* get_thread_name_string(char* buf, int buflen) const {52return "VMThreadBlocker";53}54void run() {55this->set_thread_state(_thread_in_vm);56{57MutexLocker ml(Threads_lock);58Threads::add(this);59}60VM_StopSafepoint ss(&_ready, &_unblock);61VMThread::execute(&ss);62}6364// Override as JavaThread::post_run() calls JavaThread::exit which65// expects a valid thread object oop.66virtual void post_run() {67Threads::remove(this, false);68this->smr_delete();69}7071void doit() {72if (os::create_thread(this, os::os_thread)) {73os::start_thread(this);74} else {75ASSERT_TRUE(false);76}77}78void ready() {79_ready.wait();80}81void release() {82_unblock.signal();83}84};8586// For testing in a real JavaThread.87class JavaTestThread : public JavaThread {88public:89Semaphore* _post;90JavaTestThread(Semaphore* post)91: _post(post) {92}93virtual ~JavaTestThread() {}9495const char* get_thread_name_string(char* buf, int buflen) const {96return "JavaTestThread";97}9899void pre_run() {100this->set_thread_state(_thread_in_vm);101{102MutexLocker ml(Threads_lock);103Threads::add(this);104}105}106107virtual void main_run() = 0;108109void run() {110main_run();111}112113// Override as JavaThread::post_run() calls JavaThread::exit which114// expects a valid thread object oop. And we need to call signal.115void post_run() {116Threads::remove(this, false);117_post->signal();118this->smr_delete();119}120121void doit() {122if (os::create_thread(this, os::os_thread)) {123os::start_thread(this);124} else {125ASSERT_TRUE(false);126}127}128};129130template <typename FUNC>131class SingleTestThread : public JavaTestThread {132public:133FUNC& _f;134SingleTestThread(Semaphore* post, FUNC& f)135: JavaTestThread(post), _f(f) {136}137138virtual ~SingleTestThread(){}139140virtual void main_run() {141_f(this);142}143};144145template <typename TESTFUNC>146static void nomt_test_doer(TESTFUNC &f) {147Semaphore post;148149VMThreadBlocker* blocker = new VMThreadBlocker();150blocker->doit();151blocker->ready();152153SingleTestThread<TESTFUNC>* stt = new SingleTestThread<TESTFUNC>(&post, f);154stt->doit();155post.wait();156157blocker->release();158}159160template <typename RUNNER>161static void mt_test_doer() {162Semaphore post;163164VMThreadBlocker* blocker = new VMThreadBlocker();165blocker->doit();166blocker->ready();167168RUNNER* runner = new RUNNER(&post);169runner->doit();170post.wait();171172blocker->release();173}174175#endif // include guard176177178