test/hotspot/gtest/threadHelper.inline.hpp
changeset 51179 516acf6956a2
parent 50966 f939a67fea30
child 52618 4ed308e0e89b
equal deleted inserted replaced
51178:416a76fe8067 51179:516acf6956a2
       
     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* _test_complete;
       
    37   VM_StopSafepoint(Semaphore* wait_for) : _test_complete(wait_for) {}
       
    38   VMOp_Type type() const          { return VMOp_None; }
       
    39   Mode evaluation_mode() const    { return _no_safepoint; }
       
    40   bool is_cheap_allocated() const { return false; }
       
    41   void doit()                     { _test_complete->wait(); }
       
    42 };
       
    43 
       
    44 // This class and thread keep the non-safepoint op running while we do our testing.
       
    45 class VMThreadBlocker : public JavaThread {
       
    46 public:
       
    47   Semaphore* _unblock;
       
    48   Semaphore* _done;
       
    49   VMThreadBlocker(Semaphore* ub, Semaphore* done) : _unblock(ub), _done(done) {
       
    50   }
       
    51   virtual ~VMThreadBlocker() {}
       
    52   void run() {
       
    53     this->set_thread_state(_thread_in_vm);
       
    54     {
       
    55       MutexLocker ml(Threads_lock);
       
    56       Threads::add(this);
       
    57     }
       
    58     VM_StopSafepoint ss(_unblock);
       
    59     VMThread::execute(&ss);
       
    60     _done->signal();
       
    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 };
       
    72 
       
    73 // For testing in a real JavaThread.
       
    74 class JavaTestThread : public JavaThread {
       
    75 public:
       
    76   Semaphore* _post;
       
    77   JavaTestThread(Semaphore* post)
       
    78     : _post(post) {
       
    79   }
       
    80   virtual ~JavaTestThread() {}
       
    81 
       
    82   void prerun() {
       
    83     this->set_thread_state(_thread_in_vm);
       
    84     {
       
    85       MutexLocker ml(Threads_lock);
       
    86       Threads::add(this);
       
    87     }
       
    88     {
       
    89       MutexLockerEx ml(SR_lock(), Mutex::_no_safepoint_check_flag);
       
    90     }
       
    91   }
       
    92 
       
    93   virtual void main_run() = 0;
       
    94 
       
    95   void run() {
       
    96     prerun();
       
    97     main_run();
       
    98     postrun();
       
    99   }
       
   100 
       
   101   void postrun() {
       
   102     Threads::remove(this);
       
   103     _post->signal();
       
   104     this->smr_delete();
       
   105   }
       
   106 
       
   107   void doit() {
       
   108     if (os::create_thread(this, os::os_thread)) {
       
   109       os::start_thread(this);
       
   110     } else {
       
   111       ASSERT_TRUE(false);
       
   112     }
       
   113   }
       
   114 };
       
   115 
       
   116 template <typename FUNC>
       
   117 class SingleTestThread : public JavaTestThread {
       
   118 public:
       
   119   FUNC& _f;
       
   120   SingleTestThread(Semaphore* post, FUNC& f)
       
   121     : JavaTestThread(post), _f(f) {
       
   122   }
       
   123 
       
   124   virtual ~SingleTestThread(){}
       
   125 
       
   126   virtual void main_run() {
       
   127     _f(this);
       
   128   }
       
   129 };
       
   130 
       
   131 template <typename TESTFUNC>
       
   132 static void nomt_test_doer(TESTFUNC &f) {
       
   133   Semaphore post, block_done, vmt_done;
       
   134   VMThreadBlocker* blocker = new VMThreadBlocker(&block_done, &vmt_done);
       
   135   blocker->doit();
       
   136   SingleTestThread<TESTFUNC>* stt = new SingleTestThread<TESTFUNC>(&post, f);
       
   137   stt->doit();
       
   138   post.wait();
       
   139   block_done.signal();
       
   140   vmt_done.wait();
       
   141 }
       
   142 
       
   143 template <typename RUNNER>
       
   144 static void mt_test_doer() {
       
   145   Semaphore post, block_done, vmt_done;
       
   146   VMThreadBlocker* blocker = new VMThreadBlocker(&block_done, &vmt_done);
       
   147   blocker->doit();
       
   148   RUNNER* runner = new RUNNER(&post);
       
   149   runner->doit();
       
   150   post.wait();
       
   151   block_done.signal();
       
   152   vmt_done.wait();
       
   153 }
       
   154 
       
   155 #endif // include guard