aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/cpp
diff options
context:
space:
mode:
authorGravatar Yuchen Zeng <zyc@google.com>2017-08-25 14:10:30 -0700
committerGravatar Yuchen Zeng <zyc@google.com>2017-08-25 15:17:45 -0700
commit4a11ecc076ad376833171e5f40105dc8e4320acd (patch)
tree2326f22a2d58edfc2acdb97771838b95e017bf2b /src/cpp
parent6514a0df72d9425ae5058eab6dd120179b2105d1 (diff)
Add ChannelConnectivityWatcher::Ref()/Unref()
Diffstat (limited to 'src/cpp')
-rw-r--r--src/cpp/client/channel_cc.cc32
1 files changed, 16 insertions, 16 deletions
diff --git a/src/cpp/client/channel_cc.cc b/src/cpp/client/channel_cc.cc
index acd8271dcc..0d2e834a7f 100644
--- a/src/cpp/client/channel_cc.cc
+++ b/src/cpp/client/channel_cc.cc
@@ -71,16 +71,7 @@ class ChannelConnectivityWatcher {
public:
static void StartWatching(grpc_channel* channel) {
char* env = gpr_getenv("GRPC_DISABLE_CHANNEL_CONNECTIVITY_WATCHER");
- bool disabled = false;
- if (env != nullptr) {
- static const char* truthy[] = {"yes", "true", "1"};
- for (size_t i = 0; i < GPR_ARRAY_SIZE(truthy); i++) {
- if (0 == gpr_stricmp(env, truthy[i])) {
- disabled = true;
- break;
- }
- }
- }
+ bool disabled = gpr_is_true(env);
gpr_free(env);
if (!disabled) {
gpr_once_init(&g_connectivity_watcher_once_, InitConnectivityWatcherOnce);
@@ -125,11 +116,7 @@ class ChannelConnectivityWatcher {
void* shutdown_tag = NULL;
channel_state->shutdown_cq.Next(&shutdown_tag, &ok);
delete channel_state;
- if (gpr_unref(&ref_)) {
- gpr_mu_lock(&g_watcher_mu_);
- delete g_watcher_;
- g_watcher_ = nullptr;
- gpr_mu_unlock(&g_watcher_mu_);
+ if (Unref()) {
break;
}
} else {
@@ -143,7 +130,7 @@ class ChannelConnectivityWatcher {
void StartWatchingLocked(grpc_channel* channel) {
if (thd_id_ != 0) {
- gpr_ref(&ref_);
+ Ref();
ChannelState* channel_state = new ChannelState(channel);
// The first grpc_channel_watch_connectivity_state() is not used to
// monitor the channel state change, but to hold a reference of the
@@ -160,6 +147,19 @@ class ChannelConnectivityWatcher {
}
}
+ void Ref() { gpr_ref(&ref_); }
+
+ bool Unref() {
+ if (gpr_unref(&ref_)) {
+ gpr_mu_lock(&g_watcher_mu_);
+ delete g_watcher_;
+ g_watcher_ = nullptr;
+ gpr_mu_unlock(&g_watcher_mu_);
+ return true;
+ }
+ return false;
+ }
+
static void InitOnce() { gpr_mu_init(&g_watcher_mu_); }
friend void WatchStateChange(void* arg);