2 * Copyright (c) 2018 Samsung Electronics Co., Ltd.
4 * Licensed under the Flora License, Version 1.1 (the License);
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://floralicense.org/license/
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an AS IS BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
23 ttd_task_func task_func;
26 ttd_task_data_free_func data_free_func;
27 ttd_task_complete_func complete_func;
31 ttd_task *ttd_task_new(int priority, ttd_task_func task_func, void *task_data,
32 ttd_task_data_free_func data_free_fn)
34 ttd_task *task = NULL;
36 task = g_malloc0(sizeof(struct __ttd_task));
38 task->priority = priority;
39 task->task_func = task_func;
40 task->task_data = task_data;
41 task->data_free_func = data_free_fn;
46 int ttd_task_set_task_id(ttd_task *task, const char *id)
52 g_free(task->task_id);
54 task->task_id = g_strdup(id);
59 const char *ttd_task_get_task_id(ttd_task *task)
64 return (const char *)task->task_id;
67 int ttd_task_set_complete_func(ttd_task *task, ttd_task_complete_func complete_func, void *user_data)
72 task->complete_func = complete_func;
73 task->user_data = user_data;
78 int ttd_task_sort_func(const void *a, const void *b, void *user_data)
80 const ttd_task *task_a = a;
81 const ttd_task *task_b = b;
83 if (!task_a || !task_b)
86 return (task_a->priority - task_b->priority);
89 int ttd_task_run(ttd_task *task)
93 retvm_if(!task, -1, "task is NULL");
94 retvm_if(!task->task_func, -1, "task_func is NULL");
97 _D("task [%s] is running", task->task_id);
99 ret = task->task_func(task->task_data);
101 if (task->complete_func) {
102 /* TODO : call it in mainloop or in thread????? */
103 task->complete_func(ret, task->user_data);
106 if (task->data_free_func)
107 task->data_free_func(task->task_data);
109 g_free(task->task_data);
114 void ttd_task_free(ttd_task *task)
120 _V("task [%s] is freed", task->task_id)
121 g_free(task->task_id);