From 16e2b0213b54991d9f493d4c0ee7efca18436427 Mon Sep 17 00:00:00 2001 From: Matthias Schiffer Date: Wed, 18 Nov 2015 01:23:21 +0100 Subject: Clean up peer task handling --- src/peer.c | 18 +++++++++++++----- src/poll.c | 4 ++-- src/task.c | 9 ++++----- src/task.h | 12 +++++++++--- 4 files changed, 28 insertions(+), 15 deletions(-) diff --git a/src/peer.c b/src/peer.c index 9f26803..22339b4 100644 --- a/src/peer.c +++ b/src/peer.c @@ -364,13 +364,21 @@ void fastd_peer_handle_resolve(fastd_peer_t *peer, fastd_remote_t *remote, size_ init_handshake(peer); } -/** Schedules the peer maintenance task (or removes the schduled task if there's nothing to do) */ -void schedule_peer_task(fastd_peer_t *peer) { - fastd_task_unschedule(&peer->task); - +/** Schedules the peer maintenance task (or removes the scheduled task if there's nothing to do) */ +static void schedule_peer_task(fastd_peer_t *peer) { fastd_timeout_t timeout = fastd_timeout_min(peer->reset_timeout, peer->keepalive_timeout); - if (timeout != fastd_timeout_inv) + if (timeout == fastd_timeout_inv) { + pr_debug2("Removing scheduled task for %P", peer); + fastd_task_unschedule(&peer->task); + } + else if (fastd_task_timeout(&peer->task) > timeout) { + pr_debug2("Replacing scheduled task for %P", peer); + fastd_task_unschedule(&peer->task); fastd_task_schedule(&peer->task, TASK_TYPE_PEER, timeout); + } + else { + pr_debug2("Keeping scheduled task for %P", peer); + } } /** Initializes a peer */ diff --git a/src/poll.c b/src/poll.c index fed7001..02ee00b 100644 --- a/src/poll.c +++ b/src/poll.c @@ -53,8 +53,8 @@ /** Returns the time to the next task or -1 */ static inline int task_timeout(void) { - fastd_timeout_t timeout; - if (!fastd_task_timeout(&timeout)) + fastd_timeout_t timeout = fastd_task_queue_timeout(); + if (timeout == fastd_timeout_inv) return -1; int diff_msec = timeout - ctx.now; diff --git a/src/task.c b/src/task.c index e1dc77d..079fc4a 100644 --- a/src/task.c +++ b/src/task.c @@ -76,11 +76,10 @@ void fastd_task_reschedule(fastd_task_t *task, fastd_timeout_t timeout) { fastd_pqueue_insert(&ctx.task_queue, &task->entry); } -/** Gets the timeout of the next task (if any) */ -bool fastd_task_timeout(fastd_timeout_t *timeout) { +/** Gets the timeout of the next task in the task queue */ +fastd_timeout_t fastd_task_queue_timeout(void) { if (!ctx.task_queue) - return false; + return fastd_timeout_inv; - *timeout = ctx.task_queue->value; - return true; + return ctx.task_queue->value; } diff --git a/src/task.h b/src/task.h index 91bacc0..667386a 100644 --- a/src/task.h +++ b/src/task.h @@ -44,6 +44,7 @@ struct fastd_task { void fastd_task_handle(void); void fastd_task_reschedule(fastd_task_t *task, fastd_timeout_t timeout); +fastd_timeout_t fastd_task_queue_timeout(void); /** Checks if the given task is currently scheduled */ @@ -51,6 +52,14 @@ static inline bool fastd_task_scheduled(fastd_task_t *task) { return fastd_pqueue_linked(&task->entry); } +/** Gets the timeout of a task */ +static inline fastd_timeout_t fastd_task_timeout(fastd_task_t *task) { + if (!fastd_task_scheduled(task)) + return fastd_timeout_inv; + + return task->entry.value; +} + /** Removes a task from the queue */ static inline void fastd_task_unschedule(fastd_task_t *task) { fastd_pqueue_remove(&task->entry); @@ -66,6 +75,3 @@ static inline void fastd_task_schedule(fastd_task_t *task, fastd_task_type_t typ task->type = type; fastd_task_reschedule(task, timeout); } - - -bool fastd_task_timeout(fastd_timeout_t *timeout); -- cgit v1.2.3