This repository was archived by the owner on Jun 14, 2024. It is now read-only.
forked from microsoft/cpprestsdk
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy paththreadpool.cpp
More file actions
162 lines (136 loc) · 3.88 KB
/
Copy paththreadpool.cpp
File metadata and controls
162 lines (136 loc) · 3.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
/***
* Copyright (C) Microsoft. All rights reserved.
* Licensed under the MIT license. See LICENSE.txt file in the project root for full license information.
*
* =+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+
**/
#include "stdafx.h"
#if !defined(CPPREST_EXCLUDE_WEBSOCKETS) || !defined(_WIN32)
#include "pplx/threadpool.h"
#include <boost/asio/detail/thread.hpp>
#include <vector>
#if defined(__ANDROID__)
#include <android/log.h>
#include <jni.h>
#endif
namespace
{
struct threadpool_impl final : crossplat::threadpool
{
threadpool_impl(size_t n)
: crossplat::threadpool(n)
, m_work(m_service)
{
for (size_t i = 0; i < n; i++)
add_thread();
}
~threadpool_impl()
{
m_service.stop();
for (auto iter = m_threads.begin(); iter != m_threads.end(); ++iter)
{
(*iter)->join();
}
}
private:
void add_thread()
{
m_threads.push_back(std::unique_ptr<boost::asio::detail::thread>(new boost::asio::detail::thread([&]{ thread_start(this); })));
}
#if defined(__ANDROID__)
static void detach_from_java(void*)
{
crossplat::JVM.load()->DetachCurrentThread();
}
#endif
static void* thread_start(void *arg) CPPREST_NOEXCEPT
{
#if defined(__ANDROID__)
// Calling get_jvm_env() here forces the thread to be attached.
crossplat::get_jvm_env();
pthread_cleanup_push(detach_from_java, nullptr);
#endif
threadpool_impl* _this = reinterpret_cast<threadpool_impl*>(arg);
_this->m_service.run();
#if defined(__ANDROID__)
pthread_cleanup_pop(true);
#endif
return arg;
}
std::vector<std::unique_ptr<boost::asio::detail::thread>> m_threads;
boost::asio::io_service::work m_work;
};
}
namespace crossplat
{
#if defined(__ANDROID__)
// This pointer will be 0-initialized by default (at load time).
std::atomic<JavaVM*> JVM;
static void abort_if_no_jvm()
{
if (JVM == nullptr)
{
__android_log_print(ANDROID_LOG_ERROR, "CPPRESTSDK", "%s", "The CppREST SDK must be initialized before first use on android: https://github.com/Microsoft/cpprestsdk/wiki/How-to-build-for-Android");
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;
}
threadpool& threadpool::shared_instance()
{
abort_if_no_jvm();
static threadpool_impl s_shared(40);
return s_shared;
}
#elif defined(_WIN32)
// if linked into a DLL, the threadpool shared instance will be destroyed at DLL_PROCESS_DETACH,
// at which stage joining threads causes deadlock, hence this dance
threadpool& threadpool::shared_instance()
{
static bool terminate_threads = false;
static struct restore_terminate_threads
{
~restore_terminate_threads()
{
boost::asio::detail::thread::set_terminate_threads(terminate_threads);
}
} destroyed_after;
static threadpool_impl s_shared(40);
static struct enforce_terminate_threads
{
~enforce_terminate_threads()
{
terminate_threads = boost::asio::detail::thread::terminate_threads();
boost::asio::detail::thread::set_terminate_threads(true);
}
} destroyed_before;
return s_shared;
}
#else
// initialize the static shared threadpool
threadpool& threadpool::shared_instance()
{
static threadpool_impl s_shared(40);
return s_shared;
}
#endif
}
#if defined(__ANDROID__)
void cpprest_init(JavaVM* vm) {
crossplat::JVM = vm;
}
#endif
std::unique_ptr<crossplat::threadpool> crossplat::threadpool::construct(size_t num_threads)
{
return std::unique_ptr<crossplat::threadpool>(new threadpool_impl(num_threads));
}
#endif