1 /*
   2  * Copyright (c) 2018, 2019, 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       MutexLocker ml(SR_lock(), Mutex::_no_safepoint_check_flag);
 108     }
 109   }
 110 
 111   virtual void main_run() = 0;
 112 
 113   void run() {
 114     main_run();
 115   }
 116 
 117   // Override as JavaThread::post_run() calls JavaThread::exit which
 118   // expects a valid thread object oop. And we need to call signal.
 119   void post_run() {
 120     Threads::remove(this, false);
 121     _post->signal();
 122     this->smr_delete();
 123   }
 124 
 125   void doit() {
 126     if (os::create_thread(this, os::os_thread)) {
 127       os::start_thread(this);
 128     } else {
 129       ASSERT_TRUE(false);
 130     }
 131   }
 132 };
 133 
 134 template <typename FUNC>
 135 class SingleTestThread : public JavaTestThread {
 136 public:
 137   FUNC& _f;
 138   SingleTestThread(Semaphore* post, FUNC& f)
 139     : JavaTestThread(post), _f(f) {
 140   }
 141 
 142   virtual ~SingleTestThread(){}
 143 
 144   virtual void main_run() {
 145     _f(this);
 146   }
 147 };
 148 
 149 template <typename TESTFUNC>
 150 static void nomt_test_doer(TESTFUNC &f) {
 151   Semaphore post;
 152 
 153   VMThreadBlocker* blocker = new VMThreadBlocker();
 154   blocker->doit();
 155   blocker->ready();
 156 
 157   SingleTestThread<TESTFUNC>* stt = new SingleTestThread<TESTFUNC>(&post, f);
 158   stt->doit();
 159   post.wait();
 160 
 161   blocker->release();
 162 }
 163 
 164 template <typename RUNNER>
 165 static void mt_test_doer() {
 166   Semaphore post;
 167 
 168   VMThreadBlocker* blocker = new VMThreadBlocker();
 169   blocker->doit();
 170   blocker->ready();
 171 
 172   RUNNER* runner = new RUNNER(&post);
 173   runner->doit();
 174   post.wait();
 175 
 176   blocker->release();
 177 }
 178 
 179 #endif // include guard