blob: e639eade7c759b577d2c3c63de09395abf8c2586 [file] [log] [blame] [edit]
// This file is a copy of google3/utils/clock_interface.h
// Will not be used in G3 build, only in gBMC environment.
// TODO: (b/482207765) - Remove this file once absl version in gBMC environment
// includes this file.
#ifndef ABSL_TIME_CLOCK_INTERFACE_H_
#define ABSL_TIME_CLOCK_INTERFACE_H_
#include "absl/synchronization/mutex.h"
#include "absl/time/time.h"
namespace absl {
// An abstract interface representing a Clock, which is an object that can
// tell you the current time, sleep, and wait for a condition variable.
//
// This interface allows decoupling code that uses time from the code that
// creates a point in time. You can use this to your advantage by injecting
// Clocks into interfaces rather than having implementations call absl::Now()
// directly.
//
// The Clock::GetRealClock() function returns a reference to the global realtime
// clock.
//
// Example:
//
// bool IsWeekend(Clock& clock) {
// absl::Time now = clock.TimeNow();
// // ... code to check if 'now' is a weekend.
// }
//
// // Production code.
// IsWeekend(Clock::GetRealClock());
//
// // Test code:
// MyTestClock test_clock(SATURDAY);
// IsWeekend(test_clock);
//
class Clock {
public:
// Returns a reference to the global realtime clock.
// The returned clock is thread-safe.
static Clock& GetRealClock();
virtual ~Clock();
// Returns the current time.
virtual absl::Time TimeNow() = 0;
// Sleeps for the specified duration.
virtual void Sleep(absl::Duration d) = 0;
// Sleeps until the specified time.
virtual void SleepUntil(absl::Time wakeup_time) = 0;
// Returns when cond is true or the deadline has passed. Returns true iff
// cond holds when returning.
//
// Requires *mu to be held at least in shared mode. It will be held when
// evaluating cond, and upon return, but it may be released and reacquired
// in the meantime.
//
// This method is similar to mu->AwaitWithDeadline() except that the
// latter only works with real-time deadlines. This call works properly
// with simulated time if invoked on a simulated clock.
virtual bool AwaitWithDeadline(absl::Mutex* mu,
const absl::Condition& cond,
absl::Time deadline) = 0;
};
} // namespace absl
#endif // ABSL_TIME_CLOCK_INTERFACE_H_