aboutsummaryrefslogtreecommitdiff
path: root/src/core/mainloop/mainloop_sys.c
blob: 884bae1c593cf6dd1faea6d04556dd1ae1d296f0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
/* Copyright (c) 2001 Matej Pfajfar.
 * Copyright (c) 2001-2004, Roger Dingledine.
 * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
 * Copyright (c) 2007-2020, The Tor Project, Inc. */
/* See LICENSE for licensing information */

/**
 * @file mainloop_sys.c
 * @brief Declare the "mainloop" subsystem.
 **/

#include "core/or/or.h"
#include "core/mainloop/mainloop_sys.h"
#include "core/mainloop/mainloop.h"
#include "core/mainloop/mainloop_state_st.h"
#include "core/mainloop/netstatus.h"
#include "lib/conf/conftypes.h"
#include "lib/conf/confdecl.h"

#include "lib/subsys/subsys.h"

static int
subsys_mainloop_initialize(void)
{
  initialize_periodic_events();
  return 0;
}

static void
subsys_mainloop_shutdown(void)
{
  tor_mainloop_free_all();
}

/** Declare a list of state variables for mainloop state. */
#define CONF_CONTEXT TABLE
#include "core/mainloop/mainloop_state.inc"
#undef CONF_CONTEXT

/** Magic number for mainloop state objects */
#define MAINLOOP_STATE_MAGIC 0x59455449

/**
 * Format object for mainloop state.
 **/
static config_format_t mainloop_state_fmt = {
  .size = sizeof(mainloop_state_t),
  .magic = { "mainloop_state",
             MAINLOOP_STATE_MAGIC,
             offsetof(mainloop_state_t, magic)
            },
  .vars = mainloop_state_t_vars,
};

/**
 */
static int
mainloop_set_state(void *arg)
{
  const mainloop_state_t *state = arg;
  tor_assert(state->magic == MAINLOOP_STATE_MAGIC);

  netstatus_load_from_state(state, approx_time());

  return 0;
}

static int
mainloop_flush_state(void *arg)
{
  mainloop_state_t *state = arg;
  tor_assert(state->magic == MAINLOOP_STATE_MAGIC);

  netstatus_flush_to_state(state, approx_time());

  return 0;
}

const struct subsys_fns_t sys_mainloop = {
  .name = "mainloop",
  SUBSYS_DECLARE_LOCATION(),
  .supported = true,
  .level = 5,
  .initialize = subsys_mainloop_initialize,
  .shutdown = subsys_mainloop_shutdown,

  .state_format = &mainloop_state_fmt,
  .set_state = mainloop_set_state,
  .flush_state = mainloop_flush_state,
};