Skip to content

Commit

Permalink
Merge pull request grpc#18097 from equinox1993/master
Browse files Browse the repository at this point in the history
Move thread body logic into a private static method
  • Loading branch information
vjpai authored Feb 20, 2019
2 parents fef60e1 + 72035e7 commit 318c677
Showing 1 changed file with 15 additions and 18 deletions.
33 changes: 15 additions & 18 deletions src/core/lib/gprpp/thd_windows.cc
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,8 @@

#if defined(_MSC_VER)
#define thread_local __declspec(thread)
#define WIN_LAMBDA
#elif defined(__GNUC__)
#define thread_local __thread
#define WIN_LAMBDA WINAPI
#else
#error "Unknown compiler - please file a bug report"
#endif
Expand Down Expand Up @@ -71,22 +69,7 @@ class ThreadInternalsWindows
gpr_free(info_);
*success = false;
} else {
handle = CreateThread(
nullptr, 64 * 1024,
[](void* v) WIN_LAMBDA -> DWORD {
g_thd_info = static_cast<thd_info*>(v);
gpr_mu_lock(&g_thd_info->thread->mu_);
while (!g_thd_info->thread->started_) {
gpr_cv_wait(&g_thd_info->thread->ready_, &g_thd_info->thread->mu_,
gpr_inf_future(GPR_CLOCK_MONOTONIC));
}
gpr_mu_unlock(&g_thd_info->thread->mu_);
g_thd_info->body(g_thd_info->arg);
BOOL ret = SetEvent(g_thd_info->join_event);
GPR_ASSERT(ret);
return 0;
},
info_, 0, nullptr);
handle = CreateThread(nullptr, 64 * 1024, thread_body, info_, 0, nullptr);
if (handle == nullptr) {
destroy_thread();
*success = false;
Expand Down Expand Up @@ -116,6 +99,20 @@ class ThreadInternalsWindows
}

private:
static DWORD WINAPI thread_body(void* v) {
g_thd_info = static_cast<thd_info*>(v);
gpr_mu_lock(&g_thd_info->thread->mu_);
while (!g_thd_info->thread->started_) {
gpr_cv_wait(&g_thd_info->thread->ready_, &g_thd_info->thread->mu_,
gpr_inf_future(GPR_CLOCK_MONOTONIC));
}
gpr_mu_unlock(&g_thd_info->thread->mu_);
g_thd_info->body(g_thd_info->arg);
BOOL ret = SetEvent(g_thd_info->join_event);
GPR_ASSERT(ret);
return 0;
}

void destroy_thread() {
CloseHandle(info_->join_event);
gpr_free(info_);
Expand Down

0 comments on commit 318c677

Please sign in to comment.