author | mikael |
Mon, 09 Apr 2018 10:19:33 -0700 | |
changeset 49552 | 05883543ee2a |
parent 48804 | 8f451978683c |
child 54851 | f67269c129f9 |
permissions | -rw-r--r-- |
38255 | 1 |
/* |
47659 | 2 |
* Copyright (c) 2016, 2017, Oracle and/or its affiliates. All rights reserved. |
38255 | 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 |
||
25 |
#include <stdio.h> |
|
26 |
#include <string.h> |
|
27 |
#include <stdlib.h> |
|
28 |
#ifdef __APPLE__ |
|
29 |
# include <dlfcn.h> |
|
30 |
#endif |
|
31 |
||
46684
a8121315a5b9
8179327: gtestLauncher should run tests on a separate thread (optionally)
stuefe
parents:
41672
diff
changeset
|
32 |
#ifdef _WIN32 |
a8121315a5b9
8179327: gtestLauncher should run tests on a separate thread (optionally)
stuefe
parents:
41672
diff
changeset
|
33 |
#include <windows.h> |
a8121315a5b9
8179327: gtestLauncher should run tests on a separate thread (optionally)
stuefe
parents:
41672
diff
changeset
|
34 |
#else |
a8121315a5b9
8179327: gtestLauncher should run tests on a separate thread (optionally)
stuefe
parents:
41672
diff
changeset
|
35 |
#include <pthread.h> |
a8121315a5b9
8179327: gtestLauncher should run tests on a separate thread (optionally)
stuefe
parents:
41672
diff
changeset
|
36 |
#endif |
a8121315a5b9
8179327: gtestLauncher should run tests on a separate thread (optionally)
stuefe
parents:
41672
diff
changeset
|
37 |
|
47659 | 38 |
#include "jni.h" |
38255 | 39 |
#include "unittest.hpp" |
40 |
||
46684
a8121315a5b9
8179327: gtestLauncher should run tests on a separate thread (optionally)
stuefe
parents:
41672
diff
changeset
|
41 |
// Default value for -new-thread option: true on AIX because we run into |
a8121315a5b9
8179327: gtestLauncher should run tests on a separate thread (optionally)
stuefe
parents:
41672
diff
changeset
|
42 |
// problems when attempting to initialize the JVM on the primordial thread. |
a8121315a5b9
8179327: gtestLauncher should run tests on a separate thread (optionally)
stuefe
parents:
41672
diff
changeset
|
43 |
#ifdef _AIX |
a8121315a5b9
8179327: gtestLauncher should run tests on a separate thread (optionally)
stuefe
parents:
41672
diff
changeset
|
44 |
const static bool DEFAULT_SPAWN_IN_NEW_THREAD = true; |
a8121315a5b9
8179327: gtestLauncher should run tests on a separate thread (optionally)
stuefe
parents:
41672
diff
changeset
|
45 |
#else |
a8121315a5b9
8179327: gtestLauncher should run tests on a separate thread (optionally)
stuefe
parents:
41672
diff
changeset
|
46 |
const static bool DEFAULT_SPAWN_IN_NEW_THREAD = false; |
a8121315a5b9
8179327: gtestLauncher should run tests on a separate thread (optionally)
stuefe
parents:
41672
diff
changeset
|
47 |
#endif |
38255 | 48 |
|
41672
b40421cd8b48
8166129: hitting vmassert during gtest execution doesn't generate core and hs_err files
iignatyev
parents:
38255
diff
changeset
|
49 |
static bool is_prefix(const char* prefix, const char* str) { |
b40421cd8b48
8166129: hitting vmassert during gtest execution doesn't generate core and hs_err files
iignatyev
parents:
38255
diff
changeset
|
50 |
return strncmp(str, prefix, strlen(prefix)) == 0; |
b40421cd8b48
8166129: hitting vmassert during gtest execution doesn't generate core and hs_err files
iignatyev
parents:
38255
diff
changeset
|
51 |
} |
b40421cd8b48
8166129: hitting vmassert during gtest execution doesn't generate core and hs_err files
iignatyev
parents:
38255
diff
changeset
|
52 |
|
b40421cd8b48
8166129: hitting vmassert during gtest execution doesn't generate core and hs_err files
iignatyev
parents:
38255
diff
changeset
|
53 |
static bool is_suffix(const char* suffix, const char* str) { |
b40421cd8b48
8166129: hitting vmassert during gtest execution doesn't generate core and hs_err files
iignatyev
parents:
38255
diff
changeset
|
54 |
size_t suffix_len = strlen(suffix); |
b40421cd8b48
8166129: hitting vmassert during gtest execution doesn't generate core and hs_err files
iignatyev
parents:
38255
diff
changeset
|
55 |
size_t str_len = strlen(str); |
b40421cd8b48
8166129: hitting vmassert during gtest execution doesn't generate core and hs_err files
iignatyev
parents:
38255
diff
changeset
|
56 |
if (str_len < suffix_len) { |
b40421cd8b48
8166129: hitting vmassert during gtest execution doesn't generate core and hs_err files
iignatyev
parents:
38255
diff
changeset
|
57 |
return false; |
b40421cd8b48
8166129: hitting vmassert during gtest execution doesn't generate core and hs_err files
iignatyev
parents:
38255
diff
changeset
|
58 |
} |
b40421cd8b48
8166129: hitting vmassert during gtest execution doesn't generate core and hs_err files
iignatyev
parents:
38255
diff
changeset
|
59 |
return strncmp(str + (str_len - suffix_len), suffix, suffix_len) == 0; |
b40421cd8b48
8166129: hitting vmassert during gtest execution doesn't generate core and hs_err files
iignatyev
parents:
38255
diff
changeset
|
60 |
} |
b40421cd8b48
8166129: hitting vmassert during gtest execution doesn't generate core and hs_err files
iignatyev
parents:
38255
diff
changeset
|
61 |
|
b40421cd8b48
8166129: hitting vmassert during gtest execution doesn't generate core and hs_err files
iignatyev
parents:
38255
diff
changeset
|
62 |
|
b40421cd8b48
8166129: hitting vmassert during gtest execution doesn't generate core and hs_err files
iignatyev
parents:
38255
diff
changeset
|
63 |
static int init_jvm(int argc, char **argv, bool disable_error_handling) { |
38255 | 64 |
// don't care about the program name |
65 |
argc--; |
|
66 |
argv++; |
|
67 |
||
41672
b40421cd8b48
8166129: hitting vmassert during gtest execution doesn't generate core and hs_err files
iignatyev
parents:
38255
diff
changeset
|
68 |
int extra_jvm_args = disable_error_handling ? 4 : 2; |
38255 | 69 |
int num_jvm_options = argc + extra_jvm_args; |
70 |
||
71 |
JavaVMOption* options = new JavaVMOption[num_jvm_options]; |
|
72 |
options[0].optionString = (char*) "-Dsun.java.launcher.is_altjvm=true"; |
|
73 |
options[1].optionString = (char*) "-XX:+ExecutingUnitTests"; |
|
74 |
||
41672
b40421cd8b48
8166129: hitting vmassert during gtest execution doesn't generate core and hs_err files
iignatyev
parents:
38255
diff
changeset
|
75 |
if (disable_error_handling) { |
b40421cd8b48
8166129: hitting vmassert during gtest execution doesn't generate core and hs_err files
iignatyev
parents:
38255
diff
changeset
|
76 |
// don't create core files or hs_err files executing assert tests |
38255 | 77 |
options[2].optionString = (char*) "-XX:+SuppressFatalErrorMessage"; |
78 |
options[3].optionString = (char*) "-XX:-CreateCoredumpOnCrash"; |
|
79 |
} |
|
80 |
||
81 |
for (int i = 0; i < argc; i++) { |
|
82 |
options[extra_jvm_args + i].optionString = argv[i]; |
|
83 |
} |
|
84 |
||
85 |
JavaVMInitArgs args; |
|
86 |
args.version = JNI_VERSION_1_8; |
|
87 |
args.nOptions = num_jvm_options; |
|
88 |
args.options = options; |
|
89 |
||
90 |
JavaVM* jvm; |
|
91 |
JNIEnv* env; |
|
92 |
||
93 |
return JNI_CreateJavaVM(&jvm, (void**)&env, &args); |
|
94 |
} |
|
95 |
||
96 |
class JVMInitializerListener : public ::testing::EmptyTestEventListener { |
|
97 |
private: |
|
98 |
int _argc; |
|
99 |
char** _argv; |
|
100 |
bool _is_initialized; |
|
101 |
||
102 |
void initialize_jvm() { |
|
103 |
} |
|
104 |
||
105 |
public: |
|
106 |
JVMInitializerListener(int argc, char** argv) : |
|
107 |
_argc(argc), _argv(argv), _is_initialized(false) { |
|
108 |
} |
|
109 |
||
110 |
virtual void OnTestStart(const ::testing::TestInfo& test_info) { |
|
111 |
const char* name = test_info.name(); |
|
41672
b40421cd8b48
8166129: hitting vmassert during gtest execution doesn't generate core and hs_err files
iignatyev
parents:
38255
diff
changeset
|
112 |
if (!_is_initialized && is_suffix("_test_vm", name)) { |
b40421cd8b48
8166129: hitting vmassert during gtest execution doesn't generate core and hs_err files
iignatyev
parents:
38255
diff
changeset
|
113 |
// we want to have hs_err and core files when we execute regular tests |
48804
8f451978683c
8170941: Executing gtest with invalid -Xlog argument crashes gtestLauncher
mlarsson
parents:
47659
diff
changeset
|
114 |
int ret_val = init_jvm(_argc, _argv, false); |
8f451978683c
8170941: Executing gtest with invalid -Xlog argument crashes gtestLauncher
mlarsson
parents:
47659
diff
changeset
|
115 |
if (ret_val != 0) { |
8f451978683c
8170941: Executing gtest with invalid -Xlog argument crashes gtestLauncher
mlarsson
parents:
47659
diff
changeset
|
116 |
ADD_FAILURE() << "Could not initialize the JVM"; |
8f451978683c
8170941: Executing gtest with invalid -Xlog argument crashes gtestLauncher
mlarsson
parents:
47659
diff
changeset
|
117 |
exit(1); |
8f451978683c
8170941: Executing gtest with invalid -Xlog argument crashes gtestLauncher
mlarsson
parents:
47659
diff
changeset
|
118 |
} |
38255 | 119 |
_is_initialized = true; |
120 |
} |
|
121 |
} |
|
122 |
}; |
|
123 |
||
124 |
static char* get_java_home_arg(int argc, char** argv) { |
|
125 |
for (int i = 0; i < argc; i++) { |
|
126 |
if (strncmp(argv[i], "-jdk", strlen(argv[i])) == 0) { |
|
127 |
return argv[i+1]; |
|
128 |
} |
|
129 |
if (is_prefix("--jdk=", argv[i])) { |
|
130 |
return argv[i] + strlen("--jdk="); |
|
131 |
} |
|
132 |
if (is_prefix("-jdk:", argv[i])) { |
|
133 |
return argv[i] + strlen("-jdk:"); |
|
134 |
} |
|
135 |
} |
|
136 |
return NULL; |
|
137 |
} |
|
138 |
||
46684
a8121315a5b9
8179327: gtestLauncher should run tests on a separate thread (optionally)
stuefe
parents:
41672
diff
changeset
|
139 |
static bool get_spawn_new_main_thread_arg(int argc, char** argv) { |
a8121315a5b9
8179327: gtestLauncher should run tests on a separate thread (optionally)
stuefe
parents:
41672
diff
changeset
|
140 |
// -new-thread[=(true|false)] |
a8121315a5b9
8179327: gtestLauncher should run tests on a separate thread (optionally)
stuefe
parents:
41672
diff
changeset
|
141 |
for (int i = 0; i < argc; i++) { |
a8121315a5b9
8179327: gtestLauncher should run tests on a separate thread (optionally)
stuefe
parents:
41672
diff
changeset
|
142 |
if (is_prefix("-new-thread", argv[i])) { |
a8121315a5b9
8179327: gtestLauncher should run tests on a separate thread (optionally)
stuefe
parents:
41672
diff
changeset
|
143 |
const char* v = argv[i] + strlen("-new-thread"); |
a8121315a5b9
8179327: gtestLauncher should run tests on a separate thread (optionally)
stuefe
parents:
41672
diff
changeset
|
144 |
if (strlen(v) == 0) { |
a8121315a5b9
8179327: gtestLauncher should run tests on a separate thread (optionally)
stuefe
parents:
41672
diff
changeset
|
145 |
return true; |
a8121315a5b9
8179327: gtestLauncher should run tests on a separate thread (optionally)
stuefe
parents:
41672
diff
changeset
|
146 |
} else { |
a8121315a5b9
8179327: gtestLauncher should run tests on a separate thread (optionally)
stuefe
parents:
41672
diff
changeset
|
147 |
if (strcmp(v, "=true") == 0) { |
a8121315a5b9
8179327: gtestLauncher should run tests on a separate thread (optionally)
stuefe
parents:
41672
diff
changeset
|
148 |
return true; |
a8121315a5b9
8179327: gtestLauncher should run tests on a separate thread (optionally)
stuefe
parents:
41672
diff
changeset
|
149 |
} else if (strcmp(v, "=false") == 0) { |
a8121315a5b9
8179327: gtestLauncher should run tests on a separate thread (optionally)
stuefe
parents:
41672
diff
changeset
|
150 |
return false; |
a8121315a5b9
8179327: gtestLauncher should run tests on a separate thread (optionally)
stuefe
parents:
41672
diff
changeset
|
151 |
} else { |
a8121315a5b9
8179327: gtestLauncher should run tests on a separate thread (optionally)
stuefe
parents:
41672
diff
changeset
|
152 |
fprintf(stderr, "Invalid value for -new-thread (%s)", v); |
a8121315a5b9
8179327: gtestLauncher should run tests on a separate thread (optionally)
stuefe
parents:
41672
diff
changeset
|
153 |
} |
a8121315a5b9
8179327: gtestLauncher should run tests on a separate thread (optionally)
stuefe
parents:
41672
diff
changeset
|
154 |
} |
a8121315a5b9
8179327: gtestLauncher should run tests on a separate thread (optionally)
stuefe
parents:
41672
diff
changeset
|
155 |
} |
a8121315a5b9
8179327: gtestLauncher should run tests on a separate thread (optionally)
stuefe
parents:
41672
diff
changeset
|
156 |
} |
a8121315a5b9
8179327: gtestLauncher should run tests on a separate thread (optionally)
stuefe
parents:
41672
diff
changeset
|
157 |
return DEFAULT_SPAWN_IN_NEW_THREAD; |
a8121315a5b9
8179327: gtestLauncher should run tests on a separate thread (optionally)
stuefe
parents:
41672
diff
changeset
|
158 |
} |
a8121315a5b9
8179327: gtestLauncher should run tests on a separate thread (optionally)
stuefe
parents:
41672
diff
changeset
|
159 |
|
38255 | 160 |
static int num_args_to_skip(char* arg) { |
161 |
if (strcmp(arg, "-jdk") == 0) { |
|
162 |
return 2; // skip the argument after -jdk as well |
|
163 |
} |
|
164 |
if (is_prefix("--jdk=", arg)) { |
|
165 |
return 1; |
|
166 |
} |
|
167 |
if (is_prefix("-jdk:", arg)) { |
|
168 |
return 1; |
|
169 |
} |
|
46684
a8121315a5b9
8179327: gtestLauncher should run tests on a separate thread (optionally)
stuefe
parents:
41672
diff
changeset
|
170 |
if (is_prefix("-new-thread", arg)) { |
a8121315a5b9
8179327: gtestLauncher should run tests on a separate thread (optionally)
stuefe
parents:
41672
diff
changeset
|
171 |
return 1; |
a8121315a5b9
8179327: gtestLauncher should run tests on a separate thread (optionally)
stuefe
parents:
41672
diff
changeset
|
172 |
} |
38255 | 173 |
return 0; |
174 |
} |
|
175 |
||
176 |
static char** remove_test_runner_arguments(int* argcp, char **argv) { |
|
177 |
int argc = *argcp; |
|
178 |
char** new_argv = (char**) malloc(sizeof(char*) * argc); |
|
179 |
int new_argc = 0; |
|
180 |
||
181 |
int i = 0; |
|
182 |
while (i < argc) { |
|
183 |
int args_to_skip = num_args_to_skip(argv[i]); |
|
184 |
if (args_to_skip == 0) { |
|
185 |
new_argv[new_argc] = argv[i]; |
|
186 |
i++; |
|
187 |
new_argc++; |
|
188 |
} else { |
|
189 |
i += num_args_to_skip(argv[i]); |
|
190 |
} |
|
191 |
} |
|
192 |
||
193 |
*argcp = new_argc; |
|
194 |
return new_argv; |
|
195 |
} |
|
196 |
||
46684
a8121315a5b9
8179327: gtestLauncher should run tests on a separate thread (optionally)
stuefe
parents:
41672
diff
changeset
|
197 |
static void runUnitTestsInner(int argc, char** argv) { |
41672
b40421cd8b48
8166129: hitting vmassert during gtest execution doesn't generate core and hs_err files
iignatyev
parents:
38255
diff
changeset
|
198 |
::testing::InitGoogleTest(&argc, argv); |
b40421cd8b48
8166129: hitting vmassert during gtest execution doesn't generate core and hs_err files
iignatyev
parents:
38255
diff
changeset
|
199 |
::testing::GTEST_FLAG(death_test_style) = "threadsafe"; |
b40421cd8b48
8166129: hitting vmassert during gtest execution doesn't generate core and hs_err files
iignatyev
parents:
38255
diff
changeset
|
200 |
|
b40421cd8b48
8166129: hitting vmassert during gtest execution doesn't generate core and hs_err files
iignatyev
parents:
38255
diff
changeset
|
201 |
bool is_vmassert_test = false; |
b40421cd8b48
8166129: hitting vmassert during gtest execution doesn't generate core and hs_err files
iignatyev
parents:
38255
diff
changeset
|
202 |
bool is_othervm_test = false; |
b40421cd8b48
8166129: hitting vmassert during gtest execution doesn't generate core and hs_err files
iignatyev
parents:
38255
diff
changeset
|
203 |
// death tests facility is used for both regular death tests, other vm and vmassert tests |
b40421cd8b48
8166129: hitting vmassert during gtest execution doesn't generate core and hs_err files
iignatyev
parents:
38255
diff
changeset
|
204 |
if (::testing::internal::GTEST_FLAG(internal_run_death_test).length() > 0) { |
b40421cd8b48
8166129: hitting vmassert during gtest execution doesn't generate core and hs_err files
iignatyev
parents:
38255
diff
changeset
|
205 |
// when we execute death test, filter value equals to test name |
b40421cd8b48
8166129: hitting vmassert during gtest execution doesn't generate core and hs_err files
iignatyev
parents:
38255
diff
changeset
|
206 |
const char* test_name = ::testing::GTEST_FLAG(filter).c_str(); |
b40421cd8b48
8166129: hitting vmassert during gtest execution doesn't generate core and hs_err files
iignatyev
parents:
38255
diff
changeset
|
207 |
const char* const othervm_suffix = "_other_vm_test"; // TEST_OTHER_VM |
b40421cd8b48
8166129: hitting vmassert during gtest execution doesn't generate core and hs_err files
iignatyev
parents:
38255
diff
changeset
|
208 |
const char* const vmassert_suffix = "_vm_assert_test"; // TEST_VM_ASSERT(_MSG) |
b40421cd8b48
8166129: hitting vmassert during gtest execution doesn't generate core and hs_err files
iignatyev
parents:
38255
diff
changeset
|
209 |
if (is_suffix(othervm_suffix, test_name)) { |
b40421cd8b48
8166129: hitting vmassert during gtest execution doesn't generate core and hs_err files
iignatyev
parents:
38255
diff
changeset
|
210 |
is_othervm_test = true; |
b40421cd8b48
8166129: hitting vmassert during gtest execution doesn't generate core and hs_err files
iignatyev
parents:
38255
diff
changeset
|
211 |
} else if (is_suffix(vmassert_suffix, test_name)) { |
b40421cd8b48
8166129: hitting vmassert during gtest execution doesn't generate core and hs_err files
iignatyev
parents:
38255
diff
changeset
|
212 |
is_vmassert_test = true; |
38255 | 213 |
} |
214 |
} |
|
215 |
||
216 |
char* java_home = get_java_home_arg(argc, argv); |
|
217 |
if (java_home == NULL) { |
|
218 |
fprintf(stderr, "ERROR: You must specify a JDK to use for running the unit tests.\n"); |
|
219 |
exit(1); |
|
220 |
} |
|
221 |
#ifndef _WIN32 |
|
222 |
int overwrite = 1; // overwrite an eventual existing value for JAVA_HOME |
|
223 |
setenv("JAVA_HOME", java_home, overwrite); |
|
224 |
||
225 |
// workaround for JDK-7131356 |
|
226 |
#ifdef __APPLE__ |
|
227 |
size_t len = strlen(java_home) + strlen("/lib/jli/libjli.dylib") + 1; |
|
228 |
char* path = new char[len]; |
|
229 |
snprintf(path, len, "%s/lib/jli/libjli.dylib", java_home); |
|
230 |
dlopen(path, RTLD_NOW | RTLD_GLOBAL); |
|
231 |
#endif // __APPLE__ |
|
232 |
||
233 |
#else // _WIN32 |
|
234 |
char* java_home_var = "_ALT_JAVA_HOME_DIR"; |
|
235 |
size_t len = strlen(java_home) + strlen(java_home_var) + 2; |
|
236 |
char * envString = new char[len]; |
|
237 |
sprintf_s(envString, len, "%s=%s", java_home_var, java_home); |
|
238 |
_putenv(envString); |
|
239 |
#endif // _WIN32 |
|
240 |
argv = remove_test_runner_arguments(&argc, argv); |
|
241 |
||
41672
b40421cd8b48
8166129: hitting vmassert during gtest execution doesn't generate core and hs_err files
iignatyev
parents:
38255
diff
changeset
|
242 |
if (is_vmassert_test || is_othervm_test) { |
b40421cd8b48
8166129: hitting vmassert during gtest execution doesn't generate core and hs_err files
iignatyev
parents:
38255
diff
changeset
|
243 |
// both vmassert and other vm tests require inited jvm |
b40421cd8b48
8166129: hitting vmassert during gtest execution doesn't generate core and hs_err files
iignatyev
parents:
38255
diff
changeset
|
244 |
// but only vmassert tests disable hs_err and core file generation |
b40421cd8b48
8166129: hitting vmassert during gtest execution doesn't generate core and hs_err files
iignatyev
parents:
38255
diff
changeset
|
245 |
if (init_jvm(argc, argv, is_vmassert_test) != 0) { |
38255 | 246 |
abort(); |
247 |
} |
|
248 |
} else { |
|
249 |
::testing::TestEventListeners& listeners = ::testing::UnitTest::GetInstance()->listeners(); |
|
250 |
listeners.Append(new JVMInitializerListener(argc, argv)); |
|
251 |
} |
|
252 |
||
253 |
int result = RUN_ALL_TESTS(); |
|
254 |
if (result != 0) { |
|
255 |
fprintf(stderr, "ERROR: RUN_ALL_TESTS() failed. Error %d\n", result); |
|
256 |
exit(2); |
|
257 |
} |
|
258 |
} |
|
259 |
||
46684
a8121315a5b9
8179327: gtestLauncher should run tests on a separate thread (optionally)
stuefe
parents:
41672
diff
changeset
|
260 |
// Thread support for -new-thread option |
a8121315a5b9
8179327: gtestLauncher should run tests on a separate thread (optionally)
stuefe
parents:
41672
diff
changeset
|
261 |
|
a8121315a5b9
8179327: gtestLauncher should run tests on a separate thread (optionally)
stuefe
parents:
41672
diff
changeset
|
262 |
struct args_t { |
a8121315a5b9
8179327: gtestLauncher should run tests on a separate thread (optionally)
stuefe
parents:
41672
diff
changeset
|
263 |
int argc; char** argv; |
a8121315a5b9
8179327: gtestLauncher should run tests on a separate thread (optionally)
stuefe
parents:
41672
diff
changeset
|
264 |
}; |
a8121315a5b9
8179327: gtestLauncher should run tests on a separate thread (optionally)
stuefe
parents:
41672
diff
changeset
|
265 |
|
a8121315a5b9
8179327: gtestLauncher should run tests on a separate thread (optionally)
stuefe
parents:
41672
diff
changeset
|
266 |
#define STACK_SIZE 0x200000 |
a8121315a5b9
8179327: gtestLauncher should run tests on a separate thread (optionally)
stuefe
parents:
41672
diff
changeset
|
267 |
|
a8121315a5b9
8179327: gtestLauncher should run tests on a separate thread (optionally)
stuefe
parents:
41672
diff
changeset
|
268 |
#ifdef _WIN32 |
a8121315a5b9
8179327: gtestLauncher should run tests on a separate thread (optionally)
stuefe
parents:
41672
diff
changeset
|
269 |
|
a8121315a5b9
8179327: gtestLauncher should run tests on a separate thread (optionally)
stuefe
parents:
41672
diff
changeset
|
270 |
static DWORD WINAPI thread_wrapper(void* p) { |
a8121315a5b9
8179327: gtestLauncher should run tests on a separate thread (optionally)
stuefe
parents:
41672
diff
changeset
|
271 |
const args_t* const p_args = (const args_t*) p; |
a8121315a5b9
8179327: gtestLauncher should run tests on a separate thread (optionally)
stuefe
parents:
41672
diff
changeset
|
272 |
runUnitTestsInner(p_args->argc, p_args->argv); |
a8121315a5b9
8179327: gtestLauncher should run tests on a separate thread (optionally)
stuefe
parents:
41672
diff
changeset
|
273 |
return 0; |
a8121315a5b9
8179327: gtestLauncher should run tests on a separate thread (optionally)
stuefe
parents:
41672
diff
changeset
|
274 |
} |
a8121315a5b9
8179327: gtestLauncher should run tests on a separate thread (optionally)
stuefe
parents:
41672
diff
changeset
|
275 |
|
a8121315a5b9
8179327: gtestLauncher should run tests on a separate thread (optionally)
stuefe
parents:
41672
diff
changeset
|
276 |
static void run_in_new_thread(const args_t* args) { |
a8121315a5b9
8179327: gtestLauncher should run tests on a separate thread (optionally)
stuefe
parents:
41672
diff
changeset
|
277 |
HANDLE hdl; |
a8121315a5b9
8179327: gtestLauncher should run tests on a separate thread (optionally)
stuefe
parents:
41672
diff
changeset
|
278 |
hdl = CreateThread(NULL, STACK_SIZE, thread_wrapper, (void*)args, 0, NULL); |
a8121315a5b9
8179327: gtestLauncher should run tests on a separate thread (optionally)
stuefe
parents:
41672
diff
changeset
|
279 |
if (hdl == NULL) { |
a8121315a5b9
8179327: gtestLauncher should run tests on a separate thread (optionally)
stuefe
parents:
41672
diff
changeset
|
280 |
fprintf(stderr, "Failed to create main thread\n"); |
a8121315a5b9
8179327: gtestLauncher should run tests on a separate thread (optionally)
stuefe
parents:
41672
diff
changeset
|
281 |
exit(2); |
a8121315a5b9
8179327: gtestLauncher should run tests on a separate thread (optionally)
stuefe
parents:
41672
diff
changeset
|
282 |
} |
a8121315a5b9
8179327: gtestLauncher should run tests on a separate thread (optionally)
stuefe
parents:
41672
diff
changeset
|
283 |
WaitForSingleObject(hdl, INFINITE); |
a8121315a5b9
8179327: gtestLauncher should run tests on a separate thread (optionally)
stuefe
parents:
41672
diff
changeset
|
284 |
} |
a8121315a5b9
8179327: gtestLauncher should run tests on a separate thread (optionally)
stuefe
parents:
41672
diff
changeset
|
285 |
|
a8121315a5b9
8179327: gtestLauncher should run tests on a separate thread (optionally)
stuefe
parents:
41672
diff
changeset
|
286 |
#else |
a8121315a5b9
8179327: gtestLauncher should run tests on a separate thread (optionally)
stuefe
parents:
41672
diff
changeset
|
287 |
|
a8121315a5b9
8179327: gtestLauncher should run tests on a separate thread (optionally)
stuefe
parents:
41672
diff
changeset
|
288 |
extern "C" void* thread_wrapper(void* p) { |
a8121315a5b9
8179327: gtestLauncher should run tests on a separate thread (optionally)
stuefe
parents:
41672
diff
changeset
|
289 |
const args_t* const p_args = (const args_t*) p; |
a8121315a5b9
8179327: gtestLauncher should run tests on a separate thread (optionally)
stuefe
parents:
41672
diff
changeset
|
290 |
runUnitTestsInner(p_args->argc, p_args->argv); |
a8121315a5b9
8179327: gtestLauncher should run tests on a separate thread (optionally)
stuefe
parents:
41672
diff
changeset
|
291 |
return 0; |
a8121315a5b9
8179327: gtestLauncher should run tests on a separate thread (optionally)
stuefe
parents:
41672
diff
changeset
|
292 |
} |
a8121315a5b9
8179327: gtestLauncher should run tests on a separate thread (optionally)
stuefe
parents:
41672
diff
changeset
|
293 |
|
a8121315a5b9
8179327: gtestLauncher should run tests on a separate thread (optionally)
stuefe
parents:
41672
diff
changeset
|
294 |
static void run_in_new_thread(const args_t* args) { |
a8121315a5b9
8179327: gtestLauncher should run tests on a separate thread (optionally)
stuefe
parents:
41672
diff
changeset
|
295 |
pthread_t tid; |
a8121315a5b9
8179327: gtestLauncher should run tests on a separate thread (optionally)
stuefe
parents:
41672
diff
changeset
|
296 |
pthread_attr_t attr; |
a8121315a5b9
8179327: gtestLauncher should run tests on a separate thread (optionally)
stuefe
parents:
41672
diff
changeset
|
297 |
|
a8121315a5b9
8179327: gtestLauncher should run tests on a separate thread (optionally)
stuefe
parents:
41672
diff
changeset
|
298 |
pthread_attr_init(&attr); |
a8121315a5b9
8179327: gtestLauncher should run tests on a separate thread (optionally)
stuefe
parents:
41672
diff
changeset
|
299 |
pthread_attr_setstacksize(&attr, STACK_SIZE); |
a8121315a5b9
8179327: gtestLauncher should run tests on a separate thread (optionally)
stuefe
parents:
41672
diff
changeset
|
300 |
|
a8121315a5b9
8179327: gtestLauncher should run tests on a separate thread (optionally)
stuefe
parents:
41672
diff
changeset
|
301 |
if (pthread_create(&tid, &attr, thread_wrapper, (void*)args) != 0) { |
a8121315a5b9
8179327: gtestLauncher should run tests on a separate thread (optionally)
stuefe
parents:
41672
diff
changeset
|
302 |
fprintf(stderr, "Failed to create main thread\n"); |
a8121315a5b9
8179327: gtestLauncher should run tests on a separate thread (optionally)
stuefe
parents:
41672
diff
changeset
|
303 |
exit(2); |
a8121315a5b9
8179327: gtestLauncher should run tests on a separate thread (optionally)
stuefe
parents:
41672
diff
changeset
|
304 |
} |
a8121315a5b9
8179327: gtestLauncher should run tests on a separate thread (optionally)
stuefe
parents:
41672
diff
changeset
|
305 |
|
a8121315a5b9
8179327: gtestLauncher should run tests on a separate thread (optionally)
stuefe
parents:
41672
diff
changeset
|
306 |
if (pthread_join(tid, NULL) != 0) { |
a8121315a5b9
8179327: gtestLauncher should run tests on a separate thread (optionally)
stuefe
parents:
41672
diff
changeset
|
307 |
fprintf(stderr, "Failed to join main thread\n"); |
a8121315a5b9
8179327: gtestLauncher should run tests on a separate thread (optionally)
stuefe
parents:
41672
diff
changeset
|
308 |
exit(2); |
a8121315a5b9
8179327: gtestLauncher should run tests on a separate thread (optionally)
stuefe
parents:
41672
diff
changeset
|
309 |
} |
a8121315a5b9
8179327: gtestLauncher should run tests on a separate thread (optionally)
stuefe
parents:
41672
diff
changeset
|
310 |
} |
a8121315a5b9
8179327: gtestLauncher should run tests on a separate thread (optionally)
stuefe
parents:
41672
diff
changeset
|
311 |
|
a8121315a5b9
8179327: gtestLauncher should run tests on a separate thread (optionally)
stuefe
parents:
41672
diff
changeset
|
312 |
#endif |
a8121315a5b9
8179327: gtestLauncher should run tests on a separate thread (optionally)
stuefe
parents:
41672
diff
changeset
|
313 |
|
a8121315a5b9
8179327: gtestLauncher should run tests on a separate thread (optionally)
stuefe
parents:
41672
diff
changeset
|
314 |
extern "C" |
a8121315a5b9
8179327: gtestLauncher should run tests on a separate thread (optionally)
stuefe
parents:
41672
diff
changeset
|
315 |
JNIEXPORT void JNICALL runUnitTests(int argc, char** argv) { |
a8121315a5b9
8179327: gtestLauncher should run tests on a separate thread (optionally)
stuefe
parents:
41672
diff
changeset
|
316 |
const bool spawn_new_main_thread = get_spawn_new_main_thread_arg(argc, argv); |
a8121315a5b9
8179327: gtestLauncher should run tests on a separate thread (optionally)
stuefe
parents:
41672
diff
changeset
|
317 |
if (spawn_new_main_thread) { |
a8121315a5b9
8179327: gtestLauncher should run tests on a separate thread (optionally)
stuefe
parents:
41672
diff
changeset
|
318 |
args_t args; |
a8121315a5b9
8179327: gtestLauncher should run tests on a separate thread (optionally)
stuefe
parents:
41672
diff
changeset
|
319 |
args.argc = argc; |
a8121315a5b9
8179327: gtestLauncher should run tests on a separate thread (optionally)
stuefe
parents:
41672
diff
changeset
|
320 |
args.argv = argv; |
a8121315a5b9
8179327: gtestLauncher should run tests on a separate thread (optionally)
stuefe
parents:
41672
diff
changeset
|
321 |
run_in_new_thread(&args); |
a8121315a5b9
8179327: gtestLauncher should run tests on a separate thread (optionally)
stuefe
parents:
41672
diff
changeset
|
322 |
} else { |
a8121315a5b9
8179327: gtestLauncher should run tests on a separate thread (optionally)
stuefe
parents:
41672
diff
changeset
|
323 |
runUnitTestsInner(argc, argv); |
a8121315a5b9
8179327: gtestLauncher should run tests on a separate thread (optionally)
stuefe
parents:
41672
diff
changeset
|
324 |
} |
a8121315a5b9
8179327: gtestLauncher should run tests on a separate thread (optionally)
stuefe
parents:
41672
diff
changeset
|
325 |
} |