Skip to content

Commit 54a2c5a

Browse files
committed
8290059: Do not use std::thread in panama tests
Reviewed-by: mcimadamore, stuefe, erikj
1 parent 5214a17 commit 54a2c5a

File tree

7 files changed

+172
-48
lines changed

7 files changed

+172
-48
lines changed

make/test/JtregNativeJdk.gmk

+23-15
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@ BUILD_JDK_JTREG_EXECUTABLES_CFLAGS_exeJliLaunchTest := \
5353
-I$(TOPDIR)/src/java.base/$(OPENJDK_TARGET_OS_TYPE)/native/libjli \
5454
-I$(TOPDIR)/src/java.base/$(OPENJDK_TARGET_OS)/native/libjli
5555

56+
TEST_LIB_NATIVE_SRC := $(TOPDIR)/test/lib/native
57+
5658
# Platform specific setup
5759
ifeq ($(call isTargetOs, windows), true)
5860
BUILD_JDK_JTREG_EXCLUDE += libDirectIO.c libInheritedChannel.c \
@@ -67,26 +69,32 @@ ifeq ($(call isTargetOs, windows), true)
6769
BUILD_JDK_JTREG_EXECUTABLES_LIBS_exeNullCallerTest := $(LIBCXX) jvm.lib
6870
BUILD_JDK_JTREG_EXECUTABLES_LIBS_exerevokeall := advapi32.lib
6971
BUILD_JDK_JTREG_EXECUTABLES_CFLAGS_exeNullCallerTest := /EHsc
70-
BUILD_JDK_JTREG_LIBRARIES_CFLAGS_libAsyncStackWalk := /EHsc
71-
BUILD_JDK_JTREG_LIBRARIES_CFLAGS_libAsyncInvokers := /EHsc
72-
BUILD_JDK_JTREG_LIBRARIES_LDFLAGS_libLinkerInvokerUnnamed := /EHsc
73-
BUILD_JDK_JTREG_LIBRARIES_LDFLAGS_libLinkerInvokerModule := /EHsc
74-
BUILD_JDK_JTREG_LIBRARIES_LDFLAGS_libLoaderLookupInvoker := /EHsc
75-
BUILD_JDK_JTREG_LIBRARIES_LDFLAGS_libAsyncStackWalk := $(LIBCXX)
76-
BUILD_JDK_JTREG_LIBRARIES_LDFLAGS_libAsyncInvokers := $(LIBCXX)
77-
BUILD_JDK_JTREG_LIBRARIES_LDFLAGS_libLinkerInvokerUnnamed := $(LIBCXX)
78-
BUILD_JDK_JTREG_LIBRARIES_LDFLAGS_libLinkerInvokerModule := $(LIBCXX)
79-
BUILD_JDK_JTREG_LIBRARIES_LDFLAGS_libLoaderLookupInvoker := $(LIBCXX)
72+
73+
# java.lang.foreign tests
74+
BUILD_JDK_JTREG_LIBRARIES_CFLAGS_libAsyncStackWalk := -I$(TEST_LIB_NATIVE_SRC)
75+
BUILD_JDK_JTREG_LIBRARIES_CFLAGS_libLinkerInvokerUnnamed := -I$(TEST_LIB_NATIVE_SRC)
76+
BUILD_JDK_JTREG_LIBRARIES_CFLAGS_libLinkerInvokerModule := -I$(TEST_LIB_NATIVE_SRC)
77+
BUILD_JDK_JTREG_LIBRARIES_CFLAGS_libLoaderLookupInvoker := -I$(TEST_LIB_NATIVE_SRC)
78+
BUILD_JDK_JTREG_LIBRARIES_CFLAGS_libAsyncInvokers := -I$(TEST_LIB_NATIVE_SRC)
79+
8080
BUILD_JDK_JTREG_LIBRARIES_LIBS_libTracePinnedThreads := jvm.lib
8181
else
8282
BUILD_JDK_JTREG_LIBRARIES_LIBS_libstringPlatformChars := -ljava
8383
BUILD_JDK_JTREG_LIBRARIES_LIBS_libDirectIO := -ljava
8484
BUILD_JDK_JTREG_LIBRARIES_LDFLAGS_libNativeThread := -pthread
85-
BUILD_JDK_JTREG_LIBRARIES_LDFLAGS_libAsyncStackWalk := $(LIBCXX) -pthread
86-
BUILD_JDK_JTREG_LIBRARIES_LDFLAGS_libAsyncInvokers := $(LIBCXX) -pthread
87-
BUILD_JDK_JTREG_LIBRARIES_LDFLAGS_libLinkerInvokerUnnamed := $(LIBCXX) -pthread
88-
BUILD_JDK_JTREG_LIBRARIES_LDFLAGS_libLinkerInvokerModule := $(LIBCXX) -pthread
89-
BUILD_JDK_JTREG_LIBRARIES_LDFLAGS_libLoaderLookupInvoker := $(LIBCXX) -pthread
85+
86+
# java.lang.foreign tests
87+
BUILD_JDK_JTREG_LIBRARIES_CFLAGS_libAsyncStackWalk := -I$(TEST_LIB_NATIVE_SRC)
88+
BUILD_JDK_JTREG_LIBRARIES_LDFLAGS_libAsyncStackWalk := -pthread
89+
BUILD_JDK_JTREG_LIBRARIES_CFLAGS_libAsyncInvokers := -I$(TEST_LIB_NATIVE_SRC)
90+
BUILD_JDK_JTREG_LIBRARIES_LDFLAGS_libAsyncInvokers := -pthread
91+
BUILD_JDK_JTREG_LIBRARIES_CFLAGS_libLinkerInvokerUnnamed := -I$(TEST_LIB_NATIVE_SRC)
92+
BUILD_JDK_JTREG_LIBRARIES_LDFLAGS_libLinkerInvokerUnnamed := -pthread
93+
BUILD_JDK_JTREG_LIBRARIES_CFLAGS_libLinkerInvokerModule := -I$(TEST_LIB_NATIVE_SRC)
94+
BUILD_JDK_JTREG_LIBRARIES_LDFLAGS_libLinkerInvokerModule := -pthread
95+
BUILD_JDK_JTREG_LIBRARIES_CFLAGS_libLoaderLookupInvoker := -I$(TEST_LIB_NATIVE_SRC)
96+
BUILD_JDK_JTREG_LIBRARIES_LDFLAGS_libLoaderLookupInvoker := -pthread
97+
9098
BUILD_JDK_JTREG_LIBRARIES_LIBS_libExplicitAttach := -ljvm
9199
BUILD_JDK_JTREG_LIBRARIES_LDFLAGS_libExplicitAttach := -pthread
92100
BUILD_JDK_JTREG_LIBRARIES_LDFLAGS_libImplicitAttach := -pthread

