scheduler.c 11.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 9 10
 *
 * Change Logs:
 * Date           Author       Notes
 * 2006-03-17     Bernard      the first version
 * 2006-04-28     Bernard      fix the scheduler algorthm
 * 2006-04-30     Bernard      add SCHEDULER_DEBUG
11 12
 * 2006-05-27     Bernard      fix the scheduler algorthm for same priority
 *                             thread schedule
13 14 15 16 17
 * 2006-06-04     Bernard      rewrite the scheduler algorithm
 * 2006-08-03     Bernard      add hook support
 * 2006-09-05     Bernard      add 32 priority level support
 * 2006-09-24     Bernard      add rt_system_scheduler_start function
 * 2009-09-16     Bernard      fix _rt_scheduler_stack_check
18
 * 2010-04-11     yi.qiu       add module feature
19
 * 2010-07-13     Bernard      fix the maximal number of rt_scheduler_lock_nest
20
 *                             issue found by kuronca
21
 * 2010-12-13     Bernard      add defunct list initialization even if not use heap.
B
bernard.xiong@gmail.com 已提交
22
 * 2011-05-10     Bernard      clean scheduler debug log.
23
 * 2013-12-21     Grissiom     add rt_critical_level
24 25 26 27 28 29
 */

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

static rt_int16_t rt_scheduler_lock_nest;
30
extern volatile rt_uint8_t rt_interrupt_nest;
31 32

rt_list_t rt_thread_priority_table[RT_THREAD_PRIORITY_MAX];
D
dzzxzz 已提交
33
struct rt_thread *rt_current_thread;
34 35 36 37

rt_uint8_t rt_current_priority;

#if RT_THREAD_PRIORITY_MAX > 32
38
/* Maximum priority level, 256 */
39 40 41
rt_uint32_t rt_thread_ready_priority_group;
rt_uint8_t rt_thread_ready_table[32];
#else
42
/* Maximum priority level, 32 */
43 44 45 46 47 48
rt_uint32_t rt_thread_ready_priority_group;
#endif

rt_list_t rt_thread_defunct;

#ifdef RT_USING_HOOK
D
dzzxzz 已提交
49
static void (*rt_scheduler_hook)(struct rt_thread *from, struct rt_thread *to);
50 51 52 53

/**
 * @addtogroup Hook
 */
D
dzzxzz 已提交
54

D
dogandog 已提交
55
/**@{*/
56 57 58 59 60 61 62

/**
 * This function will set a hook function, which will be invoked when thread
 * switch happens.
 *
 * @param hook the hook function
 */
63 64
void
rt_scheduler_sethook(void (*hook)(struct rt_thread *from, struct rt_thread *to))
65
{
66
    rt_scheduler_hook = hook;
67 68
}

D
dogandog 已提交
69
/**@}*/
70 71 72
#endif

#ifdef RT_USING_OVERFLOW_CHECK
D
dzzxzz 已提交
73
static void _rt_scheduler_stack_check(struct rt_thread *thread)
74
{
75 76
    RT_ASSERT(thread != RT_NULL);

A
Aubr.Cool 已提交
77
    if (*((rt_uint8_t *)thread->stack_addr) != '#' ||
78
        (rt_uint32_t)thread->sp <= (rt_uint32_t)thread->stack_addr ||
79 80 81 82 83 84
        (rt_uint32_t)thread->sp >
        (rt_uint32_t)thread->stack_addr + (rt_uint32_t)thread->stack_size)
    {
        rt_uint32_t level;

        rt_kprintf("thread:%s stack overflow\n", thread->name);
85
#ifdef RT_USING_FINSH
86 87 88 89
        {
            extern long list_thread(void);
            list_thread();
        }
90
#endif
91 92 93 94 95 96 97 98
        level = rt_hw_interrupt_disable();
        while (level);
    }
    else if ((rt_uint32_t)thread->sp <= ((rt_uint32_t)thread->stack_addr + 32))
    {
        rt_kprintf("warning: %s stack is close to end of stack address.\n",
                   thread->name);
    }
99 100 101 102 103
}
#endif

