report-json-serializer: app and top serializers
[apps/native/ttsd-worker-task.git] / src / scheduler.c
1 /*
2  * Copyright (c) 2018 Samsung Electronics Co., Ltd.
3  *
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
7  *
8  * http://floralicense.org/license/
9  *
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.
15  */
16
17 #include <glib.h>
18 #include "err-check.h"
19 #include "scheduler.h"
20 #include "task.h"
21 #include "worker.h"
22 #include "task-factory.h"
23
24 struct scheduler
25 {
26     worker_t *worker;
27     struct config_item *items;
28     size_t items_size;
29 };
30
31 struct config_item
32 {
33     worker_t *worker;
34     guint timer_id;
35     task_t *task;
36     int frequency;
37 };
38
39 static gboolean timer_func(gpointer user_data);
40 static void free_items(struct config_item *items, size_t items_size);
41
42 scheduler_t *scheduler_create()
43 {
44     struct scheduler *scheduler = g_malloc(sizeof(struct scheduler));
45     scheduler->worker = worker_create();
46     scheduler->items = NULL;
47
48     return scheduler;
49 }
50
51 void scheduler_destroy(scheduler_t *scheduler)
52 {
53     ON_NULL_RETURN(scheduler);
54
55     worker_destroy(scheduler->worker);
56     free_items(scheduler->items, scheduler->items_size);
57
58     g_free(scheduler);
59 }
60
61 void scheduler_change_config(scheduler_t *scheduler, const config_t *task_configs, size_t items_size)
62 {
63     ON_NULL_RETURN(scheduler);
64     ON_NULL_RETURN(task_configs);
65
66     worker_sync(scheduler->worker);
67     free_items(scheduler->items, scheduler->items_size);
68
69     scheduler->items_size = items_size;
70     scheduler->items = g_malloc(items_size * sizeof(struct config_item));
71
72     for (int i = 0; i < items_size; i++)
73     {
74         scheduler->items[i].worker = scheduler->worker;
75         scheduler->items[i].task = task_factory_create_single(&task_configs[i]);
76         scheduler->items[i].frequency = task_configs[i].frequency;
77         scheduler->items[i].timer_id = g_timeout_add(scheduler->items[i].frequency, timer_func, &scheduler->items[i]);
78     }
79 }
80
81 static gboolean timer_func(gpointer user_data)
82 {
83     ON_NULL_RETURN_VAL(user_data, FALSE);
84
85     struct config_item *config = (struct config_item *)user_data;
86
87     worker_enqueue_task(config->worker, config->task);
88
89     return TRUE;
90 }
91
92 static void free_items(struct config_item *items, size_t items_size)
93 {
94     ON_NULL_RETURN(items);
95
96     for (int i = 0; i < items_size; i++)
97     {
98         g_source_remove(items[i].timer_id);
99         task_release(items[i].task);
100     }
101
102     g_free(items);
103 }