mirror of
https://github.com/kidfromjupiter/nearby.git
synced 2026-09-14 22:56:12 -04:00
Mutex guard FakeClock
PiperOrigin-RevId: 527382176
This commit is contained in:
committed by
Copybara-Service
parent
a4be31137f
commit
698ca77f2a
@@ -21,7 +21,10 @@
|
||||
|
||||
namespace nearby {
|
||||
|
||||
absl::Time FakeClock::Now() const { return now_; }
|
||||
absl::Time FakeClock::Now() const {
|
||||
absl::MutexLock lock(&mutex_);
|
||||
return now_;
|
||||
}
|
||||
|
||||
void FakeClock::AddObserver(absl::string_view name,
|
||||
std::function<void()> observer) {
|
||||
@@ -36,9 +39,9 @@ void FakeClock::RemoveObserver(absl::string_view name) {
|
||||
|
||||
void FakeClock::FastForward(absl::Duration duration) {
|
||||
std::vector<std::string> timer_callback_ids;
|
||||
now_ += duration;
|
||||
{
|
||||
absl::MutexLock lock(&mutex_);
|
||||
now_ += duration;
|
||||
for (const auto& observer : observers_) {
|
||||
timer_callback_ids.push_back(observer.first);
|
||||
}
|
||||
|
||||
@@ -45,8 +45,8 @@ class FakeClock : public Clock {
|
||||
int GetObserversCount() ABSL_LOCKS_EXCLUDED(mutex_);
|
||||
|
||||
private:
|
||||
absl::Time now_;
|
||||
mutable absl::Mutex mutex_;
|
||||
absl::Time now_ ABSL_GUARDED_BY(mutex_);
|
||||
absl::flat_hash_map<std::string, std::function<void()>> observers_
|
||||
ABSL_GUARDED_BY(mutex_);
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user