/**
 * @ingroup SystemInit
B
bernard.xiong@gmail.com 已提交
104
 * This function will initialize the system scheduler
105 106 107
 */
void rt_system_scheduler_init(void)
{
108
    register rt_base_t offset;
109

110
    rt_scheduler_lock_nest = 0;
111

112
    RT_DEBUG_LOG(RT_DEBUG_SCHEDULER, ("start scheduler: max priority 0x%02x\n",
113
                                      RT_THREAD_PRIORITY_MAX));
B
bernard.xiong@gmail.com 已提交
114

115 116 117 118
    for (offset = 0; offset < RT_THREAD_PRIORITY_MAX; offset ++)
    {
        rt_list_init(&rt_thread_priority_table[offset]);
    }
119

120 121
    rt_current_priority = RT_THREAD_PRIORITY_MAX - 1;
    rt_current_thread = RT_NULL;
122

123 124
    /* initialize ready priority group */
    rt_thread_ready_priority_group = 0;
125 126

#if RT_THREAD_PRIORITY_MAX > 32
127 128
    /* initialize ready table */
    rt_memset(rt_thread_ready_table, 0, sizeof(rt_thread_ready_table));
129 130
#endif

131 132
    /* initialize thread defunct */
    rt_list_init(&rt_thread_defunct);
133 134 135
}

/**
136
 * @ingroup SystemInit
137 138 139
 * This function will startup scheduler. It will select one thread
 * with the highest priority level, then switch to it.
 */
D
dzzxzz 已提交
140
void rt_system_scheduler_start(void)
141
{
142 143
    register struct rt_thread *to_thread;
    register rt_ubase_t highest_ready_priority;
144

145
#if RT_THREAD_PRIORITY_MAX > 32
146
    register rt_ubase_t number;
147

148 149
    number = __rt_ffs(rt_thread_ready_priority_group) - 1;
    highest_ready_priority = (number << 3) + __rt_ffs(rt_thread_ready_table[number]) - 1;
150
#else
151
    highest_ready_priority = __rt_ffs(rt_thread_ready_priority_group) - 1;
152 153
#endif

154 155 156 157
    /* get switch to thread */
    to_thread = rt_list_entry(rt_thread_priority_table[highest_ready_priority].next,
                              struct rt_thread,
                              tlist);
158

159
    rt_current_thread = to_thread;
160

161 162
    /* switch to new thread */
    rt_hw_context_switch_to((rt_uint32_t)&to_thread->sp);
163

164
    /* never come back */
165 166 167 168 169
}

/**
 * @addtogroup Thread
 */
D
dzzxzz 已提交
170

D
dogandog 已提交
171
/**@{*/
172 173 174 175 176

/**
 * This function will perform one schedule. It will select one thread
 * with the highest priority level, then switch to it.
 */
