Add back tracked

reviewable/pr17308/r2
yang-g 6 years ago
parent 86b23adc7f
commit 6b67506bae
  1. 10
      src/core/lib/gprpp/thd.h
  2. 8
      src/core/lib/gprpp/thd_posix.cc
  3. 6
      src/core/lib/gprpp/thd_windows.cc
  4. 2
      src/core/lib/surface/init.cc

@ -49,7 +49,7 @@ class Thread {
public: public:
class Options { class Options {
public: public:
Options() : joinable_(true) {} Options() : joinable_(true), tracked_(true) {}
/// Set whether the thread is joinable or detached. /// Set whether the thread is joinable or detached.
Options& set_joinable(bool joinable) { Options& set_joinable(bool joinable) {
joinable_ = joinable; joinable_ = joinable;
@ -57,8 +57,16 @@ class Thread {
} }
bool joinable() const { return joinable_; } bool joinable() const { return joinable_; }
/// Set whether the thread is tracked for fork support.
Options& set_tracked(bool tracked) {
tracked_ = tracked;
return *this;
}
bool tracked() const { return tracked_; }
private: private:
bool joinable_; bool joinable_;
bool tracked_;
}; };
/// Default constructor only to allow use in structs that lack constructors /// Default constructor only to allow use in structs that lack constructors
/// Does not produce a validly-constructed thread; must later /// Does not produce a validly-constructed thread; must later

@ -45,6 +45,7 @@ struct thd_arg {
void* arg; /* argument to a thread */ void* arg; /* argument to a thread */
const char* name; /* name of thread. Can be nullptr. */ const char* name; /* name of thread. Can be nullptr. */
bool joinable; bool joinable;
bool tracked;
}; };
class ThreadInternalsPosix : public internal::ThreadInternalsInterface { class ThreadInternalsPosix : public internal::ThreadInternalsInterface {
@ -64,7 +65,10 @@ class ThreadInternalsPosix : public internal::ThreadInternalsInterface {
info->arg = arg; info->arg = arg;
info->name = thd_name; info->name = thd_name;
info->joinable = options.joinable(); info->joinable = options.joinable();
info->tracked = options.tracked();
if (options.tracked()) {
Fork::IncThreadCount(); Fork::IncThreadCount();
}
GPR_ASSERT(pthread_attr_init(&attr) == 0); GPR_ASSERT(pthread_attr_init(&attr) == 0);
if (options.joinable()) { if (options.joinable()) {
@ -108,7 +112,9 @@ class ThreadInternalsPosix : public internal::ThreadInternalsInterface {
} }
(*arg.body)(arg.arg); (*arg.body)(arg.arg);
if (arg.tracked) {
Fork::DecThreadCount(); Fork::DecThreadCount();
}
return nullptr; return nullptr;
}, },
info) == 0); info) == 0);
@ -118,9 +124,11 @@ class ThreadInternalsPosix : public internal::ThreadInternalsInterface {
if (!(*success)) { if (!(*success)) {
/* don't use gpr_free, as this was allocated using malloc (see above) */ /* don't use gpr_free, as this was allocated using malloc (see above) */
free(info); free(info);
if (options.tracked()) {
Fork::DecThreadCount(); Fork::DecThreadCount();
} }
} }
}
~ThreadInternalsPosix() override { ~ThreadInternalsPosix() override {
gpr_mu_destroy(&mu_); gpr_mu_destroy(&mu_);

@ -41,7 +41,6 @@
#error "Unknown compiler - please file a bug report" #error "Unknown compiler - please file a bug report"
#endif #endif
namespace grpc_core {
namespace { namespace {
class ThreadInternalsWindows; class ThreadInternalsWindows;
struct thd_info { struct thd_info {
@ -54,6 +53,11 @@ struct thd_info {
thread_local struct thd_info* g_thd_info; thread_local struct thd_info* g_thd_info;
} // namespace
namespace grpc_core {
namespace {
class ThreadInternalsWindows : public internal::ThreadInternalsInterface { class ThreadInternalsWindows : public internal::ThreadInternalsInterface {
public: public:
ThreadInternalsWindows(void (*thd_body)(void* arg), void* arg, bool* success, ThreadInternalsWindows(void (*thd_body)(void* arg), void* arg, bool* success,

@ -215,7 +215,7 @@ void grpc_shutdown(void) {
// currently in an executor thread. // currently in an executor thread.
grpc_core::Thread cleanup_thread( grpc_core::Thread cleanup_thread(
"grpc_shutdown", grpc_shutdown_internal, nullptr, nullptr, "grpc_shutdown", grpc_shutdown_internal, nullptr, nullptr,
grpc_core::Thread::Options().set_joinable(false)); grpc_core::Thread::Options().set_joinable(false).set_tracked(false));
cleanup_thread.Start(); cleanup_thread.Start();
} }
} }

Loading…
Cancel
Save