scheduler.c 11.7 KB
Newer Older
1
/*
2
 * Copyright (c) 2006-2018, RT-Thread Development Team
B
Bernard Xiong 已提交
3
 *
4 5 6 7 8
 * SPDX-License-Identifier: Apache-2.0
 */

/*
 * File      : scheduler.c
9 10 11 12 13 14
 *
 * 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
15 16
 * 2006-05-27     Bernard      fix the scheduler algorthm for same priority
 *                             thread schedule
17 18 19 20 21
 * 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
22
 * 2010-04-11     yi.qiu       add module feature
23
 * 2010-07-13     Bernard      fix the maximal number of rt_scheduler_lock_nest
24
 *                             issue found by kuronca
25
 * 2010-12-13     Bernard      add defunct list initialization even if not use heap.
B
bernard.xiong@gmail.com 已提交
26
 * 2011-05-10     Bernard      clean scheduler debug log.
27
 * 2013-12-21     Grissiom     add rt_critical_level
28 29 30 31 32 33
 */

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

static rt_int16_t rt_scheduler_lock_nest;
34
extern volatile rt_uint8_t rt_interrupt_nest;
35 36

rt_list_t rt_thread_priority_table[RT_THREAD_PRIORITY_MAX];
D
dzzxzz 已提交
37
struct rt_thread *rt_current_thread;
38 39 40 41

rt_uint8_t rt_current_priority;

#if RT_THREAD_PRIORITY_MAX > 32
42
/* Maximum priority level, 256 */
43 44 45
rt_uint32_t rt_thread_ready_priority_group;
rt_uint8_t rt_thread_ready_table[32];
#else
46
/* Maximum priority level, 32 */
47 48 49 50 51 52
rt_uint32_t rt_thread_ready_priority_group;
#endif

rt_list_t rt_thread_defunct;

#ifdef RT_USING_HOOK
D
dzzxzz 已提交
53
static void (*rt_scheduler_hook)(struct rt_thread *from, struct rt_thread *to);
54 55 56 57

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

D
dogandog 已提交
59
/**@{*/
60 61 62 63 64 65 66

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

D
dogandog 已提交
73
/**@}*/
74 75 76
#endif

#ifdef RT_USING_OVERFLOW_CHECK
D
dzzxzz 已提交
77
static void _rt_scheduler_stack_check(struct rt_thread *thread)
78
{
79 80
    RT_ASSERT(thread != RT_NULL);

A
Aubr.Cool 已提交
81
    if (*((rt_uint8_t *)thread->stack_addr) != '#' ||
82
        (rt_uint32_t)thread->sp <= (rt_uint32_t)thread->stack_addr ||
83 84 85 86 87 88
        (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);
89
#ifdef RT_USING_FINSH
90 91 92 93
        {
            extern long list_thread(void);
            list_thread();
        }
94
#endif
95 96 97 98 99 100 101 102
        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);
    }
103 104 105 106 107
}
#endif

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

114
    rt_scheduler_lock_nest = 0;
115

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

119 120 121 122
    for (offset = 0; offset < RT_THREAD_PRIORITY_MAX; offset ++)
    {
        rt_list_init(&rt_thread_priority_table[offset]);
    }
123

124 125
    rt_current_priority = RT_THREAD_PRIORITY_MAX - 1;
    rt_current_thread = RT_NULL;
126

127 128
    /* initialize ready priority group */
    rt_thread_ready_priority_group = 0;
129 130

#if RT_THREAD_PRIORITY_MAX > 32
131 132
    /* initialize ready table */
    rt_memset(rt_thread_ready_table, 0, sizeof(rt_thread_ready_table));
133 134
#endif

135 136
    /* initialize thread defunct */
    rt_list_init(&rt_thread_defunct);
137 138 139
}

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

149
#if RT_THREAD_PRIORITY_MAX > 32
150
    register rt_ubase_t number;
151

152 153
    number = __rt_ffs(rt_thread_ready_priority_group) - 1;
    highest_ready_priority = (number << 3) + __rt_ffs(rt_thread_ready_table[number]) - 1;
154
#else
155
    highest_ready_priority = __rt_ffs(rt_thread_ready_priority_group) - 1;
156 157
#endif

158 159 160 161
    /* get switch to thread */
    to_thread = rt_list_entry(rt_thread_priority_table[highest_ready_priority].next,
                              struct rt_thread,
                              tlist);
162

163
    rt_current_thread = to_thread;
164

165 166
    /* switch to new thread */
    rt_hw_context_switch_to((rt_uint32_t)&to_thread->sp);
167

168
    /* never come back */
169 170 171 172 173
}

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

D
dogandog 已提交
175
/**@{*/
176 177 178 179 180

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

187 188
    /* disable interrupt */
    level = rt_hw_interrupt_disable();
189

