1 /* Copyright (c) 2003, Roger Dingledine
2 * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
3 * Copyright (c) 2007-2021, The Tor Project, Inc. */
4 /* See LICENSE for licensing information */
8 * \brief Cache the last result of time(), for performance and testing.
12 #include "lib/subsys/subsys.h"
13 #include "lib/wallclock/approx_time.h"
14 #include "lib/wallclock/wallclock_sys.h"
21 /** Cached estimate of the current time. Updated around once per second;
22 * may be a few seconds off if we are really busy. This is a hack to avoid
23 * calling time(NULL) (which not everybody has optimized) on critical paths.
25 static time_t cached_approx_time
= 0;
27 /** Return a cached estimate of the current time from when
28 * update_approx_time() was last called. This is a hack to avoid calling
29 * time(NULL) on critical paths: please do not even think of calling it
34 return cached_approx_time
;
37 /** Update the cached estimate of the current time. This function SHOULD be
38 * called once per second, and MUST be called before the first call to
41 update_approx_time(time_t now
)
43 cached_approx_time
= now
;
45 #endif /* !defined(TIME_IS_FAST) */
48 * Initialize the "wallclock" subsystem by setting the current cached time.
51 subsys_wallclock_initialize(void)
53 update_approx_time(time(NULL
));
58 * Subsystem function table describing the "wallclock" subsystem.
60 const subsys_fns_t sys_wallclock
= {
62 SUBSYS_DECLARE_LOCATION(),
64 /* Approximate time is a diagnostic feature, we want it to init right after
65 * low-level error handling. */
67 .initialize
= subsys_wallclock_initialize
,