Files
adsb-tools/adsbus/wakeup.c

102 lines
2.4 KiB
C
Raw Normal View History

2016-02-18 09:33:32 -08:00
#include <assert.h>
#include <fcntl.h>
#include <errno.h>
#include <limits.h>
#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>
#include <string.h>
2016-02-20 23:56:40 -08:00
#include <sys/epoll.h>
#include <time.h>
#include <unistd.h>
2016-02-18 09:33:32 -08:00
2016-03-02 19:10:31 -08:00
#include "list.h"
2016-02-22 16:49:43 -08:00
#include "peer.h"
#include "rand.h"
2016-02-18 09:33:32 -08:00
#include "wakeup.h"
struct wakeup_entry {
int fd;
uint64_t absolute_time_ms;
struct peer *peer;
2016-03-02 19:10:31 -08:00
struct list_head wakeup_list;
};
2016-03-02 19:10:31 -08:00
static struct list_head wakeup_head = LIST_HEAD_INIT(wakeup_head);
2016-02-20 23:56:40 -08:00
static uint64_t wakeup_get_time_ms() {
struct timespec tp;
assert(!clock_gettime(CLOCK_MONOTONIC_COARSE, &tp));
#define MS_PER_S UINT64_C(1000)
#define NS_PER_MS UINT64_C(1000000)
assert(tp.tv_sec >= 0);
assert(tp.tv_nsec >= 0);
return ((uint64_t) tp.tv_sec * MS_PER_S) + ((uint64_t) tp.tv_nsec / NS_PER_MS);
}
void wakeup_init() {
}
void wakeup_cleanup() {
2016-03-02 19:10:31 -08:00
struct wakeup_entry *iter, *next;
list_for_each_entry_safe(iter, next, &wakeup_head, wakeup_list) {
free(iter);
2016-02-20 23:56:40 -08:00
}
}
int wakeup_get_delay() {
2016-03-02 19:10:31 -08:00
if (list_is_empty(&wakeup_head)) {
return -1;
}
uint64_t now = wakeup_get_time_ms();
2016-03-02 19:10:31 -08:00
struct wakeup_entry *next_to_fire = list_entry(wakeup_head.next, struct wakeup_entry, wakeup_list);
if (next_to_fire->absolute_time_ms > now) {
uint64_t delta = next_to_fire->absolute_time_ms - now;
assert(delta < INT_MAX);
return (int) delta;
} else {
return 0;
}
2016-02-20 23:56:40 -08:00
}
void wakeup_dispatch() {
uint64_t now = wakeup_get_time_ms();
2016-03-02 19:10:31 -08:00
struct wakeup_entry *iter, *next;
list_for_each_entry_safe(iter, next, &wakeup_head, wakeup_list) {
if (iter->absolute_time_ms > now) {
break;
}
peer_call(iter->peer);
list_del(&iter->wakeup_list);
free(iter);
}
2016-02-18 09:33:32 -08:00
}
void wakeup_add(struct peer *peer, uint32_t delay_ms) {
struct wakeup_entry *entry = malloc(sizeof(*entry));
entry->absolute_time_ms = wakeup_get_time_ms() + delay_ms;
entry->peer = peer;
2016-03-02 19:10:31 -08:00
struct wakeup_entry *iter, *next;
list_for_each_entry_safe(iter, next, &wakeup_head, wakeup_list) {
if (iter->absolute_time_ms > entry->absolute_time_ms) {
2016-03-02 19:10:31 -08:00
list_add(&entry->wakeup_list, &iter->wakeup_list);
return;
}
}
2016-03-02 19:10:31 -08:00
list_add(&entry->wakeup_list, &wakeup_head);
2016-02-18 09:33:32 -08:00
}
#define RETRY_MIN_MS 2000
#define RETRY_MAX_MS 60000
uint32_t wakeup_get_retry_delay_ms(uint32_t attempt) {
uint32_t max_delay = RETRY_MIN_MS * (1 << attempt);
max_delay = max_delay > RETRY_MAX_MS ? RETRY_MAX_MS : max_delay;
uint32_t jitter;
rand_fill(&jitter, sizeof(jitter));
return jitter % max_delay;
}