From 0d43d269ede949fac1e4b5bb125514a240ec3efe Mon Sep 17 00:00:00 2001 From: Kevin O'Connor Date: Tue, 29 Nov 2016 16:22:54 -0500 Subject: serialhdl: Fully deallocate serialqueue on disconnect Signed-off-by: Kevin O'Connor --- klippy/serialqueue.c | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) (limited to 'klippy/serialqueue.c') diff --git a/klippy/serialqueue.c b/klippy/serialqueue.c index 23ed6858..6c1b9d63 100644 --- a/klippy/serialqueue.c +++ b/klippy/serialqueue.c @@ -438,6 +438,18 @@ debug_queue_add(struct list_head *root, struct queue_message *qm) message_free(old); } +// Free all the messages on a queue +static void +queue_free(struct list_head *root) +{ + while (!list_empty(root)) { + struct queue_message *qm = list_first_entry( + root, struct queue_message, node); + list_del(&qm->node); + message_free(qm); + } +} + // Wake up the receiver thread if it is waiting static void check_wake_receive(struct serialqueue *sq) @@ -847,6 +859,28 @@ serialqueue_exit(struct serialqueue *sq) report_errno("pthread_join", ret); } +// Free all resources associated with a serialqueue +void +serialqueue_free(struct serialqueue *sq) +{ + if (!pollreactor_is_exit(&sq->pr)) + serialqueue_exit(sq); + pthread_mutex_lock(&sq->lock); + queue_free(&sq->sent_queue); + queue_free(&sq->receive_queue); + queue_free(&sq->old_sent); + queue_free(&sq->old_receive); + while (!list_empty(&sq->pending_queues)) { + struct command_queue *cq = list_first_entry( + &sq->pending_queues, struct command_queue, node); + list_del(&cq->node); + queue_free(&cq->ready_queue); + queue_free(&cq->stalled_queue); + } + pthread_mutex_unlock(&sq->lock); + free(sq); +} + // Allocate a 'struct command_queue' struct command_queue * serialqueue_alloc_commandqueue(void) -- cgit v1.2.3-70-g09d2