diff options
author | ncteisen <ncteisen@gmail.com> | 2018-10-11 11:33:26 -0700 |
---|---|---|
committer | ncteisen <ncteisen@gmail.com> | 2018-10-12 09:06:16 -0400 |
commit | 773434a48450be049247e1b5396e1b6d149984aa (patch) | |
tree | fc6bd863b49f0599e5c755a5a99501729f500d46 /src/core | |
parent | bb44ca2c21a18fa48223228c73fa208e13eac44f (diff) |
Atomically set BaseNode uuid
Diffstat (limited to 'src/core')
-rw-r--r-- | src/core/lib/channel/channelz.cc | 6 | ||||
-rw-r--r-- | src/core/lib/channel/channelz.h | 4 | ||||
-rw-r--r-- | src/core/lib/channel/channelz_registry.cc | 4 | ||||
-rw-r--r-- | src/core/lib/channel/channelz_registry.h | 4 |
4 files changed, 11 insertions, 7 deletions
diff --git a/src/core/lib/channel/channelz.cc b/src/core/lib/channel/channelz.cc index f3d0c03715..33577d890a 100644 --- a/src/core/lib/channel/channelz.cc +++ b/src/core/lib/channel/channelz.cc @@ -43,8 +43,10 @@ namespace grpc_core { namespace channelz { -BaseNode::BaseNode(EntityType type) - : type_(type), uuid_(ChannelzRegistry::Register(this)) {} +BaseNode::BaseNode(EntityType type) : type_(type), uuid_(-1) { + // The registry will set uuid_ under its lock. + ChannelzRegistry::Register(this); +} BaseNode::~BaseNode() { ChannelzRegistry::Unregister(uuid_); } diff --git a/src/core/lib/channel/channelz.h b/src/core/lib/channel/channelz.h index d8113585c2..fddef793fb 100644 --- a/src/core/lib/channel/channelz.h +++ b/src/core/lib/channel/channelz.h @@ -92,8 +92,10 @@ class BaseNode : public RefCounted<BaseNode> { intptr_t uuid() const { return uuid_; } private: + // to allow the ChannelzRegistry to set uuid_ under its lock. + friend class ChannelzRegistry; const EntityType type_; - const intptr_t uuid_; + intptr_t uuid_; }; // This class is a helper class for channelz entities that deal with Channels, diff --git a/src/core/lib/channel/channelz_registry.cc b/src/core/lib/channel/channelz_registry.cc index 1b54b19be3..67e56ed791 100644 --- a/src/core/lib/channel/channelz_registry.cc +++ b/src/core/lib/channel/channelz_registry.cc @@ -53,10 +53,10 @@ ChannelzRegistry::ChannelzRegistry() { gpr_mu_init(&mu_); } ChannelzRegistry::~ChannelzRegistry() { gpr_mu_destroy(&mu_); } -intptr_t ChannelzRegistry::InternalRegister(BaseNode* node) { +void ChannelzRegistry::InternalRegister(BaseNode* node) { MutexLock lock(&mu_); entities_.push_back(node); - return ++uuid_generator_; + node->uuid_ = ++uuid_generator_; } void ChannelzRegistry::MaybePerformCompactionLocked() { diff --git a/src/core/lib/channel/channelz_registry.h b/src/core/lib/channel/channelz_registry.h index 9c43d960d3..ea6ab6c8e5 100644 --- a/src/core/lib/channel/channelz_registry.h +++ b/src/core/lib/channel/channelz_registry.h @@ -44,7 +44,7 @@ class ChannelzRegistry { // To be called in grpc_shutdown(); static void Shutdown(); - static intptr_t Register(BaseNode* node) { + static void Register(BaseNode* node) { return Default()->InternalRegister(node); } static void Unregister(intptr_t uuid) { Default()->InternalUnregister(uuid); } @@ -74,7 +74,7 @@ class ChannelzRegistry { static ChannelzRegistry* Default(); // globally registers an Entry. Returns its unique uuid - intptr_t InternalRegister(BaseNode* node); + void InternalRegister(BaseNode* node); // globally unregisters the object that is associated to uuid. Also does // sanity check that an object doesn't try to unregister the wrong type. |