summaryrefslogtreecommitdiffstats
path: root/package/libcpprestsdk/0002-remove-use-of-aligned-union.patch
blob: c3f5bf56c6c56bf5e0545a7306e3f9da6eb3388b (plain)
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
From 65267c6e83e7e29ed3bdddde13d2c4bbb10e1bff Mon Sep 17 00:00:00 2001
From: Billy O'Neal <billy.oneal@gmail.com>
Date: Thu, 6 Dec 2018 23:57:00 -0800
Subject: [PATCH] Remove use of aligned_union that broke CentOS 7. (#987)

Signed-off-by: Billy O'Neal <billy.oneal@gmail.com>
[Backport from upstream commit 65267c6e83e7e29ed3bdddde13d2c4bbb10e1bff]
Signed-off-by: Adam Duskett <aduskett@gmail.com>
---
 Release/src/pplx/threadpool.cpp | 127 +++++++++++++++-----------------
 1 file changed, 61 insertions(+), 66 deletions(-)

diff --git a/Release/src/pplx/threadpool.cpp b/Release/src/pplx/threadpool.cpp
index 1c28cca8f..58920c025 100644
--- a/Release/src/pplx/threadpool.cpp
+++ b/Release/src/pplx/threadpool.cpp
@@ -1,9 +1,7 @@
 /***
-* Copyright (C) Microsoft. All rights reserved.
-* Licensed under the MIT license. See LICENSE.txt file in the project root for full license information.
-*
-* =+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+
-**/
+ * 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)
@@ -11,8 +9,9 @@
 
 #include <boost/asio/detail/thread.hpp>
 #include <new>
-#include <vector>
 #include <type_traits>
+#include <utility>
+#include <vector>
 
 #if defined(__ANDROID__)
 #include <android/log.h>
@@ -29,9 +28,11 @@ 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");
+        __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();
     }
 }
@@ -52,9 +53,7 @@ JNIEnv* get_jvm_env()
 
 struct threadpool_impl final : crossplat::threadpool
 {
-    threadpool_impl(size_t n)
-        : crossplat::threadpool(n)
-        , m_work(m_service)
+    threadpool_impl(size_t n) : crossplat::threadpool(n), m_work(m_service)
     {
         for (size_t i = 0; i < n; i++)
             add_thread();
@@ -69,26 +68,20 @@ struct threadpool_impl final : crossplat::threadpool
         }
     }
 
-    threadpool_impl& get_shared()
-    {
-        return *this;
-    }
+    threadpool_impl& get_shared() { return *this; }
 
 private:
     void add_thread()
     {
-        m_threads.push_back(std::unique_ptr<boost::asio::detail::thread>(
-            new boost::asio::detail::thread([&]{ thread_start(this); })));
+        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*)
-    {
-        JVM.load()->DetachCurrentThread();
-    }
+    static void detach_from_java(void*) { JVM.load()->DetachCurrentThread(); }
 #endif // __ANDROID__
 
-    static void* thread_start(void *arg) CPPREST_NOEXCEPT
+    static void* thread_start(void* arg) CPPREST_NOEXCEPT
     {
 #if defined(__ANDROID__)
         // Calling get_jvm_env() here forces the thread to be attached.
@@ -110,17 +103,14 @@ struct threadpool_impl final : crossplat::threadpool
 #if defined(_WIN32)
 struct shared_threadpool
 {
-    std::aligned_union<0, threadpool_impl>::type shared_storage;
+    union {
+        threadpool_impl shared_storage;
+    };
 
-    threadpool_impl& get_shared()
-    {
-        return reinterpret_cast<threadpool_impl&>(shared_storage);
-    }
+    threadpool_impl& get_shared() { return shared_storage; }
+
+    shared_threadpool(size_t n) : shared_storage(n) {}
 
-    shared_threadpool(size_t n)
-    {
-        ::new (static_cast<void*>(&get_shared())) threadpool_impl(n);
-    }
     ~shared_threadpool()
     {
         // if linked into a DLL, the threadpool shared instance will be
@@ -138,52 +128,59 @@ typedef shared_threadpool platform_shared_threadpool;
 typedef threadpool_impl platform_shared_threadpool;
 #endif
 
-std::pair<bool, platform_shared_threadpool*> initialize_shared_threadpool(size_t num_threads)
+namespace
 {
-    static std::aligned_union<0, platform_shared_threadpool>::type storage;
-    platform_shared_threadpool* const ptr =
-        &reinterpret_cast<platform_shared_threadpool&>(storage);
-    bool initialized_this_time = false;
-#if defined(__ANDROID__)
-    // mutex based implementation due to paranoia about (lack of) call_once support on Android
-    // remove this if/when call_once is supported
-    static std::mutex mtx;
-    static std::atomic<bool> initialized;
-    abort_if_no_jvm();
-    if (!initialized.load())
+template<class T>
+struct uninitialized
+{
+    union {
+        T storage;
+    };
+
+    bool initialized;
+
+    uninitialized() CPPREST_NOEXCEPT : initialized(false) {}
+    uninitialized(const uninitialized&) = delete;
+    uninitialized& operator=(const uninitialized&) = delete;
+    ~uninitialized()
     {
-        std::lock_guard<std::mutex> guard(mtx);
-        if (!initialized.load())
+        if (initialized)
         {
-            ::new (static_cast<void*>(ptr)) platform_shared_threadpool(num_threads);
-            initialized.store(true);
-            initialized_this_time = true;
+            storage.~T();
         }
-    }   // also unlock
+    }
+
+    template<class... Args>
+    void construct(Args&&... vals)
+    {
+        ::new (static_cast<void*>(&storage)) T(std::forward<Args>(vals)...);
+        initialized = true;
+    }
+};
+} // unnamed namespace
 
-#else // ^^^ __ANDROID__ ^^^ // vvv !__ANDROID___ vvv //
+std::pair<bool, platform_shared_threadpool*> initialize_shared_threadpool(size_t num_threads)
+{
+    static uninitialized<platform_shared_threadpool> uninit_threadpool;
+    bool initialized_this_time = false;
     static std::once_flag of;
 
-// #if defined(__ANDROID__) // if call_once can be used for android
-//     abort_if_no_jvm();
-// #endif // __ANDROID__
-    std::call_once(of, [num_threads, ptr, &initialized_this_time] {
-        ::new (static_cast<void*>(ptr)) platform_shared_threadpool(num_threads);
+    #if defined(__ANDROID__)
+        abort_if_no_jvm();
+    #endif // __ANDROID__
+
+    std::call_once(of, [num_threads, &initialized_this_time] {
+        uninit_threadpool.construct(num_threads);
         initialized_this_time = true;
     });
-#endif // __ANDROID__
 
-    return {initialized_this_time, ptr};
+    return {initialized_this_time, &uninit_threadpool.storage};
 }
 }
 
 namespace crossplat
 {
-threadpool& threadpool::shared_instance()
-{
-    return initialize_shared_threadpool(40).second->get_shared();
-}
-
+threadpool& threadpool::shared_instance() { return initialize_shared_threadpool(40).second->get_shared(); }
 
 void threadpool::initialize_with_threads(size_t num_threads)
 {
@@ -196,9 +193,7 @@ void threadpool::initialize_with_threads(size_t num_threads)
 }
 
 #if defined(__ANDROID__)
-void cpprest_init(JavaVM* vm) {
-    JVM = vm;
-}
+void cpprest_init(JavaVM* vm) { JVM = vm; }
 #endif
 
 std::unique_ptr<crossplat::threadpool> crossplat::threadpool::construct(size_t num_threads)

OpenPOWER on IntegriCloud