-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Shatter
JvmRefBase
and ThreadGuard
into separate files.
This change has no effect. PiperOrigin-RevId: 595901454
- Loading branch information
1 parent
71ef014
commit 0f0c77c
Showing
8 changed files
with
530 additions
and
348 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
/* | ||
* Copyright 2024 Google LLC | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
#ifndef JNI_BIND_IMPLEMENTATION_JVM_REF_BASE_H_ | ||
#define JNI_BIND_IMPLEMENTATION_JVM_REF_BASE_H_ | ||
|
||
#include <atomic> | ||
|
||
#include "implementation/forward_declarations.h" | ||
#include "jni_dep.h" | ||
|
||
namespace jni { | ||
|
||
// Helper for JvmRef to enforce correct sequencing of getting and setting | ||
// process level static fo JavaVM*. | ||
class JvmRefBase { | ||
protected: | ||
friend class ThreadGuard; | ||
friend class ThreadLocalGuardDestructor; | ||
|
||
JvmRefBase(JavaVM* vm) { process_level_jvm_.store(vm); } | ||
~JvmRefBase() { process_level_jvm_.store(nullptr); } | ||
|
||
static JavaVM* GetJavaVm() { return process_level_jvm_.load(); } | ||
static void SetJavaVm(JavaVM* jvm) { process_level_jvm_.store(jvm); } | ||
|
||
static inline std::atomic<JavaVM*> process_level_jvm_ = nullptr; | ||
}; | ||
|
||
} // namespace jni | ||
|
||
#endif // JNI_BIND_IMPLEMENTATION_JVM_REF_BASE_H_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
/* | ||
* Copyright 2024 Google LLC | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
#include "implementation/jvm_ref.h" | ||
|
||
#include <gmock/gmock.h> | ||
#include <gtest/gtest.h> | ||
#include "jni_bind.h" | ||
#include "jni_test.h" | ||
|
||
using ::jni::Class; | ||
using ::jni::JvmRef; | ||
using ::jni::LocalObject; | ||
using ::jni::test::AsGlobal; | ||
using ::jni::test::Fake; | ||
using ::jni::test::JniTest; | ||
using ::jni::test::JniTestWithNoDefaultJvmRef; | ||
using ::testing::AnyNumber; | ||
using ::testing::Return; | ||
|
||
namespace { | ||
|
||
TEST_F(JniTest, JvmRefTearsDownClassesLoadedfromDefaultLoader) { | ||
// Note, this expectation means FindClass is called *exactly* once. | ||
// Using offset to avoid Fake<jclass>() usage in jni_test.h. | ||
EXPECT_CALL(*env_, FindClass).WillOnce(Return(Fake<jclass>(1))); | ||
EXPECT_CALL(*env_, DeleteGlobalRef).Times(AnyNumber()); | ||
EXPECT_CALL(*env_, DeleteGlobalRef(AsGlobal(Fake<jclass>(1)))); | ||
|
||
static constexpr Class kClass1{"com/google/Class1"}; | ||
LocalObject<kClass1> local_object1{}; | ||
} | ||
|
||
TEST_F(JniTest, NoStaticCrossTalkWithPriorTest) { | ||
// Note, this expectation means FindClass is called *exactly* once. | ||
// Using 1 offset to avoid Fake<jclass>() usage in jni_test.h. | ||
EXPECT_CALL(*env_, FindClass).WillOnce(Return(Fake<jclass>(1))); | ||
EXPECT_CALL(*env_, DeleteGlobalRef).Times(AnyNumber()); | ||
EXPECT_CALL(*env_, DeleteGlobalRef(AsGlobal(Fake<jclass>(1)))); | ||
|
||
static constexpr Class kClass1{"com/google/Class1"}; | ||
LocalObject<kClass1> local_object1{}; | ||
} | ||
|
||
TEST_F(JniTest, NoStaticCrossTalkWithUnrelatedTest) { | ||
// Note, this expectation means FindClass is called *exactly* once. | ||
// Using 1 offset to avoid Fake<jclass>() usage in jni_test.h. | ||
EXPECT_CALL(*env_, FindClass).WillOnce(Return(Fake<jclass>(1))); | ||
EXPECT_CALL(*env_, DeleteGlobalRef).Times(AnyNumber()); | ||
EXPECT_CALL(*env_, DeleteGlobalRef(AsGlobal(Fake<jclass>(1)))); | ||
|
||
static constexpr Class kClass1{"com/google/Class2"}; | ||
LocalObject<kClass1> local_object1{}; | ||
} | ||
|
||
TEST_F(JniTestWithNoDefaultJvmRef, | ||
JvmsNeitherQueryNorReleaseIfNoObjectsCreated) { | ||
JvmRef<jni::kDefaultJvm> jvm_ref(jvm_.get()); | ||
EXPECT_CALL(*env_, FindClass).Times(0); | ||
EXPECT_CALL(*env_, DeleteGlobalRef).Times(0); | ||
} | ||
|
||
TEST_F(JniTestWithNoDefaultJvmRef, JvmRefsDontReuuseStaleFindClassValues) { | ||
EXPECT_CALL(*env_, FindClass) | ||
.WillOnce(Return(Fake<jclass>(1))) | ||
.WillOnce(Return(Fake<jclass>(2))); | ||
EXPECT_CALL(*env_, DeleteGlobalRef).Times(AnyNumber()); | ||
EXPECT_CALL(*env_, DeleteGlobalRef(AsGlobal(Fake<jclass>(1)))); | ||
EXPECT_CALL(*env_, DeleteGlobalRef(AsGlobal(Fake<jclass>(2)))); | ||
|
||
static constexpr Class kClass1{"com/google/ADifferentClassForVariety"}; | ||
{ | ||
JvmRef<jni::kDefaultJvm> jvm_ref(jvm_.get()); | ||
LocalObject<kClass1> local_object1{}; | ||
} | ||
|
||
{ | ||
JvmRef<jni::kDefaultJvm> jvm_ref(jvm_.get()); | ||
LocalObject<kClass1> local_object1{}; | ||
} | ||
} | ||
|
||
TEST_F(JniTest, DefaultLoaderReleasesMultipleClasses) { | ||
EXPECT_CALL(*env_, FindClass) | ||
.WillOnce(Return(Fake<jclass>(1))) | ||
.WillOnce(Return(Fake<jclass>(2))) | ||
.WillOnce(Return(Fake<jclass>(3))); | ||
EXPECT_CALL(*env_, DeleteGlobalRef).Times(AnyNumber()); | ||
EXPECT_CALL(*env_, DeleteGlobalRef(AsGlobal(Fake<jclass>(1)))); | ||
EXPECT_CALL(*env_, DeleteGlobalRef(AsGlobal(Fake<jclass>(2)))); | ||
EXPECT_CALL(*env_, DeleteGlobalRef(AsGlobal(Fake<jclass>(3)))); | ||
|
||
static constexpr Class kClass1{"com/google/Class1"}; | ||
static constexpr Class kClass2{"com/google/Class2"}; | ||
static constexpr Class kClass3{"com/google/Class3"}; | ||
|
||
LocalObject<kClass1> local_object1{}; | ||
LocalObject<kClass2> local_object2{}; | ||
LocalObject<kClass3> local_object3{}; | ||
} | ||
|
||
} // namespace |
Oops, something went wrong.