@@ -21,11 +21,9 @@ namespace
21
21
{
22
22
#if defined(__ANDROID__)
23
23
// This pointer will be 0-initialized by default (at load time).
24
- std::atomic<JavaVM*> JVM;
25
-
26
24
static void abort_if_no_jvm ()
27
25
{
28
- if (JVM == nullptr )
26
+ if (crossplat:: JVM == nullptr )
29
27
{
30
28
__android_log_print (ANDROID_LOG_ERROR,
31
29
" CPPRESTSDK" ,
@@ -35,19 +33,6 @@ static void abort_if_no_jvm()
35
33
std::abort ();
36
34
}
37
35
}
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
- }
51
36
#endif // __ANDROID__
52
37
53
38
struct threadpool_impl final : crossplat::threadpool
@@ -80,14 +65,14 @@ struct threadpool_impl final : crossplat::threadpool
80
65
}
81
66
82
67
#if defined(__ANDROID__)
83
- static void detach_from_java (void *) { JVM.load ()->DetachCurrentThread (); }
68
+ static void detach_from_java (void *) { crossplat:: JVM.load ()->DetachCurrentThread (); }
84
69
#endif // __ANDROID__
85
70
86
71
static void * thread_start (void * arg) CPPREST_NOEXCEPT
87
72
{
88
73
#if defined(__ANDROID__)
89
74
// Calling get_jvm_env() here forces the thread to be attached.
90
- get_jvm_env ();
75
+ crossplat:: get_jvm_env ();
91
76
pthread_cleanup_push (detach_from_java, nullptr );
92
77
#endif // __ANDROID__
93
78
threadpool_impl* _this = reinterpret_cast <threadpool_impl*>(arg);
@@ -220,11 +205,28 @@ void threadpool::initialize_with_threads(size_t num_threads)
220
205
throw std::runtime_error (" the cpprestsdk threadpool has already been initialized" );
221
206
}
222
207
}
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__)
223
225
} // namespace crossplat
224
226
225
227
#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__)
228
230
229
231
std::unique_ptr<crossplat::threadpool> crossplat::threadpool::construct (size_t num_threads)
230
232
{
0 commit comments