+++ /dev/null
-/*
- * 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;
-
-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 *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__ */
+++ /dev/null
-/*
- * 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_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_data_free_func data_free_fn);
-int ttd_task_set_task_id(ttd_task *task, const char *id);
-const char *ttd_task_get_task_id(ttd_task *task);
-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);
-void ttd_task_free(ttd_task *task);
-
-#endif /* __TT_DAEMON_TASK_H__ */
+++ /dev/null
-/*
- * 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 <glib.h>
-#include <stdlib.h>
-#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 __queue_item_free(gpointer data)
-{
- queue_item_s *queue_item = data;
- if (!queue_item)
- return;
-
- 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(__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);
- 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 *item,
- ttd_queue_item_free_func free_fn)
-{
- GAsyncQueue *queue = NULL;
- queue_item_s *queue_item = NULL;
-
- if (item == NULL) {
- _E("item 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;
- }
-
- 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;
-}
-
-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;
-}
-
-void *ttd_queue_pop(ttd_queue_type_e type)
-{
- GAsyncQueue *queue = NULL;
- queue_item_s *queue_item = NULL;
- void *item = NULL;
-
- queue = __get_queue(type);
- retv_if(!queue, NULL);
-
- queue_item = g_async_queue_pop(queue);
- if (queue_item) {
- item = queue_item->item;
- g_free(queue_item);
-
- _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);
-
- 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;
-}
+++ /dev/null
-/*
- * 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 <glib.h>
-#include "ttd-log.h"
-#include "ttd-task.h"
-
-struct __ttd_task {
- int priority;
- ttd_task_func task_func;
- char *task_id;
- 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_data_free_func data_free_fn)
-{
- ttd_task *task = NULL;
-
- task = g_malloc0(sizeof(struct __ttd_task));
- if (task) {
- task->priority = priority;
- task->task_func = task_func;
- task->task_data = task_data;
- task->data_free_func = data_free_fn;
- }
- return task;
-}
-
-int ttd_task_set_task_id(ttd_task *task, const char *id)
-{
- if (!task)
- return -1;
-
- if (task->task_id)
- g_free(task->task_id);
-
- task->task_id = g_strdup(id);
-
- return 0;
-}
-
-const char *ttd_task_get_task_id(ttd_task *task)
-{
- if (!task)
- return NULL;
-
- return (const char *)task->task_id;
-}
-
-int ttd_task_set_complete_func(ttd_task *task, ttd_task_complete_func complete_func, void *user_data)
-{
- if (!task)
- return -1;
-
- task->complete_func = complete_func;
- task->user_data = user_data;
-
- return 0;
-}
-
-int ttd_task_sort_func(const void *a, const void *b, void *user_data)
-{
- const ttd_task *task_a = a;
- const ttd_task *task_b = b;
-
- if (!task_a || !task_b)
- return 0;
-
- return (task_a->priority - task_b->priority);
-}
-
-int ttd_task_run(ttd_task *task)
-{
- int ret = 0;
-
- retvm_if(!task, -1, "task is NULL");
- retvm_if(!task->task_func, -1, "task_func is NULL");
-
- if (task->task_id)
- _D("task [%s] is running", task->task_id);
-
- ret = task->task_func(task->task_data);
-
- if (task->complete_func) {
- /* 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;
-}
-
-void ttd_task_free(ttd_task *task)
-{
- if (!task)
- return;
-
- if (task->task_id) {
- _V("task [%s] is freed", task->task_id);
- g_free(task->task_id);
- }
- g_free(task);
-}