From 8b588b092fd043ca149c538ee6d091afa63d0002 Mon Sep 17 00:00:00 2001 From: Jeonghoon Park Date: Thu, 19 Apr 2018 15:33:32 +0900 Subject: [PATCH] adds queue handle module Change-Id: Iec7a5bc699cb160f09fa145542ab648179c1cecc --- daemon/include/ttd-queue.h | 31 ++++++++++ daemon/src/ttd-queue.c | 147 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 178 insertions(+) create mode 100644 daemon/include/ttd-queue.h create mode 100644 daemon/src/ttd-queue.c diff --git a/daemon/include/ttd-queue.h b/daemon/include/ttd-queue.h new file mode 100644 index 0000000..5d90e72 --- /dev/null +++ b/daemon/include/ttd-queue.h @@ -0,0 +1,31 @@ +/* + * Copyright (c) 2018 Samsung Electronics Co., Ltd. + * + * Licensed under the Flora License, Version 1.1 (the License); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://floralicense.org/license/ + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an AS IS BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef __TT_DAEMON_QUEUE_H__ +#define __TT_DAEMON_QUEUE_H__ + +typedef enum { + TTD_QUEUE_TYPE_CMD, + TTD_QUEUE_TYPE_RESULT, +} ttd_queue_type_e; + +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); +void *ttd_queue_timeout_pop(ttd_queue_type_e type, unsigned int timeout); + +#endif /* __TT_DAEMON_QUEUE_H__ */ diff --git a/daemon/src/ttd-queue.c b/daemon/src/ttd-queue.c new file mode 100644 index 0000000..8e754c0 --- /dev/null +++ b/daemon/src/ttd-queue.c @@ -0,0 +1,147 @@ +/* + * Copyright (c) 2018 Samsung Electronics Co., Ltd. + * + * Licensed under the Flora License, Version 1.1 (the License); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://floralicense.org/license/ + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an AS IS BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include +#include +#include "ttd-log.h" +#include "ttd-queue.h" + +static GAsyncQueue *queue_cmd = NULL; +static GAsyncQueue *queue_res = NULL; + +static void _cmd_queue_item_free(gpointer data) +{ + /* TODO */ + free(data); +} + +static void _res_queue_item_free(gpointer data) +{ + /* TODO */ + 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); + + if (!(queue_cmd && queue_res)) { + _E("failed to create queue - cmd[%p], res[%p]", queue_cmd, queue_res); + return -1; + } + + return 0; +} + +int ttd_queue_fini(void) +{ + if (queue_cmd) { + g_async_queue_unref(queue_cmd); + queue_cmd = NULL; + } + + if (queue_res) { + g_async_queue_unref(queue_res); + queue_res = NULL; + } + + return 0; +} + +int ttd_queue_push(ttd_queue_type_e type, void *data) +{ + GAsyncQueue *queue = NULL; + + if (data == NULL) { + _E("data is NULL"); + return -1; + } + + switch (type) { + case TTD_QUEUE_TYPE_CMD: + if (!queue_cmd) { + _E("queue_cmd is not initialized yet"); + return -1; + } + queue = queue_cmd; + break; + case TTD_QUEUE_TYPE_RESULT: + if (!queue_res) { + _E("queue_res is not initialized yet"); + return -1; + } + queue = queue_res; + break; + } + + _V("pushing data[%p] to queue[type - %d]", data, type); + g_async_queue_push(queue, data); + + return 0; +} + +static GAsyncQueue *__get_queue(ttd_queue_type_e type) +{ + GAsyncQueue *queue = NULL; + switch (type) { + case TTD_QUEUE_TYPE_CMD: + if (!queue_cmd) { + _E("queue_cmd is not initialized yet"); + return NULL; + } + queue = queue_cmd; + break; + case TTD_QUEUE_TYPE_RESULT: + if (!queue_res) { + _E("queue_res is not initialized yet"); + return NULL; + } + queue = queue_res; + break; + } + + return queue; +} + +int ttd_queue_pop(ttd_queue_type_e type, void **data) +{ + GAsyncQueue *queue = NULL; + void *item = NULL; + + retvm_if(!data, -1, "data is NULL"); + + queue = __get_queue(type); + retv_if(!queue, -1); + + item = g_async_queue_pop(queue); + *data = item; + + return 0; +} + +void *ttd_queue_timeout_pop(ttd_queue_type_e type, unsigned int timeout_msec) +{ + GAsyncQueue *queue = NULL; + void *item = NULL; + + queue = __get_queue(type); + retv_if(!queue, NULL); + + item = g_async_queue_timeout_pop(queue, (guint64)timeout_msec*1000); + + return item; +} -- 2.7.4