From 9bd967affaafb10e8262253bc746dac234f39443 Mon Sep 17 00:00:00 2001 From: Matthias Schiffer Date: Tue, 10 Nov 2015 18:26:37 +0100 Subject: Use heap-based priority queue to schedule handshakes instead of a linked list --- src/peer.c | 21 ++++++--------------- 1 file changed, 6 insertions(+), 15 deletions(-) (limited to 'src/peer.c') diff --git a/src/peer.c b/src/peer.c index 67ce50d..87df22d 100644 --- a/src/peer.c +++ b/src/peer.c @@ -265,17 +265,8 @@ void fastd_peer_reset_socket(fastd_peer_t *peer) { void fastd_peer_schedule_handshake(fastd_peer_t *peer, int delay) { fastd_peer_unschedule_handshake(peer); - peer->next_handshake = ctx.now + delay; - - fastd_dlist_head_t *list; - for (list = &ctx.handshake_queue; list->next; list = list->next) { - fastd_peer_t *entry = container_of(list->next, fastd_peer_t, handshake_entry); - - if (entry->next_handshake > peer->next_handshake) - break; - } - - fastd_dlist_insert(list, &peer->handshake_entry); + peer->handshake_entry.value = ctx.now + delay; + fastd_pqueue_insert(&ctx.handshake_queue, &peer->handshake_entry); } /** Checks if the peer group \e group1 lies in \e group2 */ @@ -829,13 +820,13 @@ static void send_handshake(fastd_peer_t *peer, fastd_remote_t *next_remote) { /** Sends a handshake to one peer, if a scheduled handshake is due */ void fastd_peer_handle_handshake_queue(void) { - if (!ctx.handshake_queue.next) + if (!ctx.handshake_queue) return; - - fastd_peer_t *peer = container_of(ctx.handshake_queue.next, fastd_peer_t, handshake_entry); - if (!fastd_timed_out(peer->next_handshake)) + if (!fastd_timed_out(ctx.handshake_queue->value)) return; + fastd_peer_t *peer = container_of(ctx.handshake_queue, fastd_peer_t, handshake_entry); + fastd_peer_schedule_handshake_default(peer); if (!fastd_peer_may_connect(peer)) { -- cgit v1.2.3