idle.c 6.6 KB
Newer Older
1
/*
2
 * Copyright (c) 2006-2018, RT-Thread Development Team
B
Bernard Xiong 已提交
3
 *
4
 * SPDX-License-Identifier: Apache-2.0
5 6 7 8
 *
 * Change Logs:
 * Date           Author       Notes
 * 2006-03-23     Bernard      the first version
9
 * 2010-11-10     Bernard      add cleanup callback function in thread exit.
10
 * 2012-12-29     Bernard      fix compiling warning.
11 12
 * 2013-12-21     Grissiom     let rt_thread_idle_excute loop until there is no
 *                             dead thread.
13
 * 2016-08-09     ArdaFu       add method to get the handler of the idle thread.
14
 * 2018-02-07     Bernard      lock scheduler to protect tid->cleanup.
15
 * 2018-07-14     armink       add idle hook list
16 17 18 19 20
 */

#include <rthw.h>
#include <rtthread.h>

21 22 23 24
#ifdef RT_USING_MODULE
#include <dlmodule.h>
#endif

X
xieyangrun 已提交
25 26
#if defined (RT_USING_HOOK)
#ifndef RT_USING_IDLE_HOOK
27
#define RT_USING_IDLE_HOOK
X
xieyangrun 已提交
28 29 30
#endif
#endif

31
#ifndef IDLE_THREAD_STACK_SIZE
X
xieyangrun 已提交
32
#if defined (RT_USING_IDLE_HOOK) || defined(RT_USING_HEAP)
33
#define IDLE_THREAD_STACK_SIZE  256
34
#else
35
#define IDLE_THREAD_STACK_SIZE  128
36
#endif
37
#endif
38 39

static struct rt_thread idle;
40
ALIGN(RT_ALIGN_SIZE)
41 42 43 44
static rt_uint8_t rt_thread_stack[IDLE_THREAD_STACK_SIZE];

extern rt_list_t rt_thread_defunct;

X
xieyangrun 已提交
45
#ifdef RT_USING_IDLE_HOOK
armink_ztl's avatar
armink_ztl 已提交
46 47 48 49 50 51

#ifndef RT_IDEL_HOOK_LIST_SIZE
#define RT_IDEL_HOOK_LIST_SIZE          4
#endif

static void (*idle_hook_list[RT_IDEL_HOOK_LIST_SIZE])();
52 53

/**
B
bernard 已提交
54
 * @ingroup Hook
55
 * This function sets a hook function to idle thread loop. When the system performs
B
bernard 已提交
56
 * idle loop, this hook function should be invoked.
57 58 59
 *
 * @param hook the specified hook function
 *
armink_ztl's avatar
armink_ztl 已提交
60 61 62
 * @return RT_EOK: set OK
 *         -RT_EFULL: hook list is full
 *
63 64
 * @note the hook function must be simple and never be blocked or suspend.
 */
armink_ztl's avatar
armink_ztl 已提交
65
rt_err_t rt_thread_idle_sethook(void (*hook)(void))
66
{
armink_ztl's avatar
armink_ztl 已提交
67 68
    rt_size_t i;
    rt_base_t level;
69
    rt_err_t ret = -RT_EFULL;
armink_ztl's avatar
armink_ztl 已提交
70 71 72 73 74 75 76 77 78

    /* disable interrupt */
    level = rt_hw_interrupt_disable();

    for (i = 0; i < RT_IDEL_HOOK_LIST_SIZE; i++)
    {
        if (idle_hook_list[i] == RT_NULL)
        {
            idle_hook_list[i] = hook;
79 80
            ret = RT_EOK;
            break;
armink_ztl's avatar
armink_ztl 已提交
81 82 83 84 85
        }
    }
    /* enable interrupt */
    rt_hw_interrupt_enable(level);

86
    return ret;
87
}
armink_ztl's avatar
armink_ztl 已提交
88 89 90 91 92 93 94 95 96 97 98 99 100

/**
 * delete the idle hook on hook list
 *
 * @param hook the specified hook function
 *
 * @return RT_EOK: delete OK
 *         -RT_ENOSYS: hook was not found
 */
rt_err_t rt_thread_idle_delhook(void (*hook)(void))
{
    rt_size_t i;
    rt_base_t level;
101
    rt_err_t ret = -RT_ENOSYS;
armink_ztl's avatar
armink_ztl 已提交
102 103 104 105 106 107 108 109 110

    /* disable interrupt */
    level = rt_hw_interrupt_disable();

    for (i = 0; i < RT_IDEL_HOOK_LIST_SIZE; i++)
    {
        if (idle_hook_list[i] == hook)
        {
            idle_hook_list[i] = RT_NULL;
111 112
            ret = RT_EOK;
            break;
armink_ztl's avatar
armink_ztl 已提交
113 114 115 116 117
        }
    }
    /* enable interrupt */
    rt_hw_interrupt_enable(level);

118
    return ret;
armink_ztl's avatar
armink_ztl 已提交
119 120
}

121 122
#endif

123 124 125 126 127 128 129 130 131
/* Return whether there is defunctional thread to be deleted. */
rt_inline int _has_defunct_thread(void)
{
    /* The rt_list_isempty has prototype of "int rt_list_isempty(const rt_list_t *l)".
     * So the compiler has a good reason that the rt_thread_defunct list does
     * not change within rt_thread_idle_excute thus optimize the "while" loop
     * into a "if".
     *
     * So add the volatile qualifier here. */
132
    const volatile rt_list_t *l = (const volatile rt_list_t *)&rt_thread_defunct;
133 134 135 136

    return l->next != l;
}

