1 /*
   2  * Copyright (c) 2018, 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.hpp"
  30 #include "runtime/vmThread.hpp"
  31 #include "runtime/vm_operations.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   Mode evaluation_mode() const    { return _no_safepoint; }
  42   bool is_cheap_allocated() const { return false; }
  43   void doit()                     { _running->signal(); _test_complete->wait(); }
  44 };
  45 
  46 // This class and thread keep the non-safepoint op running while we do our testing.
  47 class VMThreadBlocker : public JavaThread {
  48 public:
  49   Semaphore _ready;
  50   Semaphore _unblock;
  51   VMThreadBlocker() {}
  52   virtual ~VMThreadBlocker() {}
  53   void run() {
  54     this->set_thread_state(_thread_in_vm);
  55     {
  56       MutexLocker ml(Threads_lock);
  57       Threads::add(this);
  58     }
  59     VM_StopSafepoint ss(&_ready, &_unblock);
  60     VMThread::execute(&ss);
  61     Threads::remove(this);
  62     this->smr_delete();
  63   }
  64   void doit() {
  65     if (os::create_thread(this, os::os_thread)) {
  66       os::start_thread(this);
  67     } else {
  68       ASSERT_TRUE(false);
  69     }
  70   }
  71   void ready() {
  72     _ready.wait();
  73   }
  74   void release() {
  75     _unblock.signal();
  76   }
  77 };
  78 
  79 // For testing in a real JavaThread.
  80 class JavaTestThread : public JavaThread {
  81 public:
  82   Semaphore* _post;
  83   JavaTestThread(Semaphore* post)
  84     : _post(post) {
  85   }
  86   virtual ~JavaTestThread() {}
  87 
  88   void prerun() {
  89     this->set_thread_state(_thread_in_vm);
  90     {
  91       MutexLocker ml(Threads_lock);
  92       Threads::add(this);
  93     }
  94     {
  95       MutexLockerEx ml(SR_lock(), Mutex::_no_safepoint_check_flag);
  96     }
  97   }
  98 
  99   virtual void main_run() = 0;
 100 
 101   void run() {
 102     prerun();
 103     main_run();
 104     postrun();
 105   }
 106 
 107   void postrun() {
 108     Threads::remove(this);
 109     _post->signal();
 110     this->smr_delete();
 111   }
 112 
 113   void doit() {
 114     if (os::create_thread(this, os::os_thread)) {
 115       os::start_thread(this);
 116     } else {
 117       ASSERT_TRUE(false);
 118     }
 119   }
 120 };
 121 
 122 template <typename FUNC>
 123 class SingleTestThread : public JavaTestThread {
 124 public:
 125   FUNC& _f;
 126   SingleTestThread(Semaphore* post, FUNC& f)
 127     : JavaTestThread(post), _f(f) {
 128   }
 129 
 130   virtual ~SingleTestThread(){}
 131 
 132   virtual void main_run() {
 133     _f(this);
 134   }
 135 };
 136 
 137 template <typename TESTFUNC>
 138 static void nomt_test_doer(TESTFUNC &f) {
 139   Semaphore post;
 140 
 141   VMThreadBlocker* blocker = new VMThreadBlocker();
 142   blocker->doit();
 143   blocker->ready();
 144 
 145   SingleTestThread<TESTFUNC>* stt = new SingleTestThread<TESTFUNC>(&post, f);
 146   stt->doit();
 147   post.wait();
 148 
 149   blocker->release();
 150 }
 151 
 152 template <typename RUNNER>
 153 static void mt_test_doer() {
 154   Semaphore post;
 155 
 156   VMThreadBlocker* blocker = new VMThreadBlocker();
 157   blocker->doit();
 158   blocker->ready();
 159 
 160   RUNNER* runner = new RUNNER(&post);
 161   runner->doit();
 162   post.wait();
 163 
 164   blocker->release();
 165 }
 166 
 167 #endif // include guard