Skip to content

Commit 1681627

Browse files
committed
Added initializer function for static android builds.
1 parent 16494f6 commit 1681627

File tree

4 files changed

+44
-27
lines changed

4 files changed

+44
-27
lines changed

Build/Common.Build.settings

+4-4
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@
114114
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
115115
<ClCompile>
116116
<Optimization>Disabled</Optimization>
117-
<PreprocessorDefinitions>WIN32;_DEBUG;%(PreprocessorDefinitions)</PreprocessorDefinitions>
117+
<PreprocessorDefinitions>_DEBUG;%(PreprocessorDefinitions)</PreprocessorDefinitions>
118118
</ClCompile>
119119
<Link>
120120
<GenerateDebugInformation>true</GenerateDebugInformation>
@@ -132,7 +132,7 @@
132132

133133
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|ARM'">
134134
<ClCompile>
135-
<PreprocessorDefinitions>WIN32;_DEBUG;%(PreprocessorDefinitions)</PreprocessorDefinitions>
135+
<PreprocessorDefinitions>_DEBUG;%(PreprocessorDefinitions)</PreprocessorDefinitions>
136136
</ClCompile>
137137
<Link>
138138
<GenerateDebugInformation>true</GenerateDebugInformation>
@@ -150,7 +150,7 @@
150150

151151
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|ARM'">
152152
<ClCompile>
153-
<PreprocessorDefinitions>WIN32;NDEBUG;%(PreprocessorDefinitions)</PreprocessorDefinitions>
153+
<PreprocessorDefinitions>NDEBUG;%(PreprocessorDefinitions)</PreprocessorDefinitions>
154154
</ClCompile>
155155
<Link>
156156
<GenerateDebugInformation>true</GenerateDebugInformation>
@@ -159,7 +159,7 @@
159159

160160
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
161161
<ClCompile>
162-
<PreprocessorDefinitions>WIN32;NDEBUG;%(PreprocessorDefinitions)</PreprocessorDefinitions>
162+
<PreprocessorDefinitions>NDEBUG;%(PreprocessorDefinitions)</PreprocessorDefinitions>
163163
</ClCompile>
164164
<Link>
165165
<GenerateDebugInformation>true</GenerateDebugInformation>

Release/include/pplx/pplxtasks.h

+5
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,11 @@ namespace Concurrency {
5555

5656
#include "pplx/pplx.h"
5757

58+
#if defined(__ANDROID__)
59+
#include <jni.h>
60+
void cpprest_init(JavaVM*);
61+
#endif
62+
5863
// Cannot build using a compiler that is older than dev10 SP1
5964
#if defined(_MSC_VER)
6065
#if _MSC_FULL_VER < 160040219 /*IFSTRIP=IGN*/

Release/include/pplx/threadpool.h

+6-7
Original file line numberDiff line numberDiff line change
@@ -74,12 +74,14 @@ class threadpool
7474
for (size_t i = 0; i < n; i++)
7575
add_thread();
7676
}
77-
77+
#if defined(__ANDROID__)
78+
static threadpool& shared_instance();
79+
#else
7880
static threadpool& shared_instance()
7981
{
8082
return s_shared;
8183
}
82-
84+
#endif
8385
~threadpool()
8486
{
8587
m_service.stop();
@@ -105,7 +107,9 @@ class threadpool
105107
private:
106108
struct _cancel_thread { };
107109

110+
#if !defined(__ANDROID__)
108111
static threadpool s_shared;
112+
#endif
109113

110114
void add_thread()
111115
{
@@ -130,11 +134,6 @@ class threadpool
130134
static void* thread_start(void *arg)
131135
{
132136
#if (defined(ANDROID) || defined(__ANDROID__))
133-
// Spinlock on the JVM calling JNI_OnLoad()
134-
while (JVM == nullptr)
135-
{
136-
pplx::details::platform::YieldExecution();
137-
}
138137
// Calling get_jvm_env() here forces the thread to be attached.
139138
get_jvm_env();
140139
pthread_cleanup_push(detach_from_java, nullptr);

Release/src/pplx/threadpool.cpp

+29-16
Original file line numberDiff line numberDiff line change
@@ -18,21 +18,29 @@
1818
**/
1919
#include "stdafx.h"
2020

21+
#if defined(__ANDROID__)
22+
#include <android/log.h>
23+
#include <jni.h>
24+
#endif
25+
2126
namespace crossplat
2227
{
23-
// initialize the static shared threadpool
24-
threadpool threadpool::s_shared(40);
25-
2628
#if (defined(ANDROID) || defined(__ANDROID__))
2729
// This pointer will be 0-initialized by default (at load time).
2830
std::atomic<JavaVM*> JVM;
2931

30-
JNIEnv* get_jvm_env()
32+
static void abort_if_no_jvm()
3133
{
3234
if (JVM == nullptr)
3335
{
34-
throw std::runtime_error("Could not contact JVM");
36+
__android_log_print(ANDROID_LOG_ERROR, "CPPRESTSDK", "%s", "The CppREST SDK must be initialized before first use on android: https://casablanca.codeplex.com/wikipage?title=Use%20on%20Android");
37+
std::abort();
3538
}
39+
}
40+
41+
JNIEnv* get_jvm_env()
42+
{
43+
abort_if_no_jvm();
3644
JNIEnv* env = nullptr;
3745
auto result = JVM.load()->AttachCurrentThread(&env, nullptr);
3846
if (result != JNI_OK)
@@ -43,20 +51,25 @@ JNIEnv* get_jvm_env()
4351
return env;
4452
}
4553

54+
threadpool& threadpool::shared_instance()
55+
{
56+
abort_if_no_jvm();
57+
static threadpool s_shared(40);
58+
return s_shared;
59+
}
60+
61+
#else
62+
63+
// initialize the static shared threadpool
64+
threadpool threadpool::s_shared(40);
65+
4666
#endif
4767

4868
}
4969

50-
#if (defined(ANDROID) || defined(__ANDROID__))
51-
extern "C" jint JNI_OnLoad(JavaVM* vm, void* reserved)
52-
{
53-
JNIEnv* env;
54-
if (vm->GetEnv(reinterpret_cast<void**>(&env), JNI_VERSION_1_6) != JNI_OK)
55-
{
56-
return -1;
57-
}
58-
59-
crossplat::JVM = vm;
60-
return JNI_VERSION_1_6;
70+
#if defined(__ANDROID__)
71+
void cpprest_init(JavaVM* vm) {
72+
crossplat::JVM = vm;
6173
}
6274
#endif
75+

0 commit comments

Comments
 (0)