adds task handle module 18/176418/1
authorJeonghoon Park <jh1979.park@samsung.com>
Thu, 19 Apr 2018 06:33:12 +0000 (15:33 +0900)
committerJeonghoon Park <jh1979.park@samsung.com>
Thu, 19 Apr 2018 06:33:12 +0000 (15:33 +0900)
Change-Id: Ie88d43f56bb8862d499cd70bb6a98c7ebd374d0f

daemon/include/ttd-task.h [new file with mode: 0644]
daemon/src/ttd-task.c [new file with mode: 0644]

diff --git a/daemon/include/ttd-task.h b/daemon/include/ttd-task.h
new file mode 100644 (file)
index 0000000..bf7367e
--- /dev/null
@@ -0,0 +1,29 @@
+/*
+ * 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__
+
+typedef struct __ttd_task ttd_task;
+typedef int (*ttd_task_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);
+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);
+
+#endif /* __TT_DAEMON_TASK_H__ */
diff --git a/daemon/src/ttd-task.c b/daemon/src/ttd-task.c
new file mode 100644 (file)
index 0000000..18892bc
--- /dev/null
@@ -0,0 +1,78 @@
+/*
+ * 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;
+       void *task_data;
+       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 *task = NULL;
+
+       task = g_malloc0(sizeof(struct __ttd_task));
+       if (task) {
+               task->priority = priority;
+               task->task_func = task_func;
+               task->task_data = task_data;
+       }
+       return task;
+}
+
+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");
+
+       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);
+       }
+       return 0;
+}