Add back tracked

reviewable/pr17308/r2
yang-g 6 years ago
parent 86b23adc7f
commit 6b67506bae
  1. 10
      src/core/lib/gprpp/thd.h
  2. 14
      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:
class Options {
public:
Options() : joinable_(true) {}
Options() : joinable_(true), tracked_(true) {}
/// Set whether the thread is joinable or detached.
Options& set_joinable(bool joinable) {
joinable_ = joinable;
@ -57,8 +57,16 @@ class Thread {
}
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:
bool joinable_;
bool tracked_;
};
/// Default constructor only to allow use in structs that lack constructors
/// Does not produce a validly-constructed thread; must later

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

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

@ -215,7 +215,7 @@ void grpc_shutdown(void) {
// currently in an executor thread.
grpc_core::Thread cleanup_thread(
"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();
}
}

Loading…
Cancel
Save