Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/hotspot/gtest/concurrentTestRunner.inline.hpp
41140 views
1
/*
2
* Copyright (c) 2021, 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_CONCURRENT_TEST_RUNNER_INLINE_HPP
25
#define GTEST_CONCURRENT_TEST_RUNNER_INLINE_HPP
26
27
#include "memory/allocation.hpp"
28
#include "runtime/semaphore.hpp"
29
#include "runtime/thread.inline.hpp"
30
#include "threadHelper.inline.hpp"
31
32
// This file contains helper classes to run unit tests concurrently in multiple threads.
33
34
// Base class for test runnable. Override runUnitTest() to specify what to run.
35
class TestRunnable {
36
public:
37
virtual void runUnitTest() const = 0;
38
};
39
40
// This class represents a thread for a unit test.
41
class UnitTestThread : public JavaTestThread {
42
public:
43
// runnableArg - what to run
44
// doneArg - a semaphore to notify when the thread is done running
45
// testDurationArg - how long to run (in milliseconds)
46
UnitTestThread(TestRunnable* const runnableArg, Semaphore* doneArg, const long testDurationArg) :
47
JavaTestThread(doneArg), runnable(runnableArg), testDuration(testDurationArg) {}
48
49
// from JavaTestThread
50
void main_run() {
51
long stopTime = os::javaTimeMillis() + testDuration;
52
while (os::javaTimeMillis() < stopTime) {
53
runnable->runUnitTest();
54
}
55
}
56
private:
57
TestRunnable* const runnable;
58
const long testDuration;
59
};
60
61
// Helper class for running a given unit test concurrently in multiple threads.
62
class ConcurrentTestRunner {
63
public:
64
// runnableArg - what to run
65
// nrOfThreadsArg - how many threads to use concurrently
66
// testDurationMillisArg - duration for each test run
67
ConcurrentTestRunner(TestRunnable* const runnableArg, int nrOfThreadsArg, long testDurationMillisArg) :
68
unitTestRunnable(runnableArg),
69
nrOfThreads(nrOfThreadsArg),
70
testDurationMillis(testDurationMillisArg) {}
71
72
void run() {
73
Semaphore done(0);
74
75
UnitTestThread** t = NEW_C_HEAP_ARRAY(UnitTestThread*, nrOfThreads, mtInternal);
76
77
for (int i = 0; i < nrOfThreads; i++) {
78
t[i] = new UnitTestThread(unitTestRunnable, &done, testDurationMillis);
79
}
80
81
for (int i = 0; i < nrOfThreads; i++) {
82
t[i]->doit();
83
}
84
85
for (int i = 0; i < nrOfThreads; i++) {
86
done.wait();
87
}
88
89
FREE_C_HEAP_ARRAY(UnitTestThread**, t);
90
}
91
92
private:
93
TestRunnable* const unitTestRunnable;
94
const int nrOfThreads;
95
const long testDurationMillis;
96
};
97
98
#endif // GTEST_CONCURRENT_TEST_RUNNER_INLINE_HPP
99
100