2005-09-15 09:58:31 -04:00
|
|
|
/* Timers. */
|
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include "config.h"
|
|
|
|
#endif
|
|
|
|
|
2017-11-12 07:41:31 -05:00
|
|
|
#include <errno.h>
|
|
|
|
|
2017-11-17 10:34:03 -05:00
|
|
|
#if defined(HAVE_LIBEV) && !defined(OPENVMS) && !defined(DOS)
|
|
|
|
#ifdef HAVE_LIBEV_EVENT_H
|
|
|
|
#include <libev/event.h>
|
|
|
|
#elif defined(HAVE_EVENT_H)
|
|
|
|
#include <event.h>
|
|
|
|
#endif
|
|
|
|
#define USE_LIBEVENT
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if (defined(HAVE_EVENT_H) || defined(HAVE_EV_EVENT_H) || defined(HAVE_LIBEV_EVENT_H)) && defined(HAVE_LIBEVENT) && !defined(OPENVMS) && !defined(DOS)
|
2017-11-12 07:41:31 -05:00
|
|
|
#if defined(HAVE_EVENT_H)
|
|
|
|
#include <event.h>
|
|
|
|
#elif defined(HAVE_EV_EVENT_H)
|
|
|
|
#include <ev-event.h>
|
|
|
|
#endif
|
|
|
|
#define USE_LIBEVENT
|
|
|
|
#endif
|
|
|
|
|
2005-09-15 09:58:31 -04:00
|
|
|
#include "elinks.h"
|
|
|
|
|
|
|
|
#include "main/select.h"
|
|
|
|
#include "main/timer.h"
|
|
|
|
#include "util/error.h"
|
|
|
|
#include "util/lists.h"
|
|
|
|
#include "util/memory.h"
|
|
|
|
#include "util/time.h"
|
|
|
|
|
|
|
|
|
|
|
|
struct timer {
|
|
|
|
LIST_HEAD(struct timer);
|
|
|
|
|
|
|
|
timeval_T interval;
|
|
|
|
void (*func)(void *);
|
|
|
|
void *data;
|
|
|
|
};
|
|
|
|
|
2006-12-02 13:53:42 -05:00
|
|
|
/* @timers.next points to the timer with the smallest interval,
|
|
|
|
* @timers.next->next to the second smallest, and so on. */
|
2007-07-26 15:39:08 -04:00
|
|
|
static INIT_LIST_OF(struct timer, timers);
|
2005-09-15 09:58:31 -04:00
|
|
|
|
|
|
|
int
|
2005-11-24 09:38:47 -05:00
|
|
|
get_timers_count(void)
|
2005-09-15 09:58:31 -04:00
|
|
|
{
|
|
|
|
return list_size(&timers);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2017-11-16 12:23:47 -05:00
|
|
|
|
|
|
|
#ifdef HAVE_EVENT_BASE_SET
|
|
|
|
extern struct event_base *event_base;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef USE_LIBEVENT
|
|
|
|
extern int event_enabled;
|
|
|
|
#ifndef HAVE_EVENT_GET_STRUCT_EVENT_SIZE
|
|
|
|
#define sizeof_struct_event sizeof(struct event)
|
|
|
|
#else
|
|
|
|
#define sizeof_struct_event (event_get_struct_event_size())
|
|
|
|
#endif
|
|
|
|
|
|
|
|
static void
|
|
|
|
timer_callback(int h, short ev, void *data)
|
|
|
|
{
|
|
|
|
struct timer *tm = data;
|
|
|
|
tm->func(tm->data);
|
|
|
|
kill_timer(&tm);
|
|
|
|
check_bottom_halves();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static inline
|
|
|
|
struct event *timer_event(struct timer *tm)
|
|
|
|
{
|
2021-01-02 10:20:27 -05:00
|
|
|
return (struct event *)((char *)tm - sizeof_struct_event);
|
2017-11-16 12:23:47 -05:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2005-09-15 09:58:31 -04:00
|
|
|
void
|
|
|
|
check_timers(timeval_T *last_time)
|
|
|
|
{
|
|
|
|
timeval_T now;
|
|
|
|
timeval_T interval;
|
2006-12-02 13:53:42 -05:00
|
|
|
struct timer *timer;
|
2005-09-15 09:58:31 -04:00
|
|
|
|
|
|
|
timeval_now(&now);
|
|
|
|
timeval_sub(&interval, last_time, &now);
|
|
|
|
|
|
|
|
foreach (timer, timers) {
|
|
|
|
timeval_sub_interval(&timer->interval, &interval);
|
|
|
|
}
|
|
|
|
|
2006-12-02 13:53:42 -05:00
|
|
|
while (!list_empty(timers)) {
|
|
|
|
timer = timers.next;
|
|
|
|
|
2005-09-15 09:58:31 -04:00
|
|
|
if (timeval_is_positive(&timer->interval))
|
|
|
|
break;
|
|
|
|
|
|
|
|
del_from_list(timer);
|
2006-12-02 13:53:42 -05:00
|
|
|
/* At this point, *@timer is to be considered invalid
|
|
|
|
* outside timers.c; if anything e.g. passes it to
|
|
|
|
* @kill_timer, that's a bug. However, @timer->func
|
|
|
|
* and @check_bottom_halves can still call @kill_timer
|
|
|
|
* on other timers, so this loop must be careful not to
|
|
|
|
* keep pointers to them. (bug 868) */
|
2005-09-15 09:58:31 -04:00
|
|
|
timer->func(timer->data);
|
2017-11-16 12:23:47 -05:00
|
|
|
#ifdef USE_LIBEVENT
|
|
|
|
mem_free(timer_event(timer));
|
|
|
|
#else
|
2005-09-15 09:58:31 -04:00
|
|
|
mem_free(timer);
|
2017-11-16 12:23:47 -05:00
|
|
|
#endif
|
2005-09-15 09:58:31 -04:00
|
|
|
check_bottom_halves();
|
|
|
|
}
|
|
|
|
|
|
|
|
timeval_copy(last_time, &now);
|
|
|
|
}
|
|
|
|
|
2019-11-16 05:35:18 -05:00
|
|
|
#ifdef USE_LIBEVENT
|
2017-11-12 07:41:31 -05:00
|
|
|
static void
|
|
|
|
set_event_for_timer(timer_id_T tm)
|
|
|
|
{
|
|
|
|
struct timeval tv;
|
|
|
|
struct event *ev = timer_event(tm);
|
|
|
|
timeout_set(ev, timer_callback, tm);
|
|
|
|
#ifdef HAVE_EVENT_BASE_SET
|
|
|
|
if (event_base_set(event_base, ev) == -1)
|
|
|
|
elinks_internal("ERROR: event_base_set failed: %s", strerror(errno));
|
|
|
|
#endif
|
|
|
|
tv.tv_sec = tm->interval.sec;
|
|
|
|
tv.tv_usec = tm->interval.usec;
|
|
|
|
#if defined(HAVE_LIBEV)
|
|
|
|
if (!tm->interval.usec && ev_version_major() < 4) {
|
|
|
|
/* libev bug */
|
|
|
|
tv.tv_usec = 1;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
if (timeout_add(ev, &tv) == -1)
|
|
|
|
elinks_internal("ERROR: timeout_add failed: %s", strerror(errno));
|
|
|
|
}
|
2019-11-16 05:35:18 -05:00
|
|
|
#endif
|
2017-11-12 07:41:31 -05:00
|
|
|
|
2006-12-02 11:35:03 -05:00
|
|
|
/* Install a timer that calls @func(@data) after @delay milliseconds.
|
|
|
|
* Store to *@id either the ID of the new timer, or TIMER_ID_UNDEF if
|
|
|
|
* the timer cannot be installed. (This function ignores the previous
|
|
|
|
* value of *@id in any case.)
|
|
|
|
*
|
|
|
|
* When @func is called, the timer ID has become invalid. @func
|
|
|
|
* should erase the expired timer ID from all variables, so that
|
|
|
|
* there's no chance it will be given to @kill_timer later. */
|
2005-09-15 09:58:31 -04:00
|
|
|
void
|
|
|
|
install_timer(timer_id_T *id, milliseconds_T delay, void (*func)(void *), void *data)
|
|
|
|
{
|
|
|
|
struct timer *new_timer, *timer;
|
|
|
|
|
|
|
|
assert(id && delay > 0);
|
|
|
|
|
2017-11-12 07:41:31 -05:00
|
|
|
#ifdef USE_LIBEVENT
|
2021-01-02 10:20:27 -05:00
|
|
|
char *q = mem_alloc(sizeof_struct_event + sizeof(struct timer));
|
2017-11-16 12:23:47 -05:00
|
|
|
new_timer = (struct timer *)(q + sizeof_struct_event);
|
2017-11-12 07:41:31 -05:00
|
|
|
#else
|
2005-09-15 09:58:31 -04:00
|
|
|
new_timer = mem_alloc(sizeof(*new_timer));
|
2017-11-12 07:41:31 -05:00
|
|
|
#endif
|
2005-09-15 09:58:31 -04:00
|
|
|
*id = (timer_id_T) new_timer; /* TIMER_ID_UNDEF is NULL */
|
|
|
|
if (!new_timer) return;
|
|
|
|
|
|
|
|
timeval_from_milliseconds(&new_timer->interval, delay);
|
|
|
|
new_timer->func = func;
|
|
|
|
new_timer->data = data;
|
2017-11-12 07:41:31 -05:00
|
|
|
#ifdef USE_LIBEVENT
|
|
|
|
if (event_enabled) {
|
|
|
|
set_event_for_timer(new_timer);
|
|
|
|
add_to_list(timers, new_timer);
|
|
|
|
} else
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
foreach (timer, timers) {
|
|
|
|
if (timeval_cmp(&timer->interval, &new_timer->interval) >= 0)
|
|
|
|
break;
|
|
|
|
}
|
2005-09-15 09:58:31 -04:00
|
|
|
|
2017-11-12 07:41:31 -05:00
|
|
|
add_at_pos(timer->prev, new_timer);
|
2005-09-15 09:58:31 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
kill_timer(timer_id_T *id)
|
|
|
|
{
|
|
|
|
struct timer *timer;
|
|
|
|
|
|
|
|
assert(id != NULL);
|
|
|
|
if (*id == TIMER_ID_UNDEF) return;
|
|
|
|
timer = *id;
|
|
|
|
del_from_list(timer);
|
|
|
|
|
2017-11-12 07:41:31 -05:00
|
|
|
#ifdef USE_LIBEVENT
|
2017-11-16 12:23:47 -05:00
|
|
|
if (event_enabled) {
|
2017-11-12 07:41:31 -05:00
|
|
|
timeout_del(timer_event(timer));
|
2017-11-16 12:23:47 -05:00
|
|
|
}
|
2017-11-12 07:41:31 -05:00
|
|
|
mem_free(timer_event(timer));
|
|
|
|
#else
|
|
|
|
mem_free(timer);
|
|
|
|
#endif
|
2005-09-15 09:58:31 -04:00
|
|
|
*id = TIMER_ID_UNDEF;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
get_next_timer_time(timeval_T *t)
|
|
|
|
{
|
|
|
|
if (!list_empty(timers)) {
|
|
|
|
timeval_copy(t, &((struct timer *) &timers)->next->interval);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2017-11-12 07:41:31 -05:00
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
set_events_for_timer(void)
|
|
|
|
{
|
|
|
|
#ifdef USE_LIBEVENT
|
|
|
|
timer_id_T tm;
|
|
|
|
|
|
|
|
foreach(tm, timers)
|
|
|
|
set_event_for_timer(tm);
|
|
|
|
#endif
|
|
|
|
}
|