tor  0.4.2.0-alpha-dev
Macros | Functions | Variables
periodic.c File Reference
#include "core/or/or.h"
#include "lib/evloop/compat_libevent.h"
#include "app/config/config.h"
#include "core/mainloop/mainloop.h"
#include "core/mainloop/periodic.h"

Go to the source code of this file.

Macros

#define LONGEST_TIMER_PERIOD   (30 * 86400)
 

Functions

static void periodic_event_set_interval (periodic_event_item_t *event, time_t next_interval)
 
static void periodic_event_dispatch (mainloop_event_t *ev, void *data)
 
void periodic_event_reschedule (periodic_event_item_t *event)
 
void periodic_event_connect (periodic_event_item_t *event)
 
void periodic_event_launch (periodic_event_item_t *event)
 
static void periodic_event_disconnect (periodic_event_item_t *event)
 
void periodic_event_enable (periodic_event_item_t *event)
 
void periodic_event_disable (periodic_event_item_t *event)
 
void periodic_event_schedule_and_disable (periodic_event_item_t *event)
 
void periodic_events_register (periodic_event_item_t *item)
 
void periodic_events_connect_all (void)
 
void periodic_events_reset_all (void)
 
periodic_event_item_tperiodic_events_find (const char *name)
 
void periodic_events_rescan_by_roles (int roles, bool net_disabled)
 
void periodic_events_disconnect_all (void)
 
int safe_timer_diff (time_t now, time_t next)
 

Variables

static const int MAX_INTERVAL = 10 * 365 * 86400
 
static smartlist_tthe_periodic_events = NULL
 

Detailed Description

Generic backend for handling periodic events.

The events in this module are used to track items that need to fire once every N seconds, possibly picking a new interval each time that they fire. See periodic_events[] in mainloop.c for examples.

This module manages a global list of periodic_event_item_t objects, each corresponding to a single event. To register an event, pass it to periodic_events_register() when initializing your subsystem.

Registering an event makes the periodic event subsystem know about it, but doesn't cause the event to get created immediately. Before the event can be started, periodic_event_connect_all() must be called by mainloop.c to connect all the events to Libevent.

We expect that periodic_event_item_t objects will be statically allocated; we set them up and tear them down here, but we don't take ownership of them.

Definition in file periodic.c.

Function Documentation

◆ periodic_event_connect()

void periodic_event_connect ( periodic_event_item_t event)

Connects a periodic event to the Libevent backend. Does not launch the event immediately.

Definition at line 118 of file periodic.c.

References periodic_event_item_t::ev.

Referenced by periodic_events_connect_all().

◆ periodic_event_disable()

void periodic_event_disable ( periodic_event_item_t event)

Disable the given event which means the event is destroyed and then the event's enabled flag is unset. This can be called for an event that is already disabled.

Definition at line 181 of file periodic.c.

References periodic_event_item_t::ev, mainloop_event_cancel(), and tor_assert().

Referenced by periodic_event_schedule_and_disable().

◆ periodic_event_disconnect()

static void periodic_event_disconnect ( periodic_event_item_t event)
static

Disconnect and unregister the periodic event in event

Definition at line 152 of file periodic.c.

Referenced by periodic_events_disconnect_all().

◆ periodic_event_dispatch()

static void periodic_event_dispatch ( mainloop_event_t ev,
void *  data 
)
static

Wraps dispatches for periodic events, data will be a pointer to the event that needs to be called

Definition at line 62 of file periodic.c.

References tor_assert(), and update_current_time().

◆ periodic_event_enable()

void periodic_event_enable ( periodic_event_item_t event)

Enable the given event by setting its "enabled" flag and scheduling it to run immediately in the event loop. This can be called for an event that is already enabled.

Definition at line 164 of file periodic.c.

References periodic_event_item_t::ev, mainloop_event_activate(), and tor_assert().

◆ periodic_event_launch()

void periodic_event_launch ( periodic_event_item_t event)

Handles initial dispatch for periodic events. It should happen 1 second after the events are created to mimic behaviour before #3199's refactor

Definition at line 133 of file periodic.c.

References periodic_event_item_t::ev.

