cordz_handle.cc 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. // Copyright 2019 The Abseil Authors.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // https://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #include "absl/strings/internal/cordz_handle.h"
  15. #include <atomic>
  16. #include "absl/base/internal/raw_logging.h" // For ABSL_RAW_CHECK
  17. #include "absl/base/internal/spinlock.h"
  18. namespace absl {
  19. ABSL_NAMESPACE_BEGIN
  20. namespace cord_internal {
  21. using ::absl::base_internal::SpinLockHolder;
  22. ABSL_CONST_INIT CordzHandle::Queue CordzHandle::global_queue_(absl::kConstInit);
  23. CordzHandle::CordzHandle(bool is_snapshot) : is_snapshot_(is_snapshot) {
  24. if (is_snapshot) {
  25. SpinLockHolder lock(&queue_->mutex);
  26. CordzHandle* dq_tail = queue_->dq_tail.load(std::memory_order_acquire);
  27. if (dq_tail != nullptr) {
  28. dq_prev_ = dq_tail;
  29. dq_tail->dq_next_ = this;
  30. }
  31. queue_->dq_tail.store(this, std::memory_order_release);
  32. }
  33. }
  34. CordzHandle::~CordzHandle() {
  35. ODRCheck();
  36. if (is_snapshot_) {
  37. std::vector<CordzHandle*> to_delete;
  38. {
  39. SpinLockHolder lock(&queue_->mutex);
  40. CordzHandle* next = dq_next_;
  41. if (dq_prev_ == nullptr) {
  42. // We were head of the queue, delete every CordzHandle until we reach
  43. // either the end of the list, or a snapshot handle.
  44. while (next && !next->is_snapshot_) {
  45. to_delete.push_back(next);
  46. next = next->dq_next_;
  47. }
  48. } else {
  49. // Another CordzHandle existed before this one, don't delete anything.
  50. dq_prev_->dq_next_ = next;
  51. }
  52. if (next) {
  53. next->dq_prev_ = dq_prev_;
  54. } else {
  55. queue_->dq_tail.store(dq_prev_, std::memory_order_release);
  56. }
  57. }
  58. for (CordzHandle* handle : to_delete) {
  59. delete handle;
  60. }
  61. }
  62. }
  63. bool CordzHandle::SafeToDelete() const {
  64. return is_snapshot_ || queue_->IsEmpty();
  65. }
  66. void CordzHandle::Delete(CordzHandle* handle) {
  67. assert(handle);
  68. if (handle) {
  69. handle->ODRCheck();
  70. Queue* const queue = handle->queue_;
  71. if (!handle->SafeToDelete()) {
  72. SpinLockHolder lock(&queue->mutex);
  73. CordzHandle* dq_tail = queue->dq_tail.load(std::memory_order_acquire);
  74. if (dq_tail != nullptr) {
  75. handle->dq_prev_ = dq_tail;
  76. dq_tail->dq_next_ = handle;
  77. queue->dq_tail.store(handle, std::memory_order_release);
  78. return;
  79. }
  80. }
  81. delete handle;
  82. }
  83. }
  84. std::vector<const CordzHandle*> CordzHandle::DiagnosticsGetDeleteQueue() {
  85. std::vector<const CordzHandle*> handles;
  86. SpinLockHolder lock(&global_queue_.mutex);
  87. CordzHandle* dq_tail = global_queue_.dq_tail.load(std::memory_order_acquire);
  88. for (const CordzHandle* p = dq_tail; p; p = p->dq_prev_) {
  89. handles.push_back(p);
  90. }
  91. return handles;
  92. }
  93. bool CordzHandle::DiagnosticsHandleIsSafeToInspect(
  94. const CordzHandle* handle) const {
  95. ODRCheck();
  96. if (!is_snapshot_) return false;
  97. if (handle == nullptr) return true;
  98. if (handle->is_snapshot_) return false;
  99. bool snapshot_found = false;
  100. SpinLockHolder lock(&queue_->mutex);
  101. for (const CordzHandle* p = queue_->dq_tail; p; p = p->dq_prev_) {
  102. if (p == handle) return !snapshot_found;
  103. if (p == this) snapshot_found = true;
  104. }
  105. ABSL_ASSERT(snapshot_found); // Assert that 'this' is in delete queue.
  106. return true;
  107. }
  108. std::vector<const CordzHandle*>
  109. CordzHandle::DiagnosticsGetSafeToInspectDeletedHandles() {
  110. ODRCheck();
  111. std::vector<const CordzHandle*> handles;
  112. if (!is_snapshot()) {
  113. return handles;
  114. }
  115. SpinLockHolder lock(&queue_->mutex);
  116. for (const CordzHandle* p = dq_next_; p != nullptr; p = p->dq_next_) {
  117. if (!p->is_snapshot()) {
  118. handles.push_back(p);
  119. }
  120. }
  121. return handles;
  122. }
  123. } // namespace cord_internal
  124. ABSL_NAMESPACE_END
  125. } // namespace absl