190 191 192 193
    /* check the scheduler is enabled or not */
    if (rt_scheduler_lock_nest == 0)
    {
        register rt_ubase_t highest_ready_priority;
194

195 196
#if RT_THREAD_PRIORITY_MAX <= 32
        highest_ready_priority = __rt_ffs(rt_thread_ready_priority_group) - 1;
197
#else
198
        register rt_ubase_t number;
199

200 201
        number = __rt_ffs(rt_thread_ready_priority_group) - 1;
        highest_ready_priority = (number << 3) + __rt_ffs(rt_thread_ready_table[number]) - 1;
202
#endif
203

204 205 206 207 208 209 210 211
        /* 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)
        {
212
            rt_current_priority = (rt_uint8_t)highest_ready_priority;
213 214 215 216 217 218 219
            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 已提交
220 221 222 223 224 225
                         ("[%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));
226

227
#ifdef RT_USING_OVERFLOW_CHECK
228
            _rt_scheduler_stack_check(to_thread);
229 230
#endif

231 232
            if (rt_interrupt_nest == 0)
            {
B
bernard 已提交
233 234
                extern void rt_thread_handle_sig(rt_bool_t clean_state);

235 236
                rt_hw_context_switch((rt_uint32_t)&from_thread->sp,
                                     (rt_uint32_t)&to_thread->sp);
B
bernard 已提交
237 238 239 240 241 242 243 244

                /* enable interrupt */
                rt_hw_interrupt_enable(level);

#ifdef RT_USING_SIGNALS
                /* check signal status */
                rt_thread_handle_sig(RT_TRUE);
#endif
245 246 247 248 249 250 251
            }
            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 已提交
252 253
                /* enable interrupt */
                rt_hw_interrupt_enable(level);
254 255
            }
        }
256
        else
B
bernard 已提交
257 258 259 260
        {
            /* enable interrupt */
            rt_hw_interrupt_enable(level);
        }
261
    }
armink_ztl's avatar
armink_ztl 已提交
262 263 264 265 266
    else
    {
        /* enable interrupt */
        rt_hw_interrupt_enable(level);
    }
267 268
}

B
bernard.xiong@gmail.com 已提交
269
/*
270 271 272 273 274 275
 * 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 已提交
276
void rt_schedule_insert_thread(struct rt_thread *thread)
277
{
278
    register rt_base_t temp;
279

280
    RT_ASSERT(thread != RT_NULL);
281

282 283
    /* disable interrupt */
    temp = rt_hw_interrupt_disable();
284

285
    /* change stat */
286
    thread->stat = RT_THREAD_READY | (thread->stat & ~RT_THREAD_STAT_MASK);
287

288 289 290
    /* insert thread to ready list */
    rt_list_insert_before(&(rt_thread_priority_table[thread->current_priority]),
                          &(thread->tlist));
291

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

#if RT_THREAD_PRIORITY_MAX > 32
307
    rt_thread_ready_table[thread->number] |= thread->high_mask;
308
#endif
309
    rt_thread_ready_priority_group |= thread->number_mask;
310

311 312
    /* enable interrupt */
    rt_hw_interrupt_enable(temp);
313 314
}

B
bernard.xiong@gmail.com 已提交
315
/*
316 317 318 319 320 321
 * 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 已提交
322
void rt_schedule_remove_thread(struct rt_thread *thread)
323
{
324
    register rt_base_t temp;
325

326
    RT_ASSERT(thread != RT_NULL);
327

328 329
    /* disable interrupt */
    temp = rt_hw_interrupt_disable();
330

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

345 346 347 348
    /* remove thread from ready list */
    rt_list_remove(&(thread->tlist));
    if (rt_list_isempty(&(rt_thread_priority_table[thread->current_priority])))
    {
349
#if RT_THREAD_PRIORITY_MAX > 32
350 351 352 353 354
        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;
        }
355
#else
356
        rt_thread_ready_priority_group &= ~thread->number_mask;
357
#endif
358
    }
359

360 361
    /* enable interrupt */
    rt_hw_interrupt_enable(temp);
362 363 364 365 366
}

/**
 * This function will lock the thread scheduler.
 */
D
dzzxzz 已提交
367
void rt_enter_critical(void)
368
{
369
    register rt_base_t level;
370

371 372
    /* disable interrupt */
    level = rt_hw_interrupt_disable();
373

374 375 376 377 378
    /*
     * the maximal number of nest is RT_UINT16_MAX, which is big
     * enough and does not check here
     */
    rt_scheduler_lock_nest ++;
379

380 381
    /* enable interrupt */
    rt_hw_interrupt_enable(level);
382
}
B
Bernard Xiong 已提交
383
RTM_EXPORT(rt_enter_critical);
384 385 386 387

/**
 * This function will unlock the thread scheduler.
 */
D
dzzxzz 已提交
388
void rt_exit_critical(void)
389
{
390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409
    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);
    }
410
}
B
Bernard Xiong 已提交
411
RTM_EXPORT(rt_exit_critical);
412

413 414 415 416 417 418 419 420 421
/**
 * 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 已提交
422
RTM_EXPORT(rt_critical_level);
D
dogandog 已提交
423
/**@}*/
424