Skip to content
This repository was archived by the owner on Jul 31, 2023. It is now read-only.

Commit 323be24

Browse files
g-easyisturdy
authored andcommitted
Get rid of size_ and std::atomic. (#23)
Condition is run under span_mu_ so just read out the vector's size.
1 parent 548627b commit 323be24

2 files changed

Lines changed: 10 additions & 17 deletions

File tree

opencensus/trace/internal/span_exporter_impl.cc

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ SpanExporterImpl* SpanExporterImpl::Get() {
3535
// Create detached worker thread
3636
SpanExporterImpl::SpanExporterImpl(uint32_t buffer_size,
3737
absl::Duration interval)
38-
: buffer_size_(buffer_size), interval_(interval), size_(0) {}
38+
: buffer_size_(buffer_size), interval_(interval) {}
3939

4040
void SpanExporterImpl::RegisterHandler(
4141
std::unique_ptr<SpanExporter::Handler> handler) {
@@ -50,7 +50,6 @@ void SpanExporterImpl::AddSpan(
5050
const std::shared_ptr<opencensus::trace::SpanImpl>& span_impl) {
5151
absl::MutexLock l(&span_mu_);
5252
spans_.emplace_back(span_impl);
53-
size_.fetch_add(1, std::memory_order_acq_rel);
5453
}
5554

5655
void SpanExporterImpl::StartExportThread() {
@@ -68,20 +67,19 @@ void SpanExporterImpl::RunWorkerLoop() {
6867
{
6968
absl::MutexLock l(&span_mu_);
7069
// Wait until batch is full or interval time has been exceeded.
71-
span_mu_.AwaitWithDeadline(
72-
absl::Condition(
73-
+[](SpanExporterImpl* ptr) {
74-
return (ptr->size_.load(std::memory_order_acquire) >=
75-
ptr->buffer_size_);
76-
},
77-
this),
78-
next_forced_export_time);
70+
span_mu_.AwaitWithDeadline(absl::Condition(
71+
+[](SpanExporterImpl* ptr) {
72+
ptr->span_mu_.AssertHeld();
73+
return ptr->spans_.size() >=
74+
ptr->buffer_size_;
75+
},
76+
this),
77+
next_forced_export_time);
7978
next_forced_export_time = absl::Now() + interval_;
8079
if (spans_.empty()) {
8180
continue;
8281
}
8382
std::swap(spans_copy_, spans_);
84-
size_.store(0, std::memory_order_release);
8583
}
8684
for (const auto& span : spans_copy_) {
8785
span_data_.emplace_back(span->ToSpanData());

opencensus/trace/internal/span_exporter_impl.h

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
#ifndef OPENCENSUS_TRACE_INTERNAL_SPAN_EXPORTER_IMPL_H_
1616
#define OPENCENSUS_TRACE_INTERNAL_SPAN_EXPORTER_IMPL_H_
1717

18-
#include <atomic>
1918
#include <functional>
2019
#include <memory>
2120
#include <string>
@@ -74,18 +73,14 @@ class SpanExporterImpl {
7473
static SpanExporterImpl* span_exporter_;
7574
const uint32_t buffer_size_;
7675
const absl::Duration interval_;
77-
// This is intentionally not guarded by span_mu_. You cannot lock the waiting
78-
// mutex within an AwaitWithTimeout, so we need to store the size in another
79-
// variable.
80-
std::atomic<size_t> size_;
8176
mutable absl::Mutex span_mu_;
8277
mutable absl::Mutex handler_mu_;
8378
std::vector<std::shared_ptr<opencensus::trace::SpanImpl>> spans_
8479
GUARDED_BY(span_mu_);
8580
std::vector<std::unique_ptr<SpanExporter::Handler>> handlers_
8681
GUARDED_BY(handler_mu_);
8782
bool thread_started_ GUARDED_BY(handler_mu_) = false;
88-
std::thread t_;
83+
std::thread t_ GUARDED_BY(handler_mu_);
8984
};
9085

9186
} // namespace exporter

0 commit comments

Comments
 (0)