File tree Expand file tree Collapse file tree 3 files changed +12
-11
lines changed
include/opentelemetry/sdk/metrics Expand file tree Collapse file tree 3 files changed +12
-11
lines changed Original file line number Diff line number Diff line change 3
3
4
4
#pragma once
5
5
6
+ #include < atomic>
6
7
#include < chrono>
7
8
#include < memory>
8
9
9
- #include " opentelemetry/common/spin_lock_mutex.h"
10
10
#include " opentelemetry/nostd/function_ref.h"
11
11
#include " opentelemetry/sdk/metrics/data/metric_data.h"
12
12
#include " opentelemetry/sdk/metrics/instruments.h"
@@ -72,8 +72,7 @@ class MetricReader
72
72
protected:
73
73
private:
74
74
MetricProducer *metric_producer_;
75
- mutable opentelemetry::common::SpinLockMutex lock_;
76
- bool shutdown_;
75
+ std::atomic<bool > shutdown_{false };
77
76
};
78
77
} // namespace metrics
79
78
} // namespace sdk
Original file line number Diff line number Diff line change 2
2
// SPDX-License-Identifier: Apache-2.0
3
3
4
4
#include " opentelemetry/sdk/metrics/export/periodic_exporting_metric_reader.h"
5
+ #include " opentelemetry/common/spin_lock_mutex.h"
5
6
#include " opentelemetry/sdk/common/global_log_handler.h"
6
7
#include " opentelemetry/sdk/metrics/push_metric_exporter.h"
7
8
@@ -101,6 +102,7 @@ bool PeriodicExportingMetricReader::CollectAndExportOnce()
101
102
bool notify_force_flush = is_force_flush_pending_.exchange (false , std::memory_order_acq_rel);
102
103
if (notify_force_flush)
103
104
{
105
+ std::unique_lock<std::mutex> lk (force_flush_m_);
104
106
is_force_flush_notified_.store (true , std::memory_order_release);
105
107
force_flush_cv_.notify_one ();
106
108
}
@@ -191,7 +193,11 @@ bool PeriodicExportingMetricReader::OnShutDown(std::chrono::microseconds timeout
191
193
{
192
194
if (worker_thread_.joinable ())
193
195
{
194
- cv_.notify_one ();
196
+ {
197
+ // ensure that `cv_` is awaiting, and the update doesn't get lost
198
+ std::unique_lock<std::mutex> lk (cv_m_);
199
+ cv_.notify_all ();
200
+ }
195
201
worker_thread_.join ();
196
202
}
197
203
return exporter_->Shutdown (timeout);
Original file line number Diff line number Diff line change @@ -48,10 +48,7 @@ bool MetricReader::Shutdown(std::chrono::microseconds timeout) noexcept
48
48
OTEL_INTERNAL_LOG_WARN (" MetricReader::Shutdown - Cannot invoke shutdown twice!" );
49
49
}
50
50
51
- {
52
- const std::lock_guard<opentelemetry::common::SpinLockMutex> locked (lock_);
53
- shutdown_ = true ;
54
- }
51
+ shutdown_.store (true , std::memory_order_release);
55
52
56
53
if (!OnShutDown (timeout))
57
54
{
@@ -65,7 +62,7 @@ bool MetricReader::Shutdown(std::chrono::microseconds timeout) noexcept
65
62
bool MetricReader::ForceFlush (std::chrono::microseconds timeout) noexcept
66
63
{
67
64
bool status = true ;
68
- if (shutdown_ )
65
+ if (IsShutdown () )
69
66
{
70
67
OTEL_INTERNAL_LOG_WARN (" MetricReader::Shutdown Cannot invoke Force flush on shutdown reader!" );
71
68
}
@@ -79,8 +76,7 @@ bool MetricReader::ForceFlush(std::chrono::microseconds timeout) noexcept
79
76
80
77
bool MetricReader::IsShutdown () const noexcept
81
78
{
82
- const std::lock_guard<opentelemetry::common::SpinLockMutex> locked (lock_);
83
- return shutdown_;
79
+ return shutdown_.load (std::memory_order_acquire);
84
80
}
85
81
86
82
} // namespace metrics
You can’t perform that action at this time.
0 commit comments