test/jdk/java/foreign/enablenativeaccess/org/openjdk/foreigntest/libLinkerInvokerUnnamed.cpp

+4-4
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,10 @@
2323
*/
2424

2525
#include "jni.h"
26-
#include <thread>
26+
#include "testlib_threads.h"
2727

28-
void call(JavaVM *jvm) {
28+
void call(void* ctxt) {
29+
JavaVM* jvm = (JavaVM*) ctxt;
2930
JNIEnv* env;
3031
jvm->AttachCurrentThread((void**)&env, NULL);
3132
jclass linkerClass = env->FindClass("java/lang/foreign/Linker");
@@ -39,7 +40,6 @@ extern "C" {
3940
Java_org_openjdk_foreigntest_PanamaMainUnnamedModule_nativeLinker0(JNIEnv *env, jclass cls) {
4041
JavaVM* jvm;
4142
env->GetJavaVM(&jvm);
42-
std::thread thrd(call, jvm);
43-
thrd.join();
43+
run_in_new_thread_and_join(call, jvm);
4444
}
4545
}

test/jdk/java/foreign/enablenativeaccess/panama_module/org/openjdk/foreigntest/libLinkerInvokerModule.cpp

+4-4
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,10 @@
2323
*/
2424

2525
#include "jni.h"
26-
#include <thread>
26+
#include "testlib_threads.h"
2727

28-
void call(JavaVM *jvm) {
28+
void call(void* ctxt) {
29+
JavaVM *jvm = (JavaVM*) ctxt;
2930
JNIEnv* env;
3031
jvm->AttachCurrentThread((void**)&env, NULL);
3132
jclass linkerClass = env->FindClass("java/lang/foreign/Linker");
@@ -39,7 +40,6 @@ extern "C" {
3940
Java_org_openjdk_foreigntest_PanamaMainJNI_nativeLinker0(JNIEnv *env, jclass cls) {
4041
JavaVM* jvm;
4142
env->GetJavaVM(&jvm);
42-
std::thread thrd(call, jvm);
43-
thrd.join();
43+
run_in_new_thread_and_join(call, jvm);
4444
}
4545
}

test/jdk/java/foreign/libAsyncInvokers.cpp

+23-10
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2021, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2021, 2022, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -21,27 +21,40 @@
2121
* questions.
2222
*/
2323

24-
#include <thread>
24+
#include "testlib_threads.h"
2525

2626
#include "shared.h"
2727

28+
template<typename CB>
29+
void proc_v(void* v_cb) {
30+
((CB) v_cb)();
31+
}
32+
2833
template<typename CB>
2934
void launch_v(CB cb) {
30-
std::thread thrd(cb);
31-
thrd.join();
35+
run_in_new_thread_and_join(&proc_v<CB>, (void*) cb);
3236
}
3337

3438
template<typename O, typename CB>
35-
void start(O& out, CB cb) {
36-
out = cb();
39+
struct Context {
40+
CB cb;
41+
O o;
42+
};
43+
44+
template<typename O, typename CB>
45+
void proc(void* context) {
46+
Context<O, CB>* ctxt = (Context<O, CB>*) context;
47+
ctxt->o = ctxt->cb();
3748
}
3849

3950
template<typename O, typename CB>
4051
O launch(CB cb) {
41-
O result;
42-
std::thread thrd(&start<O, CB>, std::ref(result), cb);
43-
thrd.join();
44-
return result;
52+
Context<O, CB> ctxt;
53+
ctxt.cb = cb;
54+
55+
run_in_new_thread_and_join(&proc<O, CB>, &ctxt);
56+
57+
return ctxt.o;
4558
}
4659

4760
extern "C" {

test/jdk/java/foreign/loaderLookup/libLoaderLookupInvoker.cpp

+14-10
Original file line numberDiff line numberDiff line change
@@ -23,27 +23,31 @@
2323
*/
2424

2525
#include "jni.h"
26-
#include <thread>
26+
#include "testlib_threads.h"
2727

28-
jobject res;
28+
struct Context {
29+
JavaVM* jvm;
30+
jobject res;
31+
};
2932

30-
jobject call(JavaVM *jvm) {
33+
void call(void* ctxt) {
34+
Context* context = (Context*) ctxt;
3135
JNIEnv* env;
32-
jvm->AttachCurrentThread((void**)&env, NULL);
36+
context->jvm->AttachCurrentThread((void**)&env, NULL);
3337
jclass symbolLookupClass = env->FindClass("java/lang/foreign/SymbolLookup");
3438
jmethodID loaderLookupMethod = env->GetStaticMethodID(symbolLookupClass, "loaderLookup", "()Ljava/lang/foreign/SymbolLookup;");
35-
res = env->NewGlobalRef(env->CallStaticObjectMethod(symbolLookupClass, loaderLookupMethod));
36-
jvm->DetachCurrentThread();
37-
return res;
39+
context->res = env->NewGlobalRef(env->CallStaticObjectMethod(symbolLookupClass, loaderLookupMethod));
40+
context->jvm->DetachCurrentThread();
3841
}
3942

4043
extern "C" {
4144
JNIEXPORT jobject JNICALL
4245
Java_TestLoaderLookupJNI_loaderLookup0(JNIEnv *env, jclass cls) {
4346
JavaVM* jvm;
4447
env->GetJavaVM(&jvm);
45-
std::thread thrd(call, jvm);
46-
thrd.join();
47-
return res;
48+
Context context;
49+
context.jvm = jvm;
50+
run_in_new_thread_and_join(call, &context);
51+
return context.res;
4852
}
4953
}

test/jdk/java/foreign/stackwalk/libAsyncStackWalk.cpp

+7-5
Original file line numberDiff line numberDiff line change
@@ -22,23 +22,25 @@
2222
*
2323
*/
2424

25-
#include <thread>
25+
#include "testlib_threads.h"
2626

2727
#ifdef _WIN64
2828
#define EXPORT __declspec(dllexport)
2929
#else
3030
#define EXPORT
3131
#endif
3232

33-
static void start(void (*cb)(void)) {
33+
typedef void (*CB_t)(void);
34+
35+
static void start(void* ctxt) {
36+
CB_t cb = (CB_t) ctxt;
3437
for (int i = 0; i < 25000; i++) {
3538
cb();
3639
}
3740
}
3841

3942
extern "C" {
40-
EXPORT void asyncStackWalk(void (*cb)(void)) {
41-
std::thread thrd(start, cb);
42-
thrd.join();
43+
EXPORT void asyncStackWalk(CB_t cb) {
44+
run_in_new_thread_and_join(start, (void*) cb);
4345
}
4446
}

test/lib/native/testlib_threads.h

+97
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
/*
2+
* Copyright (c) 2022, 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 TEST_LIB_NATIVE_THREAD_H
25+
#define TEST_LIB_NATIVE_THREAD_H
26+
27+
// Header only library for using threads in tests
28+
29+
#include <stdlib.h>
30+
#include <stdio.h>
31+
32+
#ifdef _WIN32
33+
#include <windows.h>
34+
#else
35+
#include <unistd.h>
36+
#include <pthread.h>
37+
#endif
38+
39+
extern "C" {
40+
41+
typedef void(*PROCEDURE)(void*);
42+
43+
struct Helper {
44+
PROCEDURE proc;
45+
void* context;
46+
};
47+
48+
static void fatal(const char* message, int code) {
49+
fputs(message, stderr);
50+
// exit the test with a non-zero exit code to avoid accidental false positives
51+
exit(code);
52+
}
53+
54+
// Adapt from the callback type the OS API expects to
55+
// our OS-independent PROCEDURE type.
56+
#ifdef _WIN32
57+
static DWORD procedure(_In_ LPVOID ctxt) {
58+
#else
59+
void* procedure(void* ctxt) {
60+
#endif
61+
Helper* helper = (Helper*)ctxt;
62+
helper->proc(helper->context);
63+
return 0;
64+
}
65+
66+
// Run 'proc' in a newly started thread, passing 'context' to it
67+
// as an argument, and then join that thread.
68+
void run_in_new_thread_and_join(PROCEDURE proc, void* context) {
69+
struct Helper helper;
70+
helper.proc = proc;
71+
helper.context = context;
72+
#ifdef _WIN32
73+
HANDLE thread = CreateThread(NULL, 0, procedure, &helper, 0, NULL);
74+
if (thread == NULL) {
75+
fatal("failed to create thread", GetLastError());
76+
}
77+
if (WaitForSingleObject(thread, INFINITE) != WAIT_OBJECT_0) {
78+
// Should be WAIT_FAILED, since this is not a mutex, and
79+
// we set no timeout.
80+
fatal("failed to join thread", GetLastError());
81+
}
82+
#else
83+
pthread_t thread;
84+
int result = pthread_create(&thread, NULL, procedure, &helper);
85+
if (result != 0) {
86+
fatal("failed to create thread", result);
87+
}
88+
result = pthread_join(thread, NULL);
89+
if (result != 0) {
90+
fatal("failed to join thread", result);
91+
}
92+
#endif
93+
}
94+
95+
}
96+
97+
#endif // TEST_LIB_NATIVE_THREAD_H

0 commit comments

Comments
 (0)