Tor 0.4.9.0-alpha-dev
approx_time.c
Go to the documentation of this file.
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 */
5
6/**
7 * \file approx_time.c
8 * \brief Cache the last result of time(), for performance and testing.
9 **/
10
11#include "orconfig.h"
12#include "lib/subsys/subsys.h"
15
16/* =====
17 * Cached time
18 * ===== */
19
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.
24 */
25static time_t cached_approx_time = 0;
26
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. */
31time_t
33{
34 return cached_approx_time;
35}
36
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. */
40void
42{
44}
45#endif /* !defined(TIME_IS_FAST) */
46
47/**
48 * Initialize the "wallclock" subsystem by setting the current cached time.
49 **/
50static int
52{
53 update_approx_time(time(NULL));
54 return 0;
55}
56
57/**
58 * Subsystem function table describing the "wallclock" subsystem.
59 **/
61 .name = "wallclock",
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,
68};
static int subsys_wallclock_initialize(void)
Definition: approx_time.c:51
void update_approx_time(time_t now)
Definition: approx_time.c:41
const subsys_fns_t sys_wallclock
Definition: approx_time.c:60
static time_t cached_approx_time
Definition: approx_time.c:25
time_t approx_time(void)
Definition: approx_time.c:32
Header for approx_time.c.
const char * name
Definition: subsys.h:43
Types used to declare a subsystem.
#define SUBSYS_DECLARE_LOCATION()
Definition: subsys.h:211
Declare subsystem object for the wallclock module.