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.
18 #include "err-check.h"
19 #include "scheduler.h"
22 #include "task-factory.h"
27 struct config_item *items;
39 static gboolean timer_func(gpointer user_data);
40 static void free_items(struct config_item *items, size_t items_size);
42 scheduler_t *scheduler_create()
44 struct scheduler *scheduler = g_malloc(sizeof(struct scheduler));
45 scheduler->worker = worker_create();
46 scheduler->items = NULL;
51 void scheduler_destroy(scheduler_t *scheduler)
53 ON_NULL_RETURN(scheduler);
55 worker_destroy(scheduler->worker);
56 free_items(scheduler->items, scheduler->items_size);
61 void scheduler_change_config(scheduler_t *scheduler, const config_t *task_configs, size_t items_size)
63 ON_NULL_RETURN(scheduler);
64 ON_NULL_RETURN(task_configs);
66 worker_sync(scheduler->worker);
67 free_items(scheduler->items, scheduler->items_size);
69 scheduler->items_size = items_size;
70 scheduler->items = g_malloc(items_size * sizeof(struct config_item));
72 for (int i = 0; i < items_size; i++)
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]);
81 static gboolean timer_func(gpointer user_data)
83 ON_NULL_RETURN_VAL(user_data, FALSE);
85 struct config_item *config = (struct config_item *)user_data;
87 worker_enqueue_task(config->worker, config->task);
92 static void free_items(struct config_item *items, size_t items_size)
94 ON_NULL_RETURN(items);
96 for (int i = 0; i < items_size; i++)
98 g_source_remove(items[i].timer_id);
99 task_release(items[i].task);