Merge branch 'fix-changelogs' into 'main'
[tor.git] / src / lib / wallclock / approx_time.c
blob1399b72f19c4db22f8cfe25f74b0e352e3c62f50
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 */
6 /**
7 * \file approx_time.c
8 * \brief Cache the last result of time(), for performance and testing.
9 **/
11 #include "orconfig.h"
12 #include "lib/subsys/subsys.h"
13 #include "lib/wallclock/approx_time.h"
14 #include "lib/wallclock/wallclock_sys.h"
16 /* =====
17 * Cached time
18 * ===== */
20 #ifndef TIME_IS_FAST
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
30 * anywhere else. */
31 time_t
32 approx_time(void)
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
39 * get_approx_time. */
40 void
41 update_approx_time(time_t now)
43 cached_approx_time = now;
45 #endif /* !defined(TIME_IS_FAST) */
47 /**
48 * Initialize the "wallclock" subsystem by setting the current cached time.
49 **/
50 static int
51 subsys_wallclock_initialize(void)
53 update_approx_time(time(NULL));
54 return 0;
57 /**
58 * Subsystem function table describing the "wallclock" subsystem.
59 **/
60 const subsys_fns_t sys_wallclock = {
61 .name = "wallclock",
62 SUBSYS_DECLARE_LOCATION(),
63 .supported = true,
64 /* Approximate time is a diagnostic feature, we want it to init right after
65 * low-level error handling. */
66 .level = -98,
67 .initialize = subsys_wallclock_initialize,