Fix Segmentation fault on TimerImpl.TestFireNow

PiperOrigin-RevId: 788087990
This commit is contained in:
Edwin Wu
2025-07-28 11:25:37 -07:00
committed by Copybara-Service
parent 736e9dba5c
commit f487377f5c
2 changed files with 68 additions and 24 deletions
+12 -6
View File
@@ -19,6 +19,8 @@
#include <utility> #include <utility>
#include "absl/base/thread_annotations.h"
#include "absl/synchronization/mutex.h"
#include "internal/platform/implementation/timer.h" #include "internal/platform/implementation/timer.h"
namespace nearby { namespace nearby {
@@ -27,18 +29,22 @@ namespace apple {
class Timer : public api::Timer { class Timer : public api::Timer {
public: public:
Timer() = default; Timer() = default;
~Timer() override; ~Timer() override ABSL_LOCKS_EXCLUDED(mutex_);
bool Create(int delay, int interval, bool Create(int delay, int interval,
absl::AnyInvocable<void()> callback) override; absl::AnyInvocable<void()> callback) override
ABSL_LOCKS_EXCLUDED(mutex_);
bool Stop() override; bool Stop() override ABSL_LOCKS_EXCLUDED(mutex_);
bool FireNow() override; bool FireNow() override ABSL_LOCKS_EXCLUDED(mutex_);
private: private:
absl::AnyInvocable<void()> callback_; absl::Mutex mutex_;
dispatch_source_t timer_; absl::CondVar condvar_ ABSL_GUARDED_BY(mutex_);
bool callback_running_ ABSL_GUARDED_BY(mutex_) = false;
absl::AnyInvocable<void()> callback_ ABSL_GUARDED_BY(mutex_);
dispatch_source_t timer_ ABSL_GUARDED_BY(mutex_);
}; };
} // namespace apple } // namespace apple
+56 -18
View File
@@ -16,6 +16,7 @@
#include <utility> #include <utility>
#include "absl/synchronization/mutex.h"
#import "internal/platform/implementation/apple/Log/GNCLogger.h" #import "internal/platform/implementation/apple/Log/GNCLogger.h"
#include "internal/platform/implementation/timer.h" #include "internal/platform/implementation/timer.h"
@@ -37,7 +38,8 @@ bool Timer::Create(int delay, int interval, absl::AnyInvocable<void()> callback)
return false; return false;
} }
if (timer_ != nil) { absl::MutexLock lock(&mutex_);
if (timer_ != nullptr) {
GNCLoggerError(@"Timer has already started."); GNCLoggerError(@"Timer has already started.");
return false; return false;
} }
@@ -52,16 +54,33 @@ bool Timer::Create(int delay, int interval, absl::AnyInvocable<void()> callback)
dispatch_get_main_queue()); dispatch_get_main_queue());
dispatch_source_set_event_handler(timer_, ^{ dispatch_source_set_event_handler(timer_, ^{
// If our interval is `DISPATCH_TIME_FOREVER`, it means we only want the timer to fire once. absl::AnyInvocable<void()> callback_to_run = nullptr;
// We need to cancel the timer before the callback is called since the `Timer` object may no bool is_one_shot = (intervalInNanoseconds == DISPATCH_TIME_FOREVER);
// longer exist immediately after invoking the callback. {
if (intervalInNanoseconds == DISPATCH_TIME_FOREVER && timer_ != nil) { absl::MutexLock lock(&mutex_);
dispatch_source_cancel(timer_); // If Stop() was called concurrently, the callback will be null.
timer_ = nil; if (!callback_ || callback_running_) {
return;
}
callback_running_ = true;
if (is_one_shot && timer_ != nullptr) {
dispatch_source_cancel(timer_);
timer_ = nullptr;
}
callback_to_run = std::move(callback_);
} }
if (callback_ != nil) { if (callback_to_run) {
callback_(); callback_to_run();
}
{
absl::MutexLock lock(&mutex_);
if (!is_one_shot && callback_to_run) {
// For periodic timers, move the callback back for the next run.
callback_ = std::move(callback_to_run);
}
callback_running_ = false;
condvar_.SignalAll();
} }
}); });
@@ -72,22 +91,41 @@ bool Timer::Create(int delay, int interval, absl::AnyInvocable<void()> callback)
} }
bool Timer::Stop() { bool Timer::Stop() {
if (timer_ != nil) { absl::MutexLock lock(&mutex_);
if (timer_ != nullptr) {
dispatch_source_cancel(timer_); dispatch_source_cancel(timer_);
timer_ = nullptr;
} }
timer_ = nil; // Wait for a potentially running callback to finish before destroying it.
callback_ = nil; while (callback_running_) {
condvar_.Wait(&mutex_);
}
callback_ = nullptr;
return true; return true;
} }
bool Timer::FireNow() { bool Timer::FireNow() {
if (callback_ != nil) { absl::AnyInvocable<void()> callback_to_run;
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^(void) { {
callback_(); absl::MutexLock lock(&mutex_);
});
return true; // Don't fire if there's no callback or if a callback is already in progress.
if (!callback_ || callback_running_) {
return false;
}
callback_running_ = true;
callback_to_run = std::move(callback_);
} }
return false;
// Execute callback outside of the lock.
callback_to_run();
{
absl::MutexLock lock(&mutex_);
callback_ = std::move(callback_to_run);
callback_running_ = false;
condvar_.Signal(); // Notify Stop() if it's waiting.
}
return true;
} }
} // namespace apple } // namespace apple