From 90febfc6c97098fe3efb2aa1b2450c1f2c2f8694 Mon Sep 17 00:00:00 2001 From: gichan Date: Wed, 4 Jan 2023 14:45:48 +0900 Subject: [PATCH] Change queue of the sending thread - Release send queue after the send thread is finished. - Add new function to clear the data in the queue. Signed-off-by: Jaeyun Signed-off-by: gichan --- .../nnstreamer-edge-internal.c | 6 ++--- .../nnstreamer-edge-queue.c | 24 +++++++++++++++++++ .../nnstreamer-edge-queue.h | 8 +++++++ 3 files changed, 35 insertions(+), 3 deletions(-) diff --git a/src/libnnstreamer-edge/nnstreamer-edge-internal.c b/src/libnnstreamer-edge/nnstreamer-edge-internal.c index 31fd8fc..8eef0dc 100644 --- a/src/libnnstreamer-edge/nnstreamer-edge-internal.c +++ b/src/libnnstreamer-edge/nnstreamer-edge-internal.c @@ -1389,14 +1389,14 @@ nns_edge_release_handle (nns_edge_h edge_h) eh->user_data = NULL; eh->broker_h = NULL; - nns_edge_queue_destroy (eh->send_queue); - eh->send_queue = NULL; - + nns_edge_queue_clear (eh->send_queue); if (eh->send_thread) { eh->sending = false; pthread_join (eh->send_thread, NULL); eh->send_thread = 0; } + nns_edge_queue_destroy (eh->send_queue); + eh->send_queue = NULL; if (eh->listener_thread) { eh->listening = false; diff --git a/src/libnnstreamer-edge/nnstreamer-edge-queue.c b/src/libnnstreamer-edge/nnstreamer-edge-queue.c index 90dc8d0..8f978e3 100644 --- a/src/libnnstreamer-edge/nnstreamer-edge-queue.c +++ b/src/libnnstreamer-edge/nnstreamer-edge-queue.c @@ -324,3 +324,27 @@ nns_edge_queue_wait_pop (nns_edge_queue_h handle, unsigned int timeout, return (popped && *data != NULL); } + +/** + * @brief Clear all data in the queue. + * @note When this function is called, nns_edge_queue_wait_pop will stop the waiting. + */ +bool +nns_edge_queue_clear (nns_edge_queue_h handle) +{ + nns_edge_queue_s *q = (nns_edge_queue_s *) handle; + + if (!q) { + nns_edge_loge ("[Queue] Invalid param, queue is null."); + return false; + } + + nns_edge_lock (q); + nns_edge_cond_signal (q); + + while (q->length > 0U) + _pop_data (q, true, NULL, NULL); + + nns_edge_unlock (q); + return true; +} diff --git a/src/libnnstreamer-edge/nnstreamer-edge-queue.h b/src/libnnstreamer-edge/nnstreamer-edge-queue.h index 7bc1994..ff43b2d 100644 --- a/src/libnnstreamer-edge/nnstreamer-edge-queue.h +++ b/src/libnnstreamer-edge/nnstreamer-edge-queue.h @@ -92,6 +92,14 @@ bool nns_edge_queue_pop (nns_edge_queue_h handle, void **data, nns_size_t *size) */ bool nns_edge_queue_wait_pop (nns_edge_queue_h handle, unsigned int timeout, void **data, nns_size_t *size); +/** + * @brief Stop waiting for new data and clear all data in the queue. + * @param[in] handle The queue handle. + * @return true on success. + * @note When this function is called, nns_edge_queue_wait_pop will stop the waiting. + */ +bool nns_edge_queue_clear (nns_edge_queue_h handle); + #ifdef __cplusplus } #endif /* __cplusplus */ -- 2.34.1