Skip to content

Commit f954e0f

Browse files
committed
FIX | build on win32
1 parent 719dd19 commit f954e0f

2 files changed

Lines changed: 69 additions & 55 deletions

File tree

src/ManapiAsync.cpp

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,35 +10,39 @@ struct current_data_t {
1010
std::coroutine_handle<> root;
1111
};
1212

13+
thread_local ::current_data_t ctxasync;
14+
1315
#ifdef _WIN32
1416
__declspec(dllexport)
1517
#else
1618
__attribute__((visibility("default")))
1719
#endif
18-
thread_local current_data_t ctxcurrent;
20+
::current_data_t &current_ctxasync () {
21+
return ::ctxasync;
22+
}
1923

2024
void manapi::init_tools::max_coro_stack(std::size_t sz) MANAPIHTTP_NOEXCEPT {
21-
::ctxcurrent.st_max = sz;
25+
current_ctxasync().st_max = sz;
2226
}
2327

2428
void manapi::async::internal::current_(std::shared_ptr<cthread> ctx) MANAPIHTTP_NOEXCEPT {
25-
ctxcurrent.cthread = std::move(ctx);
29+
current_ctxasync().cthread = std::move(ctx);
2630
}
2731

2832
const std::shared_ptr<manapi::async::cthread> & manapi::async::internal::current_() MANAPIHTTP_NOEXCEPT {
29-
return ctxcurrent.cthread;
33+
return current_ctxasync().cthread;
3034
}
3135

3236
std::size_t manapi::async::internal::current_stack_cnt_crt () MANAPIHTTP_NOEXCEPT {
33-
return ctxcurrent.st_cnt;
37+
return current_ctxasync().st_cnt;
3438
}
3539

3640
void manapi::async::internal::current_stack_cnt_set (std::size_t cnt) MANAPIHTTP_NOEXCEPT {
37-
ctxcurrent.st_cnt = cnt;
41+
current_ctxasync().st_cnt = cnt;
3842
}
3943

4044
std::size_t manapi::async::internal::max_stack_depth_crt () MANAPIHTTP_NOEXCEPT {
41-
return ::ctxcurrent.st_max;
45+
return current_ctxasync().st_max;
4246
}
4347

4448
// void manapi::async::internal::cnt_finish_inc() MANAPIHTTP_NOEXCEPT {
@@ -135,21 +139,22 @@ manapi::future<> manapi::async::internal::promise<void, manapi::future<>>::promi
135139

