From afe978442e9622e44d92f177508ea6f051a89dfb Mon Sep 17 00:00:00 2001 From: Jeonghoon Park Date: Thu, 3 May 2018 19:16:24 +0900 Subject: [PATCH] adds custom free function for data in queue and task Change-Id: Ibb69ca2c57d3ae554c18ce4de93d935c60606616 --- daemon/include/ttd-queue.h | 7 ++-- daemon/include/ttd-task.h | 8 ++++- daemon/src/tizen-things-daemon.c | 5 +-- daemon/src/ttd-queue.c | 70 ++++++++++++++++++++++++++-------------- daemon/src/ttd-task.c | 11 ++++++- 5 files changed, 71 insertions(+), 30 deletions(-) diff --git a/daemon/include/ttd-queue.h b/daemon/include/ttd-queue.h index 5d90e72..89dc793 100644 --- a/daemon/include/ttd-queue.h +++ b/daemon/include/ttd-queue.h @@ -22,10 +22,13 @@ typedef enum { TTD_QUEUE_TYPE_RESULT, } ttd_queue_type_e; +typedef void (*ttd_queue_item_free_func)(void *item); + int ttd_queue_fini(void); int ttd_queue_init(void); -int ttd_queue_push(ttd_queue_type_e type, void *data); -int ttd_queue_pop(ttd_queue_type_e type, void **data); +int ttd_queue_push( + ttd_queue_type_e type, void *item, ttd_queue_item_free_func free_fn); +void *ttd_queue_pop(ttd_queue_type_e type); void *ttd_queue_timeout_pop(ttd_queue_type_e type, unsigned int timeout); #endif /* __TT_DAEMON_QUEUE_H__ */ diff --git a/daemon/include/ttd-task.h b/daemon/include/ttd-task.h index bf7367e..4462e97 100644 --- a/daemon/include/ttd-task.h +++ b/daemon/include/ttd-task.h @@ -17,11 +17,17 @@ #ifndef __TT_DAEMON_TASK_H__ #define __TT_DAEMON_TASK_H__ +#define TTD_TASK_PRIORITY_LOW 50 +#define TTD_TASK_PRIORITY_NORMAL 100 +#define TTD_TASK_PRIORITY_HIGH 150 + typedef struct __ttd_task ttd_task; typedef int (*ttd_task_func)(void *task_data); +typedef void (*ttd_task_data_free_func)(void *task_data); typedef void (*ttd_task_complete_func)(int result, void *user_data); -ttd_task *ttd_task_new(int priority, ttd_task_func task_fn, void *task_data); +ttd_task *ttd_task_new(int priority, ttd_task_func task_fn, void *task_data, + ttd_task_data_free_func data_free_fn); int ttd_task_set_complete_func(ttd_task *task, ttd_task_complete_func complete_fn, void *user_data); int ttd_task_run(ttd_task *task); int ttd_task_sort_func(const void *a, const void *b, void *user_data); diff --git a/daemon/src/tizen-things-daemon.c b/daemon/src/tizen-things-daemon.c index 33b479d..ca4bfe0 100644 --- a/daemon/src/tizen-things-daemon.c +++ b/daemon/src/tizen-things-daemon.c @@ -31,7 +31,7 @@ #ifndef SERVER_URL /* TODO : remove it after test */ -#define TEST_SERVER_URL "https://test.showiot.xyz/api/package/metadata?id=test-page1523426067178&key=7138e585cc75f480094a9dc5d8719e925c24b435" +#define TEST_SERVER_URL "http://apitest.showiot.xyz/api/cmd?&target=test-page-device&owner=test-page&state=created" #define SERVER_URL TEST_SERVER_URL #endif @@ -154,7 +154,8 @@ static int __say_hello_to_cloud(void *data) cmd_data = (ttd_cmd_data *)l->data; if (cmd_data) - ttd_queue_push(TTD_QUEUE_TYPE_CMD, cmd_data); + ttd_queue_push(TTD_QUEUE_TYPE_CMD, + cmd_data, (ttd_queue_item_free_func)ttd_cmd_free); } }else _D("there is no cmd now"); diff --git a/daemon/src/ttd-queue.c b/daemon/src/ttd-queue.c index 8e754c0..c0af930 100644 --- a/daemon/src/ttd-queue.c +++ b/daemon/src/ttd-queue.c @@ -19,25 +19,32 @@ #include "ttd-log.h" #include "ttd-queue.h" +typedef struct _queue_item_s { + void *item; + ttd_queue_item_free_func free_fn; +} queue_item_s; + static GAsyncQueue *queue_cmd = NULL; static GAsyncQueue *queue_res = NULL; -static void _cmd_queue_item_free(gpointer data) +static void __queue_item_free(gpointer data) { - /* TODO */ - free(data); -} + queue_item_s *queue_item = data; + if (!queue_item) + return; -static void _res_queue_item_free(gpointer data) -{ - /* TODO */ - free(data); + if (queue_item->free_fn) + queue_item->free_fn(queue_item->item); + else + g_free(queue_item->item); + + g_free(data); } int ttd_queue_init(void) { - queue_cmd = g_async_queue_new_full(_cmd_queue_item_free); - queue_res = g_async_queue_new_full(_res_queue_item_free); + queue_cmd = g_async_queue_new_full(__queue_item_free); + queue_res = g_async_queue_new_full(__queue_item_free); if (!(queue_cmd && queue_res)) { _E("failed to create queue - cmd[%p], res[%p]", queue_cmd, queue_res); @@ -62,12 +69,14 @@ int ttd_queue_fini(void) return 0; } -int ttd_queue_push(ttd_queue_type_e type, void *data) +int ttd_queue_push(ttd_queue_type_e type, void *item, + ttd_queue_item_free_func free_fn) { GAsyncQueue *queue = NULL; + queue_item_s *queue_item = NULL; - if (data == NULL) { - _E("data is NULL"); + if (item == NULL) { + _E("item is NULL"); return -1; } @@ -88,8 +97,13 @@ int ttd_queue_push(ttd_queue_type_e type, void *data) break; } - _V("pushing data[%p] to queue[type - %d]", data, type); - g_async_queue_push(queue, data); + queue_item = g_malloc(sizeof(queue_item_s)); + retvm_if(!queue_item, -1, "failed to malloc for push item"); + queue_item->item = item; + queue_item->free_fn = free_fn; + + _V("pushing item[%p] to queue[type - %d]", queue_item->item, type); + g_async_queue_push(queue, queue_item); return 0; } @@ -117,31 +131,39 @@ static GAsyncQueue *__get_queue(ttd_queue_type_e type) return queue; } -int ttd_queue_pop(ttd_queue_type_e type, void **data) +void *ttd_queue_pop(ttd_queue_type_e type) { GAsyncQueue *queue = NULL; + queue_item_s *queue_item = NULL; void *item = NULL; - retvm_if(!data, -1, "data is NULL"); - queue = __get_queue(type); - retv_if(!queue, -1); + retv_if(!queue, NULL); - item = g_async_queue_pop(queue); - *data = item; + queue_item = g_async_queue_pop(queue); + if (queue_item) { + item = queue_item->item; + g_free(queue_item); - return 0; + _V("poping item[%p] from queue[type - %d]", item, type); + } + return item; } void *ttd_queue_timeout_pop(ttd_queue_type_e type, unsigned int timeout_msec) { GAsyncQueue *queue = NULL; + queue_item_s *queue_item = NULL; void *item = NULL; queue = __get_queue(type); retv_if(!queue, NULL); - item = g_async_queue_timeout_pop(queue, (guint64)timeout_msec*1000); - + queue_item = g_async_queue_timeout_pop(queue, (guint64)timeout_msec*1000); + if (queue_item) { + item = queue_item->item; + g_free(queue_item); + _V("poping item[%p] from queue[type - %d]", item, type); + } return item; } diff --git a/daemon/src/ttd-task.c b/daemon/src/ttd-task.c index 18892bc..bd69656 100644 --- a/daemon/src/ttd-task.c +++ b/daemon/src/ttd-task.c @@ -22,11 +22,13 @@ struct __ttd_task { int priority; ttd_task_func task_func; void *task_data; + ttd_task_data_free_func data_free_func; ttd_task_complete_func complete_func; void *user_data; }; -ttd_task *ttd_task_new(int priority, ttd_task_func task_func, void *task_data) +ttd_task *ttd_task_new(int priority, ttd_task_func task_func, void *task_data, + ttd_task_data_free_func data_free_fn) { ttd_task *task = NULL; @@ -35,6 +37,7 @@ ttd_task *ttd_task_new(int priority, ttd_task_func task_func, void *task_data) task->priority = priority; task->task_func = task_func; task->task_data = task_data; + task->data_free_func = data_free_fn; } return task; } @@ -74,5 +77,11 @@ int ttd_task_run(ttd_task *task) /* TODO : call it in mainloop or in thread????? */ task->complete_func(ret, task->user_data); } + + if (task->data_free_func) + task->data_free_func(task->task_data); + else + g_free(task->task_data); + return 0; } -- 2.7.4