approx_time.c 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. /* Copyright (c) 2003, Roger Dingledine
  2. * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
  3. * Copyright (c) 2007-2019, The Tor Project, Inc. */
  4. /* See LICENSE for licensing information */
  5. /**
  6. * \file approx_time.c
  7. * \brief Cache the last result of time(), for performance and testing.
  8. **/
  9. #include "orconfig.h"
  10. #include "lib/wallclock/approx_time.h"
  11. /* =====
  12. * Cached time
  13. * ===== */
  14. #ifndef TIME_IS_FAST
  15. /** Cached estimate of the current time. Updated around once per second;
  16. * may be a few seconds off if we are really busy. This is a hack to avoid
  17. * calling time(NULL) (which not everybody has optimized) on critical paths.
  18. */
  19. static time_t cached_approx_time = 0;
  20. /** Return a cached estimate of the current time from when
  21. * update_approx_time() was last called. This is a hack to avoid calling
  22. * time(NULL) on critical paths: please do not even think of calling it
  23. * anywhere else. */
  24. time_t
  25. approx_time(void)
  26. {
  27. return cached_approx_time;
  28. }
  29. /** Update the cached estimate of the current time. This function SHOULD be
  30. * called once per second, and MUST be called before the first call to
  31. * get_approx_time. */
  32. void
  33. update_approx_time(time_t now)
  34. {
  35. cached_approx_time = now;
  36. }
  37. #endif /* !defined(TIME_IS_FAST) */