◆ periodic_event_reschedule()

void periodic_event_reschedule ( periodic_event_item_t event)

Schedules event to run as soon as possible from now.

Definition at line 107 of file periodic.c.

References periodic_event_item_t::ev, and periodic_event_set_interval().

Referenced by periodic_events_reset_all(), and reschedule_descriptor_update_check().

◆ periodic_event_schedule_and_disable()

void periodic_event_schedule_and_disable ( periodic_event_item_t event)

Disable an event, then schedule it to run once. Do nothing if the event was already disabled.

Definition at line 197 of file periodic.c.

References periodic_event_item_t::ev, mainloop_event_activate(), periodic_event_disable(), and tor_assert().

◆ periodic_event_set_interval()

static void periodic_event_set_interval ( periodic_event_item_t event,
time_t  next_interval 
)
static

Set the event event to run in next_interval seconds from now.

Definition at line 49 of file periodic.c.

References periodic_event_item_t::ev, mainloop_event_schedule(), MAX_INTERVAL, and tor_assert().

Referenced by periodic_event_reschedule().

◆ periodic_events_connect_all()

void periodic_events_connect_all ( void  )

Make all registered periodic events connect to the libevent backend.

Definition at line 230 of file periodic.c.

References periodic_event_connect(), SMARTLIST_FOREACH_BEGIN, and the_periodic_events.

Referenced by do_main_loop().

◆ periodic_events_disconnect_all()

void periodic_events_disconnect_all ( void  )

Invoked at shutdown: disconnect and unregister all periodic events.

Does not free the periodic_event_item_t object themselves, because we do not own them.

Definition at line 327 of file periodic.c.

References periodic_event_disconnect(), SMARTLIST_FOREACH_BEGIN, and the_periodic_events.

◆ periodic_events_find()

periodic_event_item_t* periodic_events_find ( const char *  name)

Return the registered periodic event whose name is name. Return NULL if no such event is found.

Definition at line 268 of file periodic.c.

◆ periodic_events_register()

void periodic_events_register ( periodic_event_item_t item)

Add item to the list of periodic events.

Note that item should be statically allocated: we do not take ownership of it.

Definition at line 215 of file periodic.c.

References the_periodic_events.

◆ periodic_events_rescan_by_roles()

void periodic_events_rescan_by_roles ( int  roles,
bool  net_disabled 
)

Start or stop registered periodic events, depending on our current set of roles.

Invoked when our list of roles, or the net_disabled flag has changed.

Definition at line 287 of file periodic.c.

References SMARTLIST_FOREACH_BEGIN, and the_periodic_events.

Referenced by rescan_periodic_events().

◆ periodic_events_reset_all()

void periodic_events_reset_all ( void  )

Reset all the registered periodic events so we'll do all our actions again as if we just started up.

Useful if our clock just moved back a long time from the future, so we don't wait until that future arrives again before acting.

Definition at line 250 of file periodic.c.

References periodic_event_reschedule(), SMARTLIST_FOREACH_BEGIN, and the_periodic_events.

Referenced by reset_all_main_loop_timers().

◆ safe_timer_diff()

int safe_timer_diff ( time_t  now,
time_t  next 
)

Helper: Return the number of seconds between now and next, clipped to the range [1 second, LONGEST_TIMER_PERIOD].

We use this to answer the question, "how many seconds is it from now until next" in periodic timer callbacks. Don't use it for other purposes

Definition at line 347 of file periodic.c.

References tor_assert().

Variable Documentation

◆ MAX_INTERVAL

const int MAX_INTERVAL = 10 * 365 * 86400
static

We disable any interval greater than this number of seconds, on the grounds that it is probably an absolute time mistakenly passed in as a relative time.

Definition at line 38 of file periodic.c.

Referenced by periodic_event_set_interval().

◆ the_periodic_events

smartlist_t* the_periodic_events = NULL
static

Global list of periodic events that have been registered with periodic_event_register.

Definition at line 44 of file periodic.c.

Referenced by periodic_events_connect_all(), periodic_events_disconnect_all(), periodic_events_register(), periodic_events_rescan_by_roles(), and periodic_events_reset_all().