scheduler.c 11.9 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) != '#' ||
B
Bernard Xiong 已提交
78 79 80
        (rt_ubase_t)thread->sp <= (rt_ubase_t)thread->stack_addr ||
        (rt_ubase_t)thread->sp >
        (rt_ubase_t)thread->stack_addr + (rt_ubase_t)thread->stack_size)
81
    {
B
Bernard Xiong 已提交
82
        rt_ubase_t level;
83 84

        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
        level = rt_hw_interrupt_disable();
        while (level);
    }
94
#if defined(ARCH_CPU_STACK_GROWS_UPWARD)
95
    else if ((rt_ubase_t)thread->sp > ((rt_ubase_t)thread->stack_addr + thread->stack_size))
96 97 98 99 100
    {
        rt_kprintf("warning: %s stack is close to the top of stack address.\n",
                   thread->name);
    }
#else
B
Bernard Xiong 已提交
101
    else if ((rt_ubase_t)thread->sp <= ((rt_ubase_t)thread->stack_addr + 32))
102
    {
103
        rt_kprintf("warning: %s stack is close to the bottom of stack address.\n",
104 105
                   thread->name);
    }
106
#endif
107 108 109 110 111
}
#endif

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

118
    rt_scheduler_lock_nest = 0;
119

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

123 124 125 126
    for (offset = 0; offset < RT_THREAD_PRIORITY_MAX; offset ++)
    {
        rt_list_init(&rt_thread_priority_table[offset]);
    }
127

128 129
    rt_current_priority = RT_THREAD_PRIORITY_MAX - 1;
    rt_current_thread = RT_NULL;
130

131 132
    /* initialize ready priority group */
    rt_thread_ready_priority_group = 0;
133 134

#if RT_THREAD_PRIORITY_MAX > 32
135 136
    /* initialize ready table */
    rt_memset(rt_thread_ready_table, 0, sizeof(rt_thread_ready_table));
137 138
#endif

139 140
    /* initialize thread defunct */
    rt_list_init(&rt_thread_defunct);
141 142 143
}

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

153
#if RT_THREAD_PRIORITY_MAX > 32
154
    register rt_ubase_t number;
155

156 157
    number = __rt_ffs(rt_thread_ready_priority_group) - 1;
    highest_ready_priority = (number << 3) + __rt_ffs(rt_thread_ready_table[number]) - 1;
158
#else
159
    highest_ready_priority = __rt_ffs(rt_thread_ready_priority_group) - 1;
160 161
#endif

162 163 164 165
    /* get switch to thread */
    to_thread = rt_list_entry(rt_thread_priority_table[highest_ready_priority].next,
                              struct rt_thread,
                              tlist);
166

167
    rt_current_thread = to_thread;
168

169
    /* switch to new thread */
B
Bernard Xiong 已提交
170
    rt_hw_context_switch_to((rt_ubase_t)&to_thread->sp);
171

172
    /* never come back */
173 174 175 176 177
}

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

D
dogandog 已提交
179
/**@{*/
180 181 182 183 184

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

191 192
    /* disable interrupt */
    level = rt_hw_interrupt_disable();
193

