From 4c45969e1bbed2a794d9fa049fcc672ce9ecc289 Mon Sep 17 00:00:00 2001 From: "Billy O'Neal (VC LIBS)" Date: Tue, 19 Mar 2019 18:07:22 -0700 Subject: [PATCH 1/2] Move get_jvm_env back into the crossplat namespace as it is declared in a header and used elsewhere. --- Release/src/pplx/threadpool.cpp | 36 +++++++++++++++++---------------- 1 file changed, 19 insertions(+), 17 deletions(-) diff --git a/Release/src/pplx/threadpool.cpp b/Release/src/pplx/threadpool.cpp index 61a6b1d6e4..aa5f97a804 100644 --- a/Release/src/pplx/threadpool.cpp +++ b/Release/src/pplx/threadpool.cpp @@ -21,8 +21,6 @@ namespace { #if defined(__ANDROID__) // This pointer will be 0-initialized by default (at load time). -std::atomic JVM; - static void abort_if_no_jvm() { if (JVM == nullptr) @@ -35,19 +33,6 @@ static void abort_if_no_jvm() std::abort(); } } - -JNIEnv* get_jvm_env() -{ - abort_if_no_jvm(); - JNIEnv* env = nullptr; - auto result = JVM.load()->AttachCurrentThread(&env, nullptr); - if (result != JNI_OK) - { - throw std::runtime_error("Could not attach to JVM"); - } - - return env; -} #endif // __ANDROID__ struct threadpool_impl final : crossplat::threadpool @@ -87,7 +72,7 @@ struct threadpool_impl final : crossplat::threadpool { #if defined(__ANDROID__) // Calling get_jvm_env() here forces the thread to be attached. - get_jvm_env(); + crossplat::get_jvm_env(); pthread_cleanup_push(detach_from_java, nullptr); #endif // __ANDROID__ threadpool_impl* _this = reinterpret_cast(arg); @@ -220,11 +205,28 @@ void threadpool::initialize_with_threads(size_t num_threads) throw std::runtime_error("the cpprestsdk threadpool has already been initialized"); } } + +#if defined(__ANDROID__) +std::atomic JVM; + +JNIEnv* get_jvm_env() +{ + abort_if_no_jvm(); + JNIEnv* env = nullptr; + auto result = JVM.load()->AttachCurrentThread(&env, nullptr); + if (result != JNI_OK) + { + throw std::runtime_error("Could not attach to JVM"); + } + + return env; +} +#endif // defined(__ANDROID__) } // namespace crossplat #if defined(__ANDROID__) void cpprest_init(JavaVM* vm) { JVM = vm; } -#endif +#endif // defined(__ANDROID__) std::unique_ptr crossplat::threadpool::construct(size_t num_threads) { From f7236c7629faea547a5f93235994ac8913b266b1 Mon Sep 17 00:00:00 2001 From: "Billy O'Neal (VC LIBS)" Date: Tue, 19 Mar 2019 19:32:03 -0700 Subject: [PATCH 2/2] Add more missing crossplat:: qualifications. --- Release/src/pplx/threadpool.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/Release/src/pplx/threadpool.cpp b/Release/src/pplx/threadpool.cpp index aa5f97a804..ba38a1a12f 100644 --- a/Release/src/pplx/threadpool.cpp +++ b/Release/src/pplx/threadpool.cpp @@ -23,7 +23,7 @@ namespace // This pointer will be 0-initialized by default (at load time). static void abort_if_no_jvm() { - if (JVM == nullptr) + if (crossplat::JVM == nullptr) { __android_log_print(ANDROID_LOG_ERROR, "CPPRESTSDK", @@ -65,7 +65,7 @@ struct threadpool_impl final : crossplat::threadpool } #if defined(__ANDROID__) - static void detach_from_java(void*) { JVM.load()->DetachCurrentThread(); } + static void detach_from_java(void*) { crossplat::JVM.load()->DetachCurrentThread(); } #endif // __ANDROID__ static void* thread_start(void* arg) CPPREST_NOEXCEPT @@ -213,7 +213,7 @@ JNIEnv* get_jvm_env() { abort_if_no_jvm(); JNIEnv* env = nullptr; - auto result = JVM.load()->AttachCurrentThread(&env, nullptr); + auto result = crossplat::JVM.load()->AttachCurrentThread(&env, nullptr); if (result != JNI_OK) { throw std::runtime_error("Could not attach to JVM"); @@ -225,7 +225,7 @@ JNIEnv* get_jvm_env() } // namespace crossplat #if defined(__ANDROID__) -void cpprest_init(JavaVM* vm) { JVM = vm; } +void cpprest_init(JavaVM* vm) { crossplat::JVM = vm; } #endif // defined(__ANDROID__) std::unique_ptr crossplat::threadpool::construct(size_t num_threads)