approx_time.c 1.1 KB

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