B
bernard.xiong 已提交
137
/**
B
bernard.xiong@gmail.com 已提交
138 139 140
 * @ingroup Thread
 *
 * This function will perform system background job when system idle.
B
bernard.xiong 已提交
141 142
 */
void rt_thread_idle_excute(void)
143
{
144 145
    /* Loop until there is no dead thread. So one call to rt_thread_idle_excute
     * will do all the cleanups. */
146
    while (_has_defunct_thread())
147 148 149
    {
        rt_base_t lock;
        rt_thread_t thread;
B
bernard.xiong 已提交
150
#ifdef RT_USING_MODULE
151
        struct rt_dlmodule *module = RT_NULL;
152
#endif
153 154 155 156 157 158
        RT_DEBUG_NOT_IN_INTERRUPT;

        /* disable interrupt */
        lock = rt_hw_interrupt_disable();

        /* re-check whether list is empty */
159
        if (_has_defunct_thread())
160 161 162 163 164
        {
            /* get defunct thread */
            thread = rt_list_entry(rt_thread_defunct.next,
                                   struct rt_thread,
                                   tlist);
qiuyiuestc's avatar
qiuyiuestc 已提交
165
#ifdef RT_USING_MODULE
166 167
            module = (struct rt_dlmodule*)thread->module_id;
            if (module)
168
            {
169
                dlmodule_destroy(module);
170
            }
qiuyiuestc's avatar
qiuyiuestc 已提交
171
#endif
172 173
            /* remove defunct thread */
            rt_list_remove(&(thread->tlist));
174 175 176 177

            /* lock scheduler to prevent scheduling in cleanup function. */
            rt_enter_critical();

178 179 180 181
            /* invoke thread cleanup */
            if (thread->cleanup != RT_NULL)
                thread->cleanup(thread);

B
bernard 已提交
182 183 184 185
#ifdef RT_USING_SIGNALS
            rt_thread_free_sig(thread);
#endif

186 187 188
            /* if it's a system object, not delete it */
            if (rt_object_is_systemobject((rt_object_t)thread) == RT_TRUE)
            {
189 190 191
                /* unlock scheduler */
                rt_exit_critical();

192 193 194 195 196
                /* enable interrupt */
                rt_hw_interrupt_enable(lock);

                return;
            }
197 198 199

            /* unlock scheduler */
            rt_exit_critical();
200 201 202 203 204 205 206 207 208 209 210 211
        }
        else
        {
            /* enable interrupt */
            rt_hw_interrupt_enable(lock);

            /* may the defunct thread list is removed by others, just return */
            return;
        }

        /* enable interrupt */
        rt_hw_interrupt_enable(lock);
qiuyiuestc's avatar
qiuyiuestc 已提交
212

213
#ifdef RT_USING_HEAP
214 215
        /* release thread's stack */
        RT_KERNEL_FREE(thread->stack_addr);
216 217
        /* delete thread object */
        rt_object_delete((rt_object_t)thread);
qiuyiuestc's avatar
qiuyiuestc 已提交
218
#endif
219
    }
B
bernard.xiong 已提交
220 221
}

D
dzzxzz 已提交
222
static void rt_thread_idle_entry(void *parameter)
B
bernard.xiong 已提交
223
{
armink_ztl's avatar
armink_ztl 已提交
224 225 226 227
#ifdef RT_USING_IDLE_HOOK
    rt_size_t i;
#endif

228 229
    while (1)
    {
armink_ztl's avatar
armink_ztl 已提交
230

231
#ifdef RT_USING_IDLE_HOOK
armink_ztl's avatar
armink_ztl 已提交
232
        for (i = 0; i < RT_IDEL_HOOK_LIST_SIZE; i++)
G
geniusgogo 已提交
233
        {
armink_ztl's avatar
armink_ztl 已提交
234 235 236 237
            if (idle_hook_list[i] != RT_NULL)
            {
                idle_hook_list[i]();
            }
G
geniusgogo 已提交
238
        }
239
#endif
G
geniusgogo 已提交
240

241 242
        rt_thread_idle_excute();
    }
243 244 245
}

/**
B
bernard 已提交
246
 * @ingroup SystemInit
B
bernard.xiong@gmail.com 已提交
247
 *
248 249 250 251
 * This function will initialize idle thread, then start it.
 *
 * @note this function must be invoked when system init.
 */
D
dzzxzz 已提交
252
void rt_thread_idle_init(void)
253
{
254 255 256 257 258 259 260 261 262 263 264 265
    /* initialize thread */
    rt_thread_init(&idle,
                   "tidle",
                   rt_thread_idle_entry,
                   RT_NULL,
                   &rt_thread_stack[0],
                   sizeof(rt_thread_stack),
                   RT_THREAD_PRIORITY_MAX - 1,
                   32);

    /* startup */
    rt_thread_startup(&idle);
266
}
267 268 269 270 271 272 273 274 275 276 277

/**
 * @ingroup Thread
 *
 * This function will get the handler of the idle thread.
 *
 */
rt_thread_t rt_thread_idle_gethandler(void)
{
    return (rt_thread_t)(&idle);
}