194 195 196 197
    /* check the scheduler is enabled or not */
    if (rt_scheduler_lock_nest == 0)
    {
        register rt_ubase_t highest_ready_priority;
198

199 200
#if RT_THREAD_PRIORITY_MAX <= 32
        highest_ready_priority = __rt_ffs(rt_thread_ready_priority_group) - 1;
201
#else
202
        register rt_ubase_t number;
203

204 205
        number = __rt_ffs(rt_thread_ready_priority_group) - 1;
        highest_ready_priority = (number << 3) + __rt_ffs(rt_thread_ready_table[number]) - 1;
206
#endif
207

208 209 210 211 212 213 214 215
        /* 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)
        {
216
            rt_current_priority = (rt_uint8_t)highest_ready_priority;
217 218 219 220 221 222 223
            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 已提交
224
                         ("[%d]switch to priority#%d "
B
Bernard Xiong 已提交
225 226
                          "thread:%.*s(sp:0x%08x), "
                          "from thread:%.*s(sp: 0x%08x)\n",
G
Grissiom 已提交
227 228 229
                          rt_interrupt_nest, highest_ready_priority,
                          RT_NAME_MAX, to_thread->name, to_thread->sp,
                          RT_NAME_MAX, from_thread->name, from_thread->sp));
230

231
#ifdef RT_USING_OVERFLOW_CHECK
232
            _rt_scheduler_stack_check(to_thread);
233 234
#endif

235 236
            if (rt_interrupt_nest == 0)
            {
B
bernard 已提交
237 238
                extern void rt_thread_handle_sig(rt_bool_t clean_state);

B
Bernard Xiong 已提交
239 240
                rt_hw_context_switch((rt_ubase_t)&from_thread->sp,
                                     (rt_ubase_t)&to_thread->sp);
B
bernard 已提交
241 242 243 244 245 246 247 248

                /* enable interrupt */
                rt_hw_interrupt_enable(level);

#ifdef RT_USING_SIGNALS
                /* check signal status */
                rt_thread_handle_sig(RT_TRUE);
#endif
249 250 251 252 253
            }
            else
            {
                RT_DEBUG_LOG(RT_DEBUG_SCHEDULER, ("switch in interrupt\n"));

B
Bernard Xiong 已提交
254 255
                rt_hw_context_switch_interrupt((rt_ubase_t)&from_thread->sp,
                                               (rt_ubase_t)&to_thread->sp);
B
bernard 已提交
256 257
                /* enable interrupt */
                rt_hw_interrupt_enable(level);
258 259
            }
        }
260
        else
B
bernard 已提交
261 262 263 264
        {
            /* enable interrupt */
            rt_hw_interrupt_enable(level);
        }
265
    }
armink_ztl's avatar
armink_ztl 已提交
266 267 268 269 270
    else
    {
        /* enable interrupt */
        rt_hw_interrupt_enable(level);
    }
271 272
}

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

284
    RT_ASSERT(thread != RT_NULL);
285

286 287
    /* disable interrupt */
    temp = rt_hw_interrupt_disable();
288

289
    /* change stat */
290
    thread->stat = RT_THREAD_READY | (thread->stat & ~RT_THREAD_STAT_MASK);
291

292 293 294
    /* insert thread to ready list */
    rt_list_insert_before(&(rt_thread_priority_table[thread->current_priority]),
                          &(thread->tlist));
295

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

#if RT_THREAD_PRIORITY_MAX > 32
311
    rt_thread_ready_table[thread->number] |= thread->high_mask;
312
#endif
313
    rt_thread_ready_priority_group |= thread->number_mask;
314

315 316
    /* enable interrupt */
    rt_hw_interrupt_enable(temp);
317 318
}

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

330
    RT_ASSERT(thread != RT_NULL);
331

332 333
    /* disable interrupt */
    temp = rt_hw_interrupt_disable();
334

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

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

364 365
    /* enable interrupt */
    rt_hw_interrupt_enable(temp);
366 367 368 369 370
}

/**
 * This function will lock the thread scheduler.
 */
D
dzzxzz 已提交
371
void rt_enter_critical(void)
372
{
373
    register rt_base_t level;
374

375 376
    /* disable interrupt */
    level = rt_hw_interrupt_disable();
377

378 379 380 381 382
    /*
     * the maximal number of nest is RT_UINT16_MAX, which is big
     * enough and does not check here
     */
    rt_scheduler_lock_nest ++;
383

384 385
    /* enable interrupt */
    rt_hw_interrupt_enable(level);
386
}
B
Bernard Xiong 已提交
387
RTM_EXPORT(rt_enter_critical);
388 389 390 391

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

417 418 419 420 421 422 423 424 425
/**
 * 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 已提交
426
RTM_EXPORT(rt_critical_level);
D
dogandog 已提交
427
/**@}*/
428