util: thread: Support free function for destorying queue 66/271766/2 accepted/tizen/unified/20220302.131908 submit/tizen/20220302.025412 submit/tizen/20220302.031108
authorDongwoo Lee <dwoo08.lee@samsung.com>
Mon, 28 Feb 2022 09:02:10 +0000 (18:02 +0900)
committerDongwoo Lee <dwoo08.lee@samsung.com>
Mon, 28 Feb 2022 10:02:07 +0000 (19:02 +0900)
Since lock-free queue can have various data type, users can pass free
function when creating queue along with the corresponding data type in
queue.

Change-Id: I4ede38bd6e36a1622bb39c7183829c5372e3264b
Signed-off-by: Dongwoo Lee <dwoo08.lee@samsung.com>
include/util/queue.h
src/monitor/monitor-thread.c
src/util/queue.c

index 23b8331..2fdae3b 100644 (file)
@@ -44,6 +44,7 @@ struct queue_node {
 struct queue {
        struct queue_node *head;
        struct queue_node *tail;
+       void (*free_func)(void *data);
 };
 
 static inline void cpu_relax(void)
@@ -53,7 +54,7 @@ static inline void cpu_relax(void)
 
 int enqueue(struct queue *queue, void *data);
 int dequeue(struct queue *queue, void **data);
-int create_queue(struct queue **queue);
+int create_queue(struct queue **queue, void (*free_func)(void *data));
 void destroy_queue(struct queue *queue);
 
 #endif
index e8a3b12..8cd535a 100644 (file)
@@ -54,7 +54,7 @@ int monitor_thread_init(struct monitor *monitor)
        struct queue *queue;
        int ret;
 
-       ret = create_queue(&queue);
+       ret = create_queue(&queue, free);
        if (ret < 0) {
                _E("failed to create command queue\n");
                return ret;
index ec3819d..1312a2c 100644 (file)
@@ -65,7 +65,7 @@ int dequeue(struct queue *queue, void **data)
        return 0;
 }
 
-int create_queue(struct queue **queue)
+int create_queue(struct queue **queue, void (*free_func)(void *data))
 {
        struct queue *new_queue;
        struct queue_node *sentinel;
@@ -80,6 +80,7 @@ int create_queue(struct queue **queue)
                return -ENOMEM;
        }
 
+       new_queue->free_func = free_func;
        new_queue->head = new_queue->tail = sentinel;
        new_queue->head->data = NULL;
        new_queue->head->next = NULL;
@@ -91,7 +92,13 @@ int create_queue(struct queue **queue)
 
 void destroy_queue(struct queue *queue)
 {
-       do { } while (!dequeue(queue, NULL));
+       void *data;
+
+       if (queue->free_func)
+               while (!dequeue(queue, &data))
+                       queue->free_func(data);
+       else
+               do { } while (!dequeue(queue, NULL));
 
        /* freeing remaining sentinel */
        free(queue->head);