summaryrefslogtreecommitdiffstats
path: root/src/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/core/app-mgr/app-manager/watchdog.c
blob: ba5bb05f5cc9c1fcb79aa281b0679eac21b3be16 (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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
/*
 * Copyright (C) 2019 Intel Corporation.  All rights reserved.
 * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
 */

#include "watchdog.h"
#include "bh_platform.h"

#define WATCHDOG_THREAD_PRIORITY 5

/* Queue of watchdog */
static bh_queue *watchdog_queue;

#ifdef WATCHDOG_ENABLED /* TODO */
static void
watchdog_timer_callback(void *timer)
{
    watchdog_timer *wd_timer =
        app_manager_get_wd_timer_from_timer_handle(timer);

    watchdog_timer_stop(wd_timer);

    os_mutex_lock(&wd_timer->lock);

    if (!wd_timer->is_stopped) {

        wd_timer->is_interrupting = true;

        bh_post_msg(watchdog_queue, WD_TIMEOUT, wd_timer->module_data,
                    sizeof(module_data));
    }

    os_mutex_unlock(&wd_timer->lock);
}
#endif

bool
watchdog_timer_init(module_data *m_data)
{
#ifdef WATCHDOG_ENABLED /* TODO */
    watchdog_timer *wd_timer = &m_data->wd_timer;

    if (0 != os_mutex_init(&wd_timer->lock))
        return false;

    if (!(wd_timer->timer_handle =
              app_manager_timer_create(watchdog_timer_callback, wd_timer))) {
        os_mutex_destroy(&wd_timer->lock);
        return false;
    }

    wd_timer->module_data = m_data;
    wd_timer->is_interrupting = false;
    wd_timer->is_stopped = false;
#endif
    return true;
}

void
watchdog_timer_destroy(watchdog_timer *wd_timer)
{
#ifdef WATCHDOG_ENABLED /* TODO */
    app_manager_timer_destroy(wd_timer->timer_handle);
    os_mutex_destroy(&wd_timer->lock);
#endif
}

void
watchdog_timer_start(watchdog_timer *wd_timer)
{
    os_mutex_lock(&wd_timer->lock);

    wd_timer->is_interrupting = false;
    wd_timer->is_stopped = false;
    app_manager_timer_start(wd_timer->timer_handle,
                            wd_timer->module_data->timeout);

    os_mutex_unlock(&wd_timer->lock);
}

void
watchdog_timer_stop(watchdog_timer *wd_timer)
{
    app_manager_timer_stop(wd_timer->timer_handle);
}

#ifdef WATCHDOG_ENABLED /* TODO */
static void
watchdog_queue_callback(void *queue_msg)
{
    if (bh_message_type(queue_msg) == WD_TIMEOUT) {
        module_data *m_data = (module_data *)bh_message_payload(queue_msg);
        if (g_module_interfaces[m_data->module_type]
            && g_module_interfaces[m_data->module_type]->module_watchdog_kill) {
            g_module_interfaces[m_data->module_type]->module_watchdog_kill(
                m_data);
            app_manager_post_applets_update_event();
        }
    }
}
#endif

#ifdef WATCHDOG_ENABLED /* TODO */
static void *
watchdog_thread_routine(void *arg)
{
    /* Enter loop run */
    bh_queue_enter_loop_run(watchdog_queue, watchdog_queue_callback);

    (void)arg;
    return NULL;
}
#endif

bool
watchdog_startup()
{
    if (!(watchdog_queue = bh_queue_create())) {
        app_manager_printf(
            "App Manager start failed: create watchdog queue failed.\n");
        return false;
    }
#if 0
//todo: enable watchdog
    /* Start watchdog thread */
    if (!jeff_runtime_create_supervisor_thread_with_prio(watchdog_thread_routine, NULL,
                    WATCHDOG_THREAD_PRIORITY)) {
        bh_queue_destroy(watchdog_queue);
        return false;
    }
#endif
    return true;
}

void
watchdog_destroy()
{
    bh_queue_exit_loop_run(watchdog_queue);
    bh_queue_destroy(watchdog_queue);
}