@@ -98,7 +98,7 @@ void BatchSpanProcessor::OnEnd(std::unique_ptr<Recordable> &&span) noexcept
9898 size_t buffer_size = buffer_.size ();
9999 if (buffer_size >= max_queue_size_ / 2 || buffer_size >= max_export_batch_size_)
100100 {
101- // signal the worker thread
101+ // Best effort wakeup for worker thread.
102102 synchronization_data_->cv .notify_all ();
103103 }
104104}
@@ -127,6 +127,7 @@ bool BatchSpanProcessor::ForceFlush(std::chrono::microseconds timeout) noexcept
127127 if (synchronization_data_->force_flush_pending_sequence .load (std::memory_order_acquire) >
128128 synchronization_data_->force_flush_notified_sequence .load (std::memory_order_acquire))
129129 {
130+ std::lock_guard<std::mutex> cv_lock (synchronization_data_->cv_m );
130131 synchronization_data_->is_force_wakeup_background_worker .store (true ,
131132 std::memory_order_release);
132133 synchronization_data_->cv .notify_all ();
@@ -188,18 +189,21 @@ void BatchSpanProcessor::DoBackgroundWork()
188189 }
189190#endif /* ENABLE_THREAD_INSTRUMENTATION_PREVIEW */
190191
191- // Wait for `timeout` milliseconds
192- std::unique_lock<std::mutex> lk (synchronization_data_->cv_m );
193- synchronization_data_->cv .wait_for (lk, timeout, [this ] {
194- if (synchronization_data_->is_force_wakeup_background_worker .load (std::memory_order_acquire))
195- {
196- return true ;
197- }
198-
199- return !buffer_.empty ();
200- });
201- synchronization_data_->is_force_wakeup_background_worker .store (false ,
202- std::memory_order_release);
192+ // Wait for `timeout` milliseconds.
193+ {
194+ std::unique_lock<std::mutex> lk (synchronization_data_->cv_m );
195+ synchronization_data_->cv .wait_for (lk, timeout, [this ] {
196+ if (synchronization_data_->is_force_wakeup_background_worker .load (
197+ std::memory_order_acquire))
198+ {
199+ return true ;
200+ }
201+
202+ return !buffer_.empty ();
203+ });
204+ synchronization_data_->is_force_wakeup_background_worker .store (false ,
205+ std::memory_order_release);
206+ }
203207
204208#ifdef ENABLE_THREAD_INSTRUMENTATION_PREVIEW
205209 if (worker_thread_instrumentation_ != nullptr )
@@ -309,6 +313,7 @@ void BatchSpanProcessor::NotifyCompletion(
309313 exporter->ForceFlush (timeout);
310314 }
311315
316+ std::lock_guard<std::mutex> lock (synchronization_data->force_flush_cv_m );
312317 std::uint64_t notified_sequence =
313318 synchronization_data->force_flush_notified_sequence .load (std::memory_order_acquire);
314319 while (notify_force_flush > notified_sequence)
@@ -376,8 +381,12 @@ bool BatchSpanProcessor::InternalShutdown(std::chrono::microseconds timeout) noe
376381
377382 if (worker_thread_.joinable ())
378383 {
379- synchronization_data_->is_force_wakeup_background_worker .store (true , std::memory_order_release);
380- synchronization_data_->cv .notify_all ();
384+ {
385+ std::lock_guard<std::mutex> cv_lock (synchronization_data_->cv_m );
386+ synchronization_data_->is_force_wakeup_background_worker .store (true ,
387+ std::memory_order_release);
388+ synchronization_data_->cv .notify_all ();
389+ }
381390 worker_thread_.join ();
382391 }
383392
0 commit comments