From 9f6450d5a53cff26874d032cb599237601e7bea5 Mon Sep 17 00:00:00 2001 From: Oleh Prypin Date: Fri, 3 Aug 2018 10:05:41 +0200 Subject: [PATCH] JNI generation: Replace base::subtle::AtomicWord with std::atomic<> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This also rolls up //base in DEPS, because it needs to be landed together with https://chromium.googlesource.com/chromium/src/+/54f759310c8e219984b7bf5450f3c871035804b7 Bug: chromium:867475 Change-Id: I5792cb0610d2df46a9368fd3b1846583aa134b38 Reviewed-on: https://webrtc-review.googlesource.com/90404 Commit-Queue: Oleh Prypin Reviewed-by: Sami Kalliomäki Cr-Commit-Position: refs/heads/master@{#24180} --- DEPS | 2 +- sdk/android/src/jni/jni_generator_helper.cc | 50 ++++++++------------- sdk/android/src/jni/jni_generator_helper.h | 30 +++++++------ 3 files changed, 36 insertions(+), 46 deletions(-) diff --git a/DEPS b/DEPS index 88e4fd5cf2..4941404f08 100644 --- a/DEPS +++ b/DEPS @@ -42,7 +42,7 @@ deps = { # TODO(kjellander): Move this to be Android-only once the libevent dependency # in base/third_party/libevent is solved. 'src/base': - Var('chromium_git') + '/chromium/src/base' + '@' + 'e15177f81ad60f5bf6a00d1244a701e2844796d8', + Var('chromium_git') + '/chromium/src/base' + '@' + 'f9ce552913fd38f5b1f21d1e4eed67a815c94791', 'src/build': Var('chromium_git') + '/chromium/src/build' + '@' + 'fbf92119338eb0d6b4c364cd98a2017b6f7a30c9', 'src/buildtools': diff --git a/sdk/android/src/jni/jni_generator_helper.cc b/sdk/android/src/jni/jni_generator_helper.cc index 4cd9b43f5f..24a6212543 100644 --- a/sdk/android/src/jni/jni_generator_helper.cc +++ b/sdk/android/src/jni/jni_generator_helper.cc @@ -13,28 +13,22 @@ #include "rtc_base/atomicops.h" #include "sdk/android/native_api/jni/class_loader.h" -namespace base { -namespace android { +namespace webrtc { // If |atomic_class_id| set, it'll return immediately. Otherwise, it will look // up the class and store it. If there's a race, we take care to only store one // global reference (and the duplicated effort will happen only once). jclass LazyGetClass(JNIEnv* env, const char* class_name, - base::subtle::AtomicWord* atomic_class_id) { - static_assert(sizeof(base::subtle::AtomicWord) >= sizeof(jclass), - "AtomicWord can't be smaller than jclass"); - base::subtle::AtomicWord value = - rtc::AtomicOps::AcquireLoadPtr(atomic_class_id); + std::atomic* atomic_class_id) { + const jclass value = std::atomic_load(atomic_class_id); if (value) - return reinterpret_cast(value); + return value; webrtc::ScopedJavaGlobalRef clazz(webrtc::GetClass(env, class_name)); RTC_CHECK(!clazz.is_null()) << class_name; - base::subtle::AtomicWord null_aw = nullptr; - base::subtle::AtomicWord cas_result = rtc::AtomicOps::CompareAndSwapPtr( - atomic_class_id, null_aw, - reinterpret_cast(clazz.obj())); - if (cas_result == null_aw) { + jclass cas_result = nullptr; + if (std::atomic_compare_exchange_strong(atomic_class_id, &cas_result, + clazz.obj())) { // We sucessfully stored |clazz| in |atomic_class_id|, so we are // intentionally leaking the global ref since it's now stored there. return clazz.Release(); @@ -42,7 +36,7 @@ jclass LazyGetClass(JNIEnv* env, // Some other thread came before us and stored a global pointer in // |atomic_class_id|. Relase our global ref and return the ref from the // other thread. - return reinterpret_cast(cas_result); + return cas_result; } } @@ -54,23 +48,18 @@ jmethodID MethodID::LazyGet(JNIEnv* env, jclass clazz, const char* method_name, const char* jni_signature, - base::subtle::AtomicWord* atomic_method_id) { - static_assert(sizeof(base::subtle::AtomicWord) >= sizeof(jmethodID), - "AtomicWord can't be smaller than jMethodID"); - base::subtle::AtomicWord value = - rtc::AtomicOps::AcquireLoadPtr(atomic_method_id); + std::atomic* atomic_method_id) { + const jmethodID value = std::atomic_load(atomic_method_id); if (value) - return reinterpret_cast(value); - jmethodID id = (type == MethodID::TYPE_STATIC) - ? env->GetStaticMethodID(clazz, method_name, jni_signature) - : env->GetMethodID(clazz, method_name, jni_signature); + return value; + auto get_method_ptr = type == MethodID::TYPE_STATIC + ? &JNIEnv::GetStaticMethodID + : &JNIEnv::GetMethodID; + jmethodID id = (env->*get_method_ptr)(clazz, method_name, jni_signature); CHECK_EXCEPTION(env) << "error during GetMethodID: " << method_name << ", " << jni_signature; RTC_CHECK(id) << method_name << ", " << jni_signature; - - rtc::AtomicOps::CompareAndSwapPtr( - atomic_method_id, base::subtle::AtomicWord(nullptr), - reinterpret_cast(id)); + std::atomic_store(atomic_method_id, id); return id; } @@ -80,14 +69,13 @@ template jmethodID MethodID::LazyGet( jclass clazz, const char* method_name, const char* jni_signature, - base::subtle::AtomicWord* atomic_method_id); + std::atomic* atomic_method_id); template jmethodID MethodID::LazyGet( JNIEnv* env, jclass clazz, const char* method_name, const char* jni_signature, - base::subtle::AtomicWord* atomic_method_id); + std::atomic* atomic_method_id); -} // namespace android -} // namespace base +} // namespace webrtc diff --git a/sdk/android/src/jni/jni_generator_helper.h b/sdk/android/src/jni/jni_generator_helper.h index 95040ce2ff..53e1d84127 100644 --- a/sdk/android/src/jni/jni_generator_helper.h +++ b/sdk/android/src/jni/jni_generator_helper.h @@ -15,6 +15,7 @@ #define SDK_ANDROID_SRC_JNI_JNI_GENERATOR_HELPER_H_ #include +#include #include "rtc_base/checks.h" #include "sdk/android/native_api/jni/jni_int_wrapper.h" @@ -38,18 +39,7 @@ inline void CheckException(JNIEnv* env) { } } // namespace jni_generator -namespace base { - -namespace subtle { -// This needs to be a type that is big enough to store a jobject/jclass. -typedef void* AtomicWord; -} // namespace subtle - -namespace android { - -using webrtc::JavaRef; -using webrtc::ScopedJavaLocalRef; -using webrtc::JavaParamRef; +namespace webrtc { // This function will initialize |atomic_class_id| to contain a global ref to // the given class, and will return that ref on subsequent calls. The caller is @@ -58,7 +48,7 @@ using webrtc::JavaParamRef; // |atomic_method_id|. jclass LazyGetClass(JNIEnv* env, const char* class_name, - base::subtle::AtomicWord* atomic_class_id); + std::atomic* atomic_class_id); // This class is a wrapper for JNIEnv Get(Static)MethodID. class MethodID { @@ -78,9 +68,21 @@ class MethodID { jclass clazz, const char* method_name, const char* jni_signature, - base::subtle::AtomicWord* atomic_method_id); + std::atomic* atomic_method_id); }; +} // namespace webrtc + +// Re-export relevant classes into the namespaces the script expects. +namespace base { +namespace android { + +using webrtc::JavaParamRef; +using webrtc::JavaRef; +using webrtc::ScopedJavaLocalRef; +using webrtc::LazyGetClass; +using webrtc::MethodID; + } // namespace android } // namespace base