136140
void manapi::async::coro_resume(std::coroutine_handle<> handle) {
137141
assert(handle);
142+
auto &z = current_ctxasync();
138143

139-
if (::ctxcurrent.root) {
144+
if (z.root) {
140145
handle.resume();
141146
}
142147
else {
143148
try {
144149
handle.resume();
145150

146-
if (::ctxcurrent.root) {
147-
std::exchange(::ctxcurrent.root, nullptr).destroy();
151+
if (z.root) {
152+
std::exchange(z.root, nullptr).destroy();
148153
}
149154
}
150155
catch (std::exception const &) {
151-
if (::ctxcurrent.root) {
152-
std::exchange(::ctxcurrent.root, nullptr).destroy();
156+
if (z.root) {
157+
std::exchange(z.root, nullptr).destroy();
153158
}
154159

155160
std::rethrow_exception(std::current_exception());
@@ -171,6 +176,6 @@ void manapi::async::coro_resume(std::coroutine_handle<> handle) {
171176
}
172177

173178
void manapi::async::coro_finish(std::coroutine_handle<> handle) MANAPIHTTP_NOEXCEPT {
174-
assert(!::ctxcurrent.root);
175-
::ctxcurrent.root = handle;
179+
assert(!current_ctxasync().root);
180+
current_ctxasync().root = handle;
176181
}

src/ManapiGrpc.cpp

Lines changed: 50 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -66,30 +66,34 @@ enum manapi_grpc_server_flags {
6666
MANAPI_GRPC_SERVER_IS_STOPPING = 1<<1
6767
};
6868

69+
thread_local wgrpc_thread_local_storage_t wgrpc_storage;
70+
6971
#ifdef _WIN32
7072
__declspec(dllexport)
7173
#else
7274
__attribute__((visibility("default")))
7375
#endif
74-
thread_local wgrpc_thread_local_storage_t wgrpc_storage;
76+
wgrpc_thread_local_storage_t &current_wgrpc_storage () {
77+
return ::wgrpc_storage;
78+
}
7579

7680
struct wgrpc_current_ctx_deleter_t {
7781
wgrpc_current_ctx_deleter_t (std::shared_ptr<manapi::async::cthread> ctx) {
78-
assert(!wgrpc_storage.ctx);
79-
wgrpc_storage.ctx = std::move(ctx);
82+
assert(!current_wgrpc_storage().ctx);
83+
current_wgrpc_storage().ctx = std::move(ctx);
8084
}
8185

8286
~wgrpc_current_ctx_deleter_t () {
83-
wgrpc_storage.ctx = nullptr;
87+
current_wgrpc_storage().ctx = nullptr;
8488
}
8589
};
8690

8791
static std::unordered_map<std::size_t, std::uintptr_t>::iterator wgrpc_tasks_find (std::intptr_t keys[2]) {
88-
return wgrpc_storage.wgrpc_tasks_exists.find(static_cast<std::size_t>(keys[1]));
92+
return current_wgrpc_storage().wgrpc_tasks_exists.find(static_cast<std::size_t>(keys[1]));
8993
}
9094

9195
static std::unordered_map<std::size_t, std::uintptr_t>::iterator wgrpc_connect_find (std::intptr_t keys[2]) {
92-
return wgrpc_storage.wgrpc_connect_exists.find(static_cast<std::size_t>(keys[1]));
96+
return current_wgrpc_storage().wgrpc_connect_exists.find(static_cast<std::size_t>(keys[1]));
9397
}
9498

9599
static void unbind_net_listener (manapi::ev::shared_tcp conn, manapi::async::shared_cthread ev, absl::AnyInvocable<void(absl::Status)> on_shutdown) {
@@ -161,12 +165,12 @@ static void unbind_net_listener (manapi::ev::shared_tcp conn, manapi::async::sha
161165

162166
static bool task_handle_cancel (grpc_event_engine::experimental::EventEngine::TaskHandle &handle) {
163167
auto it = wgrpc_tasks_find(handle.keys);
164-
if (it == wgrpc_storage.wgrpc_tasks_exists.end())
168+
if (it == current_wgrpc_storage().wgrpc_tasks_exists.end())
165169
return false;
166170

167171
/* timer */
168172
std::unique_ptr<manapi::timer> timer (reinterpret_cast<manapi::timer *> (it->second));
169-
wgrpc_storage.wgrpc_tasks_exists.erase(it);
173+
current_wgrpc_storage().wgrpc_tasks_exists.erase(it);
170174

171175

172176
if (!timer)
@@ -186,11 +190,11 @@ static bool task_handle_cancel (manapi::async::cthread *t, std::size_t idx) {
186190

187191
static bool task_connect_cancel (grpc_event_engine::experimental::EventEngine::ConnectionHandle &keys) {
188192
auto it = wgrpc_connect_find(keys.keys);
189-
if (it == wgrpc_storage.wgrpc_connect_exists.end())
193+
if (it == current_wgrpc_storage().wgrpc_connect_exists.end())
190194
return false;
191195

192196
std::unique_ptr<wgrpc_connection_data_t> p (reinterpret_cast<wgrpc_connection_data_t *> (it->second));
193-
wgrpc_storage.wgrpc_connect_exists.erase(it);
197+
current_wgrpc_storage().wgrpc_connect_exists.erase(it);
194198

195199
if (!p)
196200
return true;
@@ -311,7 +315,7 @@ void manapi::net::wgrpc::net_listener::shutdown(net_listener *id, manapi::ev::sh
311315
if (conn) {
312316
auto &s = async::internal::current_();
313317
if (s == ev) {
314-
wgrpc_storage.wgrpc_tcp_listeners.erase(id);
318+
current_wgrpc_storage().wgrpc_tcp_listeners.erase(id);
315319
unbind_net_listener(std::move(conn), ev, std::move(on_shutdown_cb));
316320
}
317321
else {
@@ -333,7 +337,7 @@ void manapi::net::wgrpc::net_listener::shutdown(net_listener *id, manapi::ev::sh
333337
manapi::status manapi::net::wgrpc::net_listener::set(ev::shared_tcp connection) {
334338
try {
335339
manapi_log_trace2("manapihttp::grpc", "wgrpc:listener tcp: %p", connection.get());
336-
auto res = wgrpc_storage.wgrpc_tcp_listeners.insert(this).second;
340+
auto res = current_wgrpc_storage().wgrpc_tcp_listeners.insert(this).second;
337341
assert(res);
338342
}
339343
catch (std::exception const &) {
@@ -357,7 +361,7 @@ void manapi::net::wgrpc::dns_resolved::LookupHostname(LookupHostnameCallback on_
357361
#if true || MANAPIHTTP_GRPC_SINCE_AT(1,73,0)
358362
auto ctx = manapi::async::internal::current_();
359363
if (!ctx) {
360-
ctx = wgrpc_storage.ctx;
364+
ctx = current_wgrpc_storage().ctx;
361365
}
362366

363367
std::move_only_function<void()> cb = [on_resolve = std::move(on_resolve), name, default_port, ctx] (/*const ev::shared_work &w*/) mutable -> void {
@@ -890,10 +894,11 @@ grpc_event_engine::experimental::EventEngine::ConnectionHandle manapi::net::wgrp
890894
manapi_log_trace2("manapihttp::grpc", "wgrpc:connect tcp: %p", w.get());
891895
std::unique_ptr<wgrpc_connection_data_t> conn_data{nullptr};
892896

893-
auto conn_row = wgrpc_storage.wgrpc_connect_exists.find(ref->index);
894-
if (conn_row != wgrpc_storage.wgrpc_connect_exists.end()) {
897+
auto &zb = current_wgrpc_storage();
898+
auto conn_row = zb.wgrpc_connect_exists.find(ref->index);
899+
if (conn_row != zb.wgrpc_connect_exists.end()) {
895900
conn_data.reset(reinterpret_cast<wgrpc_connection_data_t *>(conn_row->second));
896-
wgrpc_storage.wgrpc_connect_exists.erase(conn_row);
901+
zb.wgrpc_connect_exists.erase(conn_row);
897902
}
898903

899904
if (!ref->on_connect_cb) {
@@ -970,10 +975,11 @@ grpc_event_engine::experimental::EventEngine::ConnectionHandle manapi::net::wgrp
970975
-> void {
971976
std::unique_ptr<wgrpc_connection_data_t> conn_data{nullptr};
972977

973-
auto conn_row = wgrpc_storage.wgrpc_connect_exists.find(ref->index);
974-
if (conn_row != wgrpc_storage.wgrpc_connect_exists.end()) {
978+
auto &zb = current_wgrpc_storage();
979+
auto conn_row = zb.wgrpc_connect_exists.find(ref->index);
980+
if (conn_row != zb.wgrpc_connect_exists.end()) {
975981
conn_data.reset(reinterpret_cast<wgrpc_connection_data_t *>(conn_row->second));
976-
wgrpc_storage.wgrpc_connect_exists.erase(conn_row);
982+
zb.wgrpc_connect_exists.erase(conn_row);
977983
}
978984
if (ref->connect && ref->connect->is_active()) {
979985
manapi::async::current()->eventloop()->stop_watcher(std::move(ref->connect));
@@ -994,13 +1000,14 @@ grpc_event_engine::experimental::EventEngine::ConnectionHandle manapi::net::wgrp
9941000

9951001
while (true) {
9961002
auto const time = static_cast<std::size_t>(std::chrono::steady_clock::now().time_since_epoch().count());
997-
std::size_t indx = (static_cast<std::size_t>(wgrpc_storage.current_connect_index++) << 32) | (time & 0x0000FFFF);
998-
if (wgrpc_storage.current_connect_index == std::numeric_limits<uint32_t>::max()) {
999-
wgrpc_storage.current_connect_index = 0;
1003+
auto &zb = current_wgrpc_storage();
1004+
std::size_t indx = (static_cast<std::size_t>(zb.current_connect_index++) << 32) | (time & 0x0000FFFF);
1005+
if (zb.current_connect_index == std::numeric_limits<uint32_t>::max()) {
1006+
zb.current_connect_index = 0;
10001007
}
10011008

10021009
try {
1003-
auto insert_res = wgrpc_storage.wgrpc_connect_exists.insert(
1010+
auto insert_res = zb.wgrpc_connect_exists.insert(
10041011
{indx, reinterpret_cast<std::uintptr_t>(data.get())});
10051012

10061013
if (!insert_res.second) {
@@ -1024,7 +1031,7 @@ grpc_event_engine::experimental::EventEngine::ConnectionHandle manapi::net::wgrp
10241031
}
10251032
}
10261033

1027-
auto cur = (wgrpc_storage.ctx);
1034+
auto cur = (current_wgrpc_storage().ctx);
10281035
if (!cur) {
10291036
cur = this->primary;
10301037
}
@@ -1074,7 +1081,7 @@ void manapi::net::wgrpc::event_engine_wrapper::Run(absl::AnyInvocable<void()> cl
10741081
if (ctx)
10751082
ctx->etaskpool()->append_task(std::move(closure));
10761083
else {
1077-
auto cur = wgrpc_storage.ctx;
1084+
auto cur = current_wgrpc_storage().ctx;
10781085
if (!cur) {
10791086
cur = this->primary;
10801087
}
@@ -1101,7 +1108,7 @@ void manapi::net::wgrpc::event_engine_wrapper::Run(Closure *closure) {
11011108
ctx->etaskpool()->append_task([closure] ()
11021109
-> void { closure->Run(); });
11031110
else {
1104-
auto cur = wgrpc_storage.ctx;
1111+
auto cur = current_wgrpc_storage().ctx;
11051112
if (!cur) {
11061113
cur = this->primary;
11071114
}
@@ -1222,7 +1229,7 @@ net::wgrpc::event_engine_wrapper::CreateListener(Listener::AcceptCallback on_acc
12221229

12231230
return std::move(b);
12241231
}
1225-
auto cur = wgrpc_storage.ctx;
1232+
auto cur = current_wgrpc_storage().ctx;
12261233
if (!cur) {
12271234
cur = this->primary;
12281235
}
@@ -1299,14 +1306,15 @@ grpc_event_engine::experimental::EventEngine::TaskHandle manapi::net::wgrpc::eve
12991306

13001307
while (true) {
13011308
auto const time = static_cast<std::size_t>(std::chrono::steady_clock::now().time_since_epoch().count());
1302-
td->index = (static_cast<std::size_t>(wgrpc_storage.current_task_index++) << 32) | (time & 0x0000FFFF);
1309+
auto &zb = current_wgrpc_storage();
1310+
td->index = (static_cast<std::size_t>(zb.current_task_index++) << 32) | (time & 0x0000FFFF);
13031311

1304-
if (wgrpc_storage.current_task_index == std::numeric_limits<uint32_t>::max()) {
1305-
wgrpc_storage.current_task_index = 0;
1312+
if (zb.current_task_index == std::numeric_limits<uint32_t>::max()) {
1313+
zb.current_task_index = 0;
13061314
}
13071315

13081316

1309-
auto res = wgrpc_storage.wgrpc_tasks_exists.insert(
1317+
auto res = zb.wgrpc_tasks_exists.insert(
13101318
{td->index, reinterpret_cast<std::uintptr_t>(timer.get())});
13111319

13121320
if (!res.second) {
@@ -1322,7 +1330,7 @@ grpc_event_engine::experimental::EventEngine::TaskHandle manapi::net::wgrpc::eve
13221330
}
13231331
}
13241332

1325-
auto cur = wgrpc_storage.ctx;
1333+
auto cur = current_wgrpc_storage().ctx;
13261334
if (!cur) {
13271335
cur = this->primary;
13281336
}
@@ -1372,8 +1380,8 @@ grpc_event_engine::experimental::EventEngine::TaskHandle manapi::net::wgrpc::eve
13721380
auto const ms = std::max(static_cast<std::size_t>(1),
13731381
static_cast<std::size_t>(when.count() / 1000000));
13741382

1375-
if (wgrpc_storage.current_task_index == std::numeric_limits<uint32_t>::max()) {
1376-
wgrpc_storage.current_task_index = 0;
1383+
if (current_wgrpc_storage().current_task_index == std::numeric_limits<uint32_t>::max()) {
1384+
current_wgrpc_storage().current_task_index = 0;
13771385
}
13781386

13791387
struct wgrpc_timer_data_t {
@@ -1398,9 +1406,9 @@ grpc_event_engine::experimental::EventEngine::TaskHandle manapi::net::wgrpc::eve
13981406

13991407
while (true) {
14001408
auto const time = std::chrono::steady_clock::now().time_since_epoch().count();
1401-
td->index = (static_cast<std::size_t>(wgrpc_storage.current_task_index++) << 32) | (time & 0x0000FFFF);
1409+
td->index = (static_cast<std::size_t>(current_wgrpc_storage().current_task_index++) << 32) | (time & 0x0000FFFF);
14021410

1403-
auto res = wgrpc_storage.wgrpc_tasks_exists.insert({td->index, reinterpret_cast<std::uintptr_t>(timer.get())});
1411+
auto res = current_wgrpc_storage().wgrpc_tasks_exists.insert({td->index, reinterpret_cast<std::uintptr_t>(timer.get())});
14041412

14051413
if (!res.second) {
14061414
continue;
@@ -1415,7 +1423,7 @@ grpc_event_engine::experimental::EventEngine::TaskHandle manapi::net::wgrpc::eve
14151423
}
14161424
}
14171425

1418-
auto cur = (wgrpc_storage.ctx);
1426+
auto cur = (current_wgrpc_storage().ctx);
14191427
if (!cur) {
14201428
cur = this->primary;
14211429
}
@@ -1537,13 +1545,14 @@ manapi::status manapi::net::wgrpc::server_ctx::enable_threadpool(bool status) MA
15371545
void manapi::net::wgrpc::server_ctx::clean() MANAPIHTTP_NOEXCEPT {
15381546
auto &ctx = manapi::async::internal::current_();
15391547
if (ctx) {
1540-
while (!wgrpc_storage.wgrpc_tasks_exists.empty()) {
1541-
auto it = wgrpc_storage.wgrpc_tasks_exists.begin();
1548+
auto &zb = current_wgrpc_storage();
1549+
while (!zb.wgrpc_tasks_exists.empty()) {
1550+
auto it = zb.wgrpc_tasks_exists.begin();
15421551
task_handle_cancel(ctx.get(), it->first);
15431552
}
15441553

1545-
while (!wgrpc_storage.wgrpc_connect_exists.empty()) {
1546-
auto it = wgrpc_storage.wgrpc_connect_exists.begin();
1554+
while (!zb.wgrpc_connect_exists.empty()) {
1555+
auto it = zb.wgrpc_connect_exists.begin();
15471556
task_connect_cancel(ctx.get(), it->first);
15481557
}
15491558
}

0 commit comments

Comments
 (0)