@@ -21,11 +21,9 @@ namespace
2121{
2222#if defined(__ANDROID__)
2323// This pointer will be 0-initialized by default (at load time).
24- std::atomic<JavaVM*> JVM;
25-
2624static void abort_if_no_jvm ()
2725{
28- if (JVM == nullptr )
26+ if (crossplat:: JVM == nullptr )
2927 {
3028 __android_log_print (ANDROID_LOG_ERROR,
3129 " CPPRESTSDK" ,
@@ -35,19 +33,6 @@ static void abort_if_no_jvm()
3533 std::abort ();
3634 }
3735}
38-
39- JNIEnv* get_jvm_env ()
40- {
41- abort_if_no_jvm ();
42- JNIEnv* env = nullptr ;
43- auto result = JVM.load ()->AttachCurrentThread (&env, nullptr );
44- if (result != JNI_OK)
45- {
46- throw std::runtime_error (" Could not attach to JVM" );
47- }
48-
49- return env;
50- }
5136#endif // __ANDROID__
5237
5338struct threadpool_impl final : crossplat::threadpool
@@ -80,14 +65,14 @@ struct threadpool_impl final : crossplat::threadpool
8065 }
8166
8267#if defined(__ANDROID__)
83- static void detach_from_java (void *) { JVM.load ()->DetachCurrentThread (); }
68+ static void detach_from_java (void *) { crossplat:: JVM.load ()->DetachCurrentThread (); }
8469#endif // __ANDROID__
8570
8671 static void * thread_start (void * arg) CPPREST_NOEXCEPT
8772 {
8873#if defined(__ANDROID__)
8974 // Calling get_jvm_env() here forces the thread to be attached.
90- get_jvm_env ();
75+ crossplat:: get_jvm_env ();
9176 pthread_cleanup_push (detach_from_java, nullptr );
9277#endif // __ANDROID__
9378 threadpool_impl* _this = reinterpret_cast <threadpool_impl*>(arg);
@@ -220,11 +205,28 @@ void threadpool::initialize_with_threads(size_t num_threads)
220205 throw std::runtime_error (" the cpprestsdk threadpool has already been initialized" );
221206 }
222207}
208+
209+ #if defined(__ANDROID__)
210+ std::atomic<JavaVM*> JVM;
211+
212+ JNIEnv* get_jvm_env ()
213+ {
214+ abort_if_no_jvm ();
215+ JNIEnv* env = nullptr ;
216+ auto result = crossplat::JVM.load ()->AttachCurrentThread (&env, nullptr );
217+ if (result != JNI_OK)
218+ {
219+ throw std::runtime_error (" Could not attach to JVM" );
220+ }
221+
222+ return env;
223+ }
224+ #endif // defined(__ANDROID__)
223225} // namespace crossplat
224226
225227#if defined(__ANDROID__)
226- void cpprest_init (JavaVM* vm) { JVM = vm; }
227- #endif
228+ void cpprest_init (JavaVM* vm) { crossplat:: JVM = vm; }
229+ #endif // defined(__ANDROID__)
228230
229231std::unique_ptr<crossplat::threadpool> crossplat::threadpool::construct (size_t num_threads)
230232{
0 commit comments