D
dzzxzz 已提交
177
void rt_schedule(void)
178
{
179 180 181
    rt_base_t level;
    struct rt_thread *to_thread;
    struct rt_thread *from_thread;
182

183 184
    /* disable interrupt */
    level = rt_hw_interrupt_disable();
185

186 187 188 189
    /* check the scheduler is enabled or not */
    if (rt_scheduler_lock_nest == 0)
    {
        register rt_ubase_t highest_ready_priority;
190

191 192
#if RT_THREAD_PRIORITY_MAX <= 32
        highest_ready_priority = __rt_ffs(rt_thread_ready_priority_group) - 1;
193
#else
194
        register rt_ubase_t number;
195

196 197
        number = __rt_ffs(rt_thread_ready_priority_group) - 1;
        highest_ready_priority = (number << 3) + __rt_ffs(rt_thread_ready_table[number]) - 1;
198
#endif
199

200 201 202 203 204 205 206 207
        /* get switch to thread */
        to_thread = rt_list_entry(rt_thread_priority_table[highest_ready_priority].next,
                                  struct rt_thread,
                                  tlist);

        /* if the destination thread is not the same as current thread */
        if (to_thread != rt_current_thread)
        {
208
            rt_current_priority = (rt_uint8_t)highest_ready_priority;
209 210 211 212 213 214 215
            from_thread         = rt_current_thread;
            rt_current_thread   = to_thread;

            RT_OBJECT_HOOK_CALL(rt_scheduler_hook, (from_thread, to_thread));

            /* switch to new thread */
            RT_DEBUG_LOG(RT_DEBUG_SCHEDULER,
G
Grissiom 已提交
216 217 218 219 220 221
                         ("[%d]switch to priority#%d "
                          "thread:%.*s(sp:0x%p), "
                          "from thread:%.*s(sp: 0x%p)\n",
                          rt_interrupt_nest, highest_ready_priority,
                          RT_NAME_MAX, to_thread->name, to_thread->sp,
                          RT_NAME_MAX, from_thread->name, from_thread->sp));
222

223
#ifdef RT_USING_OVERFLOW_CHECK
224
            _rt_scheduler_stack_check(to_thread);
225 226
#endif

227 228
            if (rt_interrupt_nest == 0)
            {
B
bernard 已提交
229 230
                extern void rt_thread_handle_sig(rt_bool_t clean_state);

231 232
                rt_hw_context_switch((rt_uint32_t)&from_thread->sp,
                                     (rt_uint32_t)&to_thread->sp);
B
bernard 已提交
233 234 235 236 237 238 239 240

                /* enable interrupt */
                rt_hw_interrupt_enable(level);

#ifdef RT_USING_SIGNALS
                /* check signal status */
                rt_thread_handle_sig(RT_TRUE);
#endif
241 242 243 244 245 246 247
            }
            else
            {
                RT_DEBUG_LOG(RT_DEBUG_SCHEDULER, ("switch in interrupt\n"));

                rt_hw_context_switch_interrupt((rt_uint32_t)&from_thread->sp,
                                               (rt_uint32_t)&to_thread->sp);
B
bernard 已提交
248 249
                /* enable interrupt */
                rt_hw_interrupt_enable(level);
250 251
            }
        }
252
        else
B
bernard 已提交
253 254 255 256
        {
            /* enable interrupt */
            rt_hw_interrupt_enable(level);
        }
257
    }
armink_ztl's avatar
armink_ztl 已提交
258 259 260 261 262
    else
    {
        /* enable interrupt */
        rt_hw_interrupt_enable(level);
    }
263 264
}

B
bernard.xiong@gmail.com 已提交
265
/*
266 267 268 269 270 271
 * This function will insert a thread to system ready queue. The state of
 * thread will be set as READY and remove from suspend queue.
 *
 * @param thread the thread to be inserted
 * @note Please do not invoke this function in user application.
 */
D
dzzxzz 已提交
272
void rt_schedule_insert_thread(struct rt_thread *thread)
273
{
274
    register rt_base_t temp;
275

276
    RT_ASSERT(thread != RT_NULL);
277

278 279
    /* disable interrupt */
    temp = rt_hw_interrupt_disable();
280

281
    /* change stat */
282
    thread->stat = RT_THREAD_READY | (thread->stat & ~RT_THREAD_STAT_MASK);
283

284 285 286
    /* insert thread to ready list */
    rt_list_insert_before(&(rt_thread_priority_table[thread->current_priority]),
                          &(thread->tlist));
287

288
    /* set priority mask */
289
#if RT_THREAD_PRIORITY_MAX <= 32
G
Grissiom 已提交
290 291
    RT_DEBUG_LOG(RT_DEBUG_SCHEDULER, ("insert thread[%.*s], the priority: %d\n",
                                      RT_NAME_MAX, thread->name, thread->current_priority));
292
#else
293
    RT_DEBUG_LOG(RT_DEBUG_SCHEDULER,
G
Grissiom 已提交
294 295
                 ("insert thread[%.*s], the priority: %d 0x%x %d\n",
                  RT_NAME_MAX,
296 297 298 299
                  thread->name,
                  thread->number,
                  thread->number_mask,
                  thread->high_mask));
300 301 302
#endif

#if RT_THREAD_PRIORITY_MAX > 32
303
    rt_thread_ready_table[thread->number] |= thread->high_mask;
304
#endif
305
    rt_thread_ready_priority_group |= thread->number_mask;
306

307 308
    /* enable interrupt */
    rt_hw_interrupt_enable(temp);
309 310
}

