summaryrefslogtreecommitdiff
path: root/src/scheduler.c
blob: 6239cd58e3898ac935105cbd91197c8abcc0c4da (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
/*
 * 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 "err-check.h"
#include "scheduler.h"
#include "task.h"
#include "worker.h"
#include "task-factory.h"
#include "task-worker.h"

struct scheduler
{
    worker_t *worker;
    struct config_item *items;
    size_t items_size;
};

struct config_item
{
    worker_t *worker;
    guint timer_id;
    task_t *task;
    int frequency;
};

static gboolean timer_func(gpointer user_data);
static void free_items(struct config_item *items, size_t items_size);

scheduler_t *scheduler_create()
{
    struct scheduler *scheduler = g_malloc(sizeof(struct scheduler));
    scheduler->worker = worker_create();
    scheduler->items = NULL;

    return scheduler;
}

void scheduler_destroy(scheduler_t *scheduler)
{
    ON_NULL_RETURN(scheduler);

    worker_destroy(scheduler->worker);
    free_items(scheduler->items, scheduler->items_size);

    g_free(scheduler);
}

void scheduler_change_config(scheduler_t *scheduler, const config_t *task_configs, size_t items_size)
{
    ON_NULL_RETURN(scheduler);
    ON_NULL_RETURN(task_configs);

    worker_sync(scheduler->worker);
    free_items(scheduler->items, scheduler->items_size);

    scheduler->items_size = items_size;
    scheduler->items = g_malloc(items_size * sizeof(struct config_item));

    for (int i = 0; i < items_size; i++)
    {
        scheduler->items[i].worker = scheduler->worker;
        scheduler->items[i].task = task_factory_create_task(&task_configs[i]);
        scheduler->items[i].frequency = task_configs[i].frequency;
        scheduler->items[i].timer_id = g_timeout_add(scheduler->items[i].frequency, timer_func, &scheduler->items[i]);
    }
}

static gboolean timer_func(gpointer user_data)
{
    ON_NULL_RETURN_VAL(user_data, FALSE);

    struct config_item *config = (struct config_item *)user_data;

    worker_enqueue_task(config->worker, config->task);

    return TRUE;
}

static void free_items(struct config_item *items, size_t items_size)
{
    ON_NULL_RETURN(items);

    for (int i = 0; i < items_size; i++)
    {
        g_source_remove(items[i].timer_id);
        task_release(items[i].task);
    }

    g_free(items);
}