Switch wakeup to standard list.
This commit is contained in:
@@ -10,6 +10,7 @@
|
|||||||
#include <time.h>
|
#include <time.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
|
||||||
|
#include "list.h"
|
||||||
#include "peer.h"
|
#include "peer.h"
|
||||||
#include "rand.h"
|
#include "rand.h"
|
||||||
|
|
||||||
@@ -19,10 +20,10 @@ struct wakeup_entry {
|
|||||||
int fd;
|
int fd;
|
||||||
uint64_t absolute_time_ms;
|
uint64_t absolute_time_ms;
|
||||||
struct peer *peer;
|
struct peer *peer;
|
||||||
struct wakeup_entry *next;
|
struct list_head wakeup_list;
|
||||||
};
|
};
|
||||||
|
|
||||||
static struct wakeup_entry *head = NULL;
|
static struct list_head wakeup_head = LIST_HEAD_INIT(wakeup_head);
|
||||||
|
|
||||||
static uint64_t wakeup_get_time_ms() {
|
static uint64_t wakeup_get_time_ms() {
|
||||||
struct timespec tp;
|
struct timespec tp;
|
||||||
@@ -38,20 +39,20 @@ void wakeup_init() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void wakeup_cleanup() {
|
void wakeup_cleanup() {
|
||||||
while (head) {
|
struct wakeup_entry *iter, *next;
|
||||||
struct wakeup_entry *next = head->next;
|
list_for_each_entry_safe(iter, next, &wakeup_head, wakeup_list) {
|
||||||
free(head);
|
free(iter);
|
||||||
head = next;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
int wakeup_get_delay() {
|
int wakeup_get_delay() {
|
||||||
if (!head) {
|
if (list_is_empty(&wakeup_head)) {
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
uint64_t now = wakeup_get_time_ms();
|
uint64_t now = wakeup_get_time_ms();
|
||||||
if (head->absolute_time_ms > now) {
|
struct wakeup_entry *next_to_fire = list_entry(wakeup_head.next, struct wakeup_entry, wakeup_list);
|
||||||
uint64_t delta = head->absolute_time_ms - now;
|
if (next_to_fire->absolute_time_ms > now) {
|
||||||
|
uint64_t delta = next_to_fire->absolute_time_ms - now;
|
||||||
assert(delta < INT_MAX);
|
assert(delta < INT_MAX);
|
||||||
return (int) delta;
|
return (int) delta;
|
||||||
} else {
|
} else {
|
||||||
@@ -61,11 +62,14 @@ int wakeup_get_delay() {
|
|||||||
|
|
||||||
void wakeup_dispatch() {
|
void wakeup_dispatch() {
|
||||||
uint64_t now = wakeup_get_time_ms();
|
uint64_t now = wakeup_get_time_ms();
|
||||||
while (head && head->absolute_time_ms <= now) {
|
struct wakeup_entry *iter, *next;
|
||||||
peer_call(head->peer);
|
list_for_each_entry_safe(iter, next, &wakeup_head, wakeup_list) {
|
||||||
struct wakeup_entry *next = head->next;
|
if (iter->absolute_time_ms > now) {
|
||||||
free(head);
|
break;
|
||||||
head = next;
|
}
|
||||||
|
peer_call(iter->peer);
|
||||||
|
list_del(&iter->wakeup_list);
|
||||||
|
free(iter);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -74,22 +78,14 @@ void wakeup_add(struct peer *peer, uint32_t delay_ms) {
|
|||||||
entry->absolute_time_ms = wakeup_get_time_ms() + delay_ms;
|
entry->absolute_time_ms = wakeup_get_time_ms() + delay_ms;
|
||||||
entry->peer = peer;
|
entry->peer = peer;
|
||||||
|
|
||||||
struct wakeup_entry *prev = NULL, *iter = head;
|
struct wakeup_entry *iter, *next;
|
||||||
while (iter) {
|
list_for_each_entry_safe(iter, next, &wakeup_head, wakeup_list) {
|
||||||
if (iter->absolute_time_ms > entry->absolute_time_ms) {
|
if (iter->absolute_time_ms > entry->absolute_time_ms) {
|
||||||
break;
|
list_add(&entry->wakeup_list, &iter->wakeup_list);
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
prev = iter;
|
|
||||||
iter = iter->next;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (prev) {
|
|
||||||
entry->next = prev->next;
|
|
||||||
prev->next = entry;
|
|
||||||
} else {
|
|
||||||
entry->next = head;
|
|
||||||
head = entry;
|
|
||||||
}
|
}
|
||||||
|
list_add(&entry->wakeup_list, &wakeup_head);
|
||||||
}
|
}
|
||||||
|
|
||||||
#define RETRY_MIN_MS 2000
|
#define RETRY_MIN_MS 2000
|
||||||
|
|||||||
Reference in New Issue
Block a user