|
1 | 1 | /*
|
2 |
| - * Copyright (c) 1997, 2021, Oracle and/or its affiliates. All rights reserved. |
| 2 | + * Copyright (c) 1997, 2022, Oracle and/or its affiliates. All rights reserved. |
3 | 3 | * Copyright (c) 2021, Azul Systems, Inc. All rights reserved.
|
4 | 4 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
5 | 5 | *
|
@@ -1312,16 +1312,21 @@ class JavaThread: public Thread {
|
1312 | 1312 | // Returns the jni environment for this thread
|
1313 | 1313 | JNIEnv* jni_environment() { return &_jni_environment; }
|
1314 | 1314 |
|
| 1315 | + // Returns the current thread as indicated by the given JNIEnv. |
| 1316 | + // We don't assert it is Thread::current here as that is done at the |
| 1317 | + // external JNI entry points where the JNIEnv is passed into the VM. |
1315 | 1318 | static JavaThread* thread_from_jni_environment(JNIEnv* env) {
|
1316 |
| - JavaThread *thread_from_jni_env = (JavaThread*)((intptr_t)env - in_bytes(jni_environment_offset())); |
1317 |
| - // Only return NULL if thread is off the thread list; starting to |
1318 |
| - // exit should not return NULL. |
1319 |
| - if (thread_from_jni_env->is_terminated()) { |
1320 |
| - thread_from_jni_env->block_if_vm_exited(); |
1321 |
| - return NULL; |
1322 |
| - } else { |
1323 |
| - return thread_from_jni_env; |
| 1319 | + JavaThread* current = (JavaThread*)((intptr_t)env - in_bytes(jni_environment_offset())); |
| 1320 | + // We can't get here in a thread that has completed its execution and so |
| 1321 | + // "is_terminated", but a thread is also considered terminated if the VM |
| 1322 | + // has exited, so we have to check this and block in case this is a daemon |
| 1323 | + // thread returning to the VM (the JNI DirectBuffer entry points rely on |
| 1324 | + // this). |
| 1325 | + if (current->is_terminated()) { |
| 1326 | + current->block_if_vm_exited(); |
| 1327 | + ShouldNotReachHere(); |
1324 | 1328 | }
|
| 1329 | + return current; |
1325 | 1330 | }
|
1326 | 1331 |
|
1327 | 1332 | // JNI critical regions. These can nest.
|
|
0 commit comments