B
bernard.xiong@gmail.com 已提交
311
/*
312 313 314 315 316 317
 * This function will remove a thread from system ready queue.
 *
 * @param thread the thread to be removed
 *
 * @note Please do not invoke this function in user application.
 */
D
dzzxzz 已提交
318
void rt_schedule_remove_thread(struct rt_thread *thread)
319
{
320
    register rt_base_t temp;
321

322
    RT_ASSERT(thread != RT_NULL);
323

324 325
    /* disable interrupt */
    temp = rt_hw_interrupt_disable();
326

327
#if RT_THREAD_PRIORITY_MAX <= 32
G
Grissiom 已提交
328 329 330
    RT_DEBUG_LOG(RT_DEBUG_SCHEDULER, ("remove thread[%.*s], the priority: %d\n",
                                      RT_NAME_MAX, thread->name,
                                      thread->current_priority));
331
#else
332
    RT_DEBUG_LOG(RT_DEBUG_SCHEDULER,
G
Grissiom 已提交
333 334
                 ("remove thread[%.*s], the priority: %d 0x%x %d\n",
                  RT_NAME_MAX,
335 336 337 338
                  thread->name,
                  thread->number,
                  thread->number_mask,
                  thread->high_mask));
339 340
#endif

341 342 343 344
    /* remove thread from ready list */
    rt_list_remove(&(thread->tlist));
    if (rt_list_isempty(&(rt_thread_priority_table[thread->current_priority])))
    {
345
#if RT_THREAD_PRIORITY_MAX > 32
346 347 348 349 350
        rt_thread_ready_table[thread->number] &= ~thread->high_mask;
        if (rt_thread_ready_table[thread->number] == 0)
        {
            rt_thread_ready_priority_group &= ~thread->number_mask;
        }
351
#else
352
        rt_thread_ready_priority_group &= ~thread->number_mask;
353
#endif
354
    }
355

356 357
    /* enable interrupt */
    rt_hw_interrupt_enable(temp);
358 359 360 361 362
}

/**
 * This function will lock the thread scheduler.
 */
D
dzzxzz 已提交
363
void rt_enter_critical(void)
364
{
365
    register rt_base_t level;
366

367 368
    /* disable interrupt */
    level = rt_hw_interrupt_disable();
369

370 371 372 373 374
    /*
     * the maximal number of nest is RT_UINT16_MAX, which is big
     * enough and does not check here
     */
    rt_scheduler_lock_nest ++;
375

376 377
    /* enable interrupt */
    rt_hw_interrupt_enable(level);
378
}
B
Bernard Xiong 已提交
379
RTM_EXPORT(rt_enter_critical);
380 381 382 383

/**
 * This function will unlock the thread scheduler.
 */
D
dzzxzz 已提交
384
void rt_exit_critical(void)
385
{
386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405
    register rt_base_t level;

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

    rt_scheduler_lock_nest --;

    if (rt_scheduler_lock_nest <= 0)
    {
        rt_scheduler_lock_nest = 0;
        /* enable interrupt */
        rt_hw_interrupt_enable(level);

        rt_schedule();
    }
    else
    {
        /* enable interrupt */
        rt_hw_interrupt_enable(level);
    }
406
}
B
Bernard Xiong 已提交
407
RTM_EXPORT(rt_exit_critical);
408

409 410 411 412 413 414 415 416 417
/**
 * Get the scheduler lock level
 *
 * @return the level of the scheduler lock. 0 means unlocked.
 */
rt_uint16_t rt_critical_level(void)
{
    return rt_scheduler_lock_nest;
}
B
Bernard Xiong 已提交
418
RTM_EXPORT(rt_critical_level);
D
dogandog 已提交
419
/**@}*/
420