thread.c 28.3 KB
Newer Older
1
/*
mysterywolf's avatar
mysterywolf 已提交
2
 * Copyright (c) 2006-2021, RT-Thread Development Team
B
Bernard Xiong 已提交
3
 *
4
 * SPDX-License-Identifier: Apache-2.0
5 6 7 8 9
 *
 * Change Logs:
 * Date           Author       Notes
 * 2006-03-28     Bernard      first version
 * 2006-04-29     Bernard      implement thread timer
10 11 12 13 14
 * 2006-04-30     Bernard      added THREAD_DEBUG
 * 2006-05-27     Bernard      fixed the rt_thread_yield bug
 * 2006-06-03     Bernard      fixed the thread timer init bug
 * 2006-08-10     Bernard      fixed the timer bug in thread_sleep
 * 2006-09-03     Bernard      changed rt_timer_delete to rt_timer_detach
15
 * 2006-09-03     Bernard      implement rt_thread_detach
16
 * 2008-02-16     Bernard      fixed the rt_thread_timeout bug
17 18
 * 2010-03-21     Bernard      change the errno of rt_thread_delay/sleep to
 *                             RT_EOK.
19
 * 2010-11-10     Bernard      add cleanup callback function in thread exit.
20 21
 * 2011-09-01     Bernard      fixed rt_thread_exit issue when the current
 *                             thread preempted, which reported by Jiaxing Lee.
22
 * 2011-09-08     Bernard      fixed the scheduling issue in rt_thread_startup.
23
 * 2012-12-29     Bernard      fixed compiling warning.
24
 * 2016-08-09     ArdaFu       add thread suspend and resume hook.
25
 * 2017-04-10     armink       fixed the rt_thread_delete and rt_thread_detach
S
shaojinchun 已提交
26 27 28
 *                             bug when thread has not startup.
 * 2018-11-22     Jesven       yield is same to rt_schedule
 *                             add support for tasks bound to cpu
29
 * 2021-02-24     Meco Man     rearrange rt_thread_control() - schedule the thread when close it
30
 * 2021-11-15     THEWON       Remove duplicate work between idle and _thread_exit
mysterywolf's avatar
mysterywolf 已提交
31
 * 2021-12-27     Meco Man     remove .init_priority
32
 * 2022-01-07     Gabriel      Moving __on_rt_xxxxx_hook to thread.c
33
 * 2022-01-24     THEWON       let rt_thread_sleep return thread->error when using signal
34 35 36
 */

#include <rthw.h>
S
shaojinchun 已提交
37
#include <rtthread.h>
38
#include <stddef.h>
39

40 41 42 43 44 45 46 47 48 49 50
#ifndef __on_rt_thread_inited_hook
    #define __on_rt_thread_inited_hook(thread)      __ON_HOOK_ARGS(rt_thread_inited_hook, (thread))
#endif
#ifndef __on_rt_thread_suspend_hook
    #define __on_rt_thread_suspend_hook(thread)     __ON_HOOK_ARGS(rt_thread_suspend_hook, (thread))
#endif
#ifndef __on_rt_thread_resume_hook
    #define __on_rt_thread_resume_hook(thread)      __ON_HOOK_ARGS(rt_thread_resume_hook, (thread))
#endif

#if defined(RT_USING_HOOK) && defined(RT_HOOK_USING_FUNC_PTR)
51
static void (*rt_thread_suspend_hook)(rt_thread_t thread);
B
Bernard Xiong 已提交
52 53 54
static void (*rt_thread_resume_hook) (rt_thread_t thread);
static void (*rt_thread_inited_hook) (rt_thread_t thread);

55
/**
56
 * @brief   This function sets a hook function when the system suspend a thread.
57
 *
L
liukangcc 已提交
58
 * @note    The hook function must be simple and never be blocked or suspend.
59
 *
L
liukangcc 已提交
60
 * @param   hook is the specified hook function.
61 62 63 64 65
 */
void rt_thread_suspend_sethook(void (*hook)(rt_thread_t thread))
{
    rt_thread_suspend_hook = hook;
}
B
Bernard Xiong 已提交
66

67
/**
68
 * @brief   This function sets a hook function when the system resume a thread.
69
 *
L
liukangcc 已提交
70
 * @note    The hook function must be simple and never be blocked or suspend.
71
 *
L
liukangcc 已提交
72
 * @param   hook is the specified hook function.
73 74 75 76 77
 */
void rt_thread_resume_sethook(void (*hook)(rt_thread_t thread))
{
    rt_thread_resume_hook = hook;
}
B
Bernard Xiong 已提交
78 79

/**
80
 * @brief   This function sets a hook function when a thread is initialized.
B
Bernard Xiong 已提交
81
 *
L
liukangcc 已提交
82
 * @param   hook is the specified hook function.
B
Bernard Xiong 已提交
83 84 85
 */
void rt_thread_inited_sethook(void (*hook)(rt_thread_t thread))
{
86
    rt_thread_inited_hook = hook;
B
Bernard Xiong 已提交
87
}
mysterywolf's avatar
mysterywolf 已提交
88
#endif /* defined(RT_USING_HOOK) && defined(RT_HOOK_USING_FUNC_PTR) */
89

mysterywolf's avatar
mysterywolf 已提交
90
static void _thread_exit(void)
91
{
92
    struct rt_thread *thread;
93
    rt_base_t level;
94 95

    /* get current thread */
S
shaojinchun 已提交
96
    thread = rt_thread_self();
97 98 99 100 101 102 103 104

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

    /* remove from schedule */
    rt_schedule_remove_thread(thread);

    /* remove it from timer list */
105
    rt_timer_detach(&thread->thread_timer);
106

107 108
    /* change stat */
    thread->stat = RT_THREAD_CLOSE;
109

110 111
    /* insert to defunct thread list */
    rt_thread_defunct_enqueue(thread);
112 113 114

    /* enable interrupt */
    rt_hw_interrupt_enable(level);
115 116 117

    /* switch to next task */
    rt_schedule();
118
}
119

120 121 122 123 124 125 126 127 128
/**
 * @brief   This function is the timeout function for thread, normally which is invoked
 *          when thread is timeout to wait some resource.
 *
 * @param   parameter is the parameter of thread timeout function
 */
static void _thread_timeout(void *parameter)
{
    struct rt_thread *thread;
129
    rt_base_t level;
130 131 132

    thread = (struct rt_thread *)parameter;

mysterywolf's avatar
mysterywolf 已提交
133
    /* parameter check */
134 135 136 137 138
    RT_ASSERT(thread != RT_NULL);
    RT_ASSERT((thread->stat & RT_THREAD_STAT_MASK) == RT_THREAD_SUSPEND);
    RT_ASSERT(rt_object_get_type((rt_object_t)thread) == RT_Object_Class_Thread);

    /* disable interrupt */
139
    level = rt_hw_interrupt_disable();
140 141 142 143 144 145 146 147 148 149 150

    /* set error number */
    thread->error = -RT_ETIMEOUT;

    /* remove from suspend list */
    rt_list_remove(&(thread->tlist));

    /* insert to schedule ready list */
    rt_schedule_insert_thread(thread);

    /* enable interrupt */
151
    rt_hw_interrupt_enable(level);
152 153 154 155 156

    /* do schedule */
    rt_schedule();
}

mysterywolf's avatar
mysterywolf 已提交
157 158 159 160 161 162 163 164
static rt_err_t _thread_init(struct rt_thread *thread,
                             const char       *name,
                             void (*entry)(void *parameter),
                             void             *parameter,
                             void             *stack_start,
                             rt_uint32_t       stack_size,
                             rt_uint8_t        priority,
                             rt_uint32_t       tick)
165
{
166 167 168 169 170 171 172 173
    /* init thread list */
    rt_list_init(&(thread->tlist));

    thread->entry = (void *)entry;
    thread->parameter = parameter;

    /* stack init */
    thread->stack_addr = stack_start;
W
weety 已提交
174
    thread->stack_size = stack_size;
175 176 177

    /* init thread stack */
    rt_memset(thread->stack_addr, '#', thread->stack_size);
178 179 180
#ifdef ARCH_CPU_STACK_GROWS_UPWARD
    thread->sp = (void *)rt_hw_stack_init(thread->entry, thread->parameter,
                                          (void *)((char *)thread->stack_addr),
mysterywolf's avatar
mysterywolf 已提交
181
                                          (void *)_thread_exit);
182
#else
183
    thread->sp = (void *)rt_hw_stack_init(thread->entry, thread->parameter,
184
                                          (rt_uint8_t *)((char *)thread->stack_addr + thread->stack_size - sizeof(rt_ubase_t)),
mysterywolf's avatar
mysterywolf 已提交
185
                                          (void *)_thread_exit);
186
#endif /* ARCH_CPU_STACK_GROWS_UPWARD */
187 188 189 190 191

    /* priority init */
    RT_ASSERT(priority < RT_THREAD_PRIORITY_MAX);
    thread->current_priority = priority;

192
    thread->number_mask = 0;
193 194 195 196

#ifdef RT_USING_EVENT
    thread->event_set = 0;
    thread->event_info = 0;
mysterywolf's avatar
mysterywolf 已提交
197
#endif /* RT_USING_EVENT */
198

199 200
#if RT_THREAD_PRIORITY_MAX > 32
    thread->number = 0;
B
Bernard Xiong 已提交
201
    thread->high_mask = 0;
202
#endif /* RT_THREAD_PRIORITY_MAX > 32 */
203

204 205 206 207 208 209 210 211
    /* tick init */
    thread->init_tick      = tick;
    thread->remaining_tick = tick;

    /* error and flags */
    thread->error = RT_EOK;
    thread->stat  = RT_THREAD_INIT;

S
shaojinchun 已提交
212 213 214 215 216 217 218 219
#ifdef RT_USING_SMP
    /* not bind on any cpu */
    thread->bind_cpu = RT_CPUS_NR;
    thread->oncpu = RT_CPU_DETACHED;

    /* lock init */
    thread->scheduler_lock_nest = 0;
    thread->cpus_lock_nest = 0;
S
shaojinchun 已提交
220
    thread->critical_lock_nest = 0;
221
#endif /* RT_USING_SMP */
S
shaojinchun 已提交
222

223 224 225 226
    /* initialize cleanup function and user data */
    thread->cleanup   = 0;
    thread->user_data = 0;

B
Bernard Xiong 已提交
227
    /* initialize thread timer */
228 229
    rt_timer_init(&(thread->thread_timer),
                  thread->name,
230
                  _thread_timeout,
231 232 233 234
                  thread,
                  0,
                  RT_TIMER_FLAG_ONE_SHOT);

B
bernard 已提交
235 236 237 238 239
    /* initialize signal */
#ifdef RT_USING_SIGNALS
    thread->sig_mask    = 0x00;
    thread->sig_pending = 0x00;

S
shaojinchun 已提交
240
#ifndef RT_USING_SMP
B
bernard 已提交
241
    thread->sig_ret     = RT_NULL;
242
#endif /* RT_USING_SMP */
B
bernard 已提交
243 244
    thread->sig_vectors = RT_NULL;
    thread->si_list     = RT_NULL;
245
#endif /* RT_USING_SIGNALS */
B
bernard 已提交
246

H
heyuanjie 已提交
247 248
#ifdef RT_USING_LWP
    thread->lwp = RT_NULL;
249
#endif /* RT_USING_LWP */
H
heyuanjie 已提交
250

F
fenghuijie 已提交
251 252
#ifdef RT_USING_CPU_USAGE
    thread->duration_tick = 0;
mysterywolf's avatar
mysterywolf 已提交
253
#endif /* RT_USING_CPU_USAGE */
254 255 256

#ifdef RT_USING_MODULE
    thread->module_id = 0;
mysterywolf's avatar
mysterywolf 已提交
257
#endif /* RT_USING_MODULE */
258

259
    RT_OBJECT_HOOK_CALL(rt_thread_inited_hook, (thread));
B
Bernard Xiong 已提交
260

261
    return RT_EOK;
262 263 264 265 266 267
}

/**
 * @addtogroup Thread
 */

D
dogandog 已提交
268
/**@{*/
269 270

/**
L
liukangcc 已提交
271
 * @brief   This function will initialize a thread. It's used to initialize a
272 273
 *          static thread object.
 *
L
liukangcc 已提交
274
 * @param   thread is the static thread object.
275
 *
L
liukangcc 已提交
276
 * @param   name is the name of thread, which shall be unique.
277
 *
L
liukangcc 已提交
278
 * @param   entry is the entry function of thread.
279
 *
L
liukangcc 已提交
280
 * @param   parameter is the parameter of thread enter function.
281
 *
L
liukangcc 已提交
282
 * @param   stack_start is the start address of thread stack.
283
 *
L
liukangcc 已提交
284
 * @param   stack_size is the size of thread stack.
285
 *
L
liukangcc 已提交
286
 * @param   priority is the priority of thread.
287
 *
L
liukangcc 已提交
288
 * @param   tick is the time slice if there are same priority thread.
289
 *
L
liukangcc 已提交
290 291
 * @return  Return the operation status. If the return value is RT_EOK, the function is successfully executed.
 *          If the return value is any other values, it means this operation failed.
292
 */
D
dzzxzz 已提交
293
rt_err_t rt_thread_init(struct rt_thread *thread,
294 295 296 297 298 299 300
                        const char       *name,
                        void (*entry)(void *parameter),
                        void             *parameter,
                        void             *stack_start,
                        rt_uint32_t       stack_size,
                        rt_uint8_t        priority,
                        rt_uint32_t       tick)
301
{
mysterywolf's avatar
mysterywolf 已提交
302
    /* parameter check */
303 304 305
    RT_ASSERT(thread != RT_NULL);
    RT_ASSERT(stack_start != RT_NULL);

B
Bernard Xiong 已提交
306
    /* initialize thread object */
307 308
    rt_object_init((rt_object_t)thread, RT_Object_Class_Thread, name);

mysterywolf's avatar
mysterywolf 已提交
309 310 311 312 313 314 315 316
    return _thread_init(thread,
                        name,
                        entry,
                        parameter,
                        stack_start,
                        stack_size,
                        priority,
                        tick);
317
}
318
RTM_EXPORT(rt_thread_init);
319 320

/**
321
 * @brief   This function will return self thread object.
322
 *
L
liukangcc 已提交
323
 * @return  The self thread object.
324
 */
D
dzzxzz 已提交
325
rt_thread_t rt_thread_self(void)
326
{
S
shaojinchun 已提交
327
#ifdef RT_USING_SMP
328
    rt_base_t lock;
S
shaojinchun 已提交
329 330 331 332 333 334 335 336 337
    rt_thread_t self;

    lock = rt_hw_local_irq_disable();
    self = rt_cpu_self()->current_thread;
    rt_hw_local_irq_enable(lock);
    return self;
#else
    extern rt_thread_t rt_current_thread;

338
    return rt_current_thread;
339
#endif /* RT_USING_SMP */
340
}
341
RTM_EXPORT(rt_thread_self);
342 343

/**
344
 * @brief   This function will start a thread and put it to system ready queue.
345
 *
L
liukangcc 已提交
346
 * @param   thread is the thread to be started.
347
 *
L
liukangcc 已提交
348 349
 * @return  Return the operation status. If the return value is RT_EOK, the function is successfully executed.
 *          If the return value is any other values, it means this operation failed.
350
 */
D
dzzxzz 已提交
351
rt_err_t rt_thread_startup(rt_thread_t thread)
352
{
mysterywolf's avatar
mysterywolf 已提交
353
    /* parameter check */
354
    RT_ASSERT(thread != RT_NULL);
B
bernard 已提交
355
    RT_ASSERT((thread->stat & RT_THREAD_STAT_MASK) == RT_THREAD_INIT);
356
    RT_ASSERT(rt_object_get_type((rt_object_t)thread) == RT_Object_Class_Thread);
357

358
    /* calculate priority attribute */
359
#if RT_THREAD_PRIORITY_MAX > 32
360 361 362
    thread->number      = thread->current_priority >> 3;            /* 5bit */
    thread->number_mask = 1L << thread->number;
    thread->high_mask   = 1L << (thread->current_priority & 0x07);  /* 3bit */
363
#else
364
    thread->number_mask = 1L << thread->current_priority;
365
#endif /* RT_THREAD_PRIORITY_MAX > 32 */
366

367
    RT_DEBUG_LOG(RT_DEBUG_THREAD, ("startup a thread:%s with priority:%d\n",
mysterywolf's avatar
mysterywolf 已提交
368
                                   thread->name, thread->current_priority));
369 370 371 372 373 374 375 376 377 378 379
    /* change thread stat */
    thread->stat = RT_THREAD_SUSPEND;
    /* then resume it */
    rt_thread_resume(thread);
    if (rt_thread_self() != RT_NULL)
    {
        /* do a scheduling */
        rt_schedule();
    }

    return RT_EOK;
380
}
381
RTM_EXPORT(rt_thread_startup);
382 383

/**
384
 * @brief   This function will detach a thread. The thread object will be removed from
L
liukangcc 已提交
385
 *          thread queue and detached/deleted from the system object management.
386
 *
L
liukangcc 已提交
387
 * @param   thread is the thread to be deleted.
388
 *
L
liukangcc 已提交
389 390
 * @return  Return the operation status. If the return value is RT_EOK, the function is successfully executed.
 *          If the return value is any other values, it means this operation failed.
391
 */
D
dzzxzz 已提交
392
rt_err_t rt_thread_detach(rt_thread_t thread)
393
{
394
    rt_base_t level;
T
tangyuxin 已提交
395

mysterywolf's avatar
mysterywolf 已提交
396
    /* parameter check */
397
    RT_ASSERT(thread != RT_NULL);
398 399
    RT_ASSERT(rt_object_get_type((rt_object_t)thread) == RT_Object_Class_Thread);
    RT_ASSERT(rt_object_is_systemobject((rt_object_t)thread));
400

C
cliff-cmc 已提交
401
    if ((thread->stat & RT_THREAD_STAT_MASK) == RT_THREAD_CLOSE)
402 403
        return RT_EOK;

B
bernard 已提交
404
    if ((thread->stat & RT_THREAD_STAT_MASK) != RT_THREAD_INIT)
405 406 407 408
    {
        /* remove from schedule */
        rt_schedule_remove_thread(thread);
    }
409

410
    /* disable interrupt */
411
    level = rt_hw_interrupt_disable();
412

413 414
    /* release thread timer */
    rt_timer_detach(&(thread->thread_timer));
415

416 417
    /* change stat */
    thread->stat = RT_THREAD_CLOSE;
418

419 420 421 422
    /* insert to defunct thread list */
    rt_thread_defunct_enqueue(thread);

    /* enable interrupt */
423
    rt_hw_interrupt_enable(level);
T
tangyuxin 已提交
424

425
    return RT_EOK;
426
}
427 428
RTM_EXPORT(rt_thread_detach);

429
#ifdef RT_USING_HEAP
430
/**
431 432 433
 * @brief   This function will create a thread object and allocate thread object memory.
 *          and stack.
 *
L
liukangcc 已提交
434
 * @param   name is the name of thread, which shall be unique.
435
 *
L
liukangcc 已提交
436
 * @param   entry is the entry function of thread.
437
 *
L
liukangcc 已提交
438
 * @param   parameter is the parameter of thread enter function.
439
 *
L
liukangcc 已提交
440
 * @param   stack_size is the size of thread stack.
441
 *
L
liukangcc 已提交
442
 * @param   priority is the priority of thread.
443
 *
L
liukangcc 已提交
444
 * @param   tick is the time slice if there are same priority thread.
445
 *
L
liukangcc 已提交
446 447
 * @return  If the return value is a rt_thread structure pointer, the function is successfully executed.
 *          If the return value is RT_NULL, it means this operation failed.
448 449
 */
rt_thread_t rt_thread_create(const char *name,
450 451 452 453 454
                             void (*entry)(void *parameter),
                             void       *parameter,
                             rt_uint32_t stack_size,
                             rt_uint8_t  priority,
                             rt_uint32_t tick)
455
{
456 457 458 459 460 461 462 463
    struct rt_thread *thread;
    void *stack_start;

    thread = (struct rt_thread *)rt_object_allocate(RT_Object_Class_Thread,
                                                    name);
    if (thread == RT_NULL)
        return RT_NULL;

B
Bernard Xiong 已提交
464
    stack_start = (void *)RT_KERNEL_MALLOC(stack_size);
465 466 467 468 469 470 471 472
    if (stack_start == RT_NULL)
    {
        /* allocate stack failure */
        rt_object_delete((rt_object_t)thread);

        return RT_NULL;
    }

mysterywolf's avatar
mysterywolf 已提交
473
    _thread_init(thread,
mysterywolf's avatar
mysterywolf 已提交
474 475 476 477 478 479 480
                 name,
                 entry,
                 parameter,
                 stack_start,
                 stack_size,
                 priority,
                 tick);
481 482

    return thread;
483
}
484
RTM_EXPORT(rt_thread_create);
485

486
/**
487 488
 * @brief   This function will delete a thread. The thread object will be removed from
 *          thread queue and deleted from system object management in the idle thread.
489
 *
L
liukangcc 已提交
490
 * @param   thread is the thread to be deleted.
491
 *
L
liukangcc 已提交
492 493
 * @return  Return the operation status. If the return value is RT_EOK, the function is successfully executed.
 *          If the return value is any other values, it means this operation failed.
494
 */
D
dzzxzz 已提交
495
rt_err_t rt_thread_delete(rt_thread_t thread)
496
{
497
    rt_base_t level;
498

mysterywolf's avatar
mysterywolf 已提交
499
    /* parameter check */
500
    RT_ASSERT(thread != RT_NULL);
501 502
    RT_ASSERT(rt_object_get_type((rt_object_t)thread) == RT_Object_Class_Thread);
    RT_ASSERT(rt_object_is_systemobject((rt_object_t)thread) == RT_FALSE);
503

C
cliff-cmc 已提交
504
    if ((thread->stat & RT_THREAD_STAT_MASK) == RT_THREAD_CLOSE)
505 506
        return RT_EOK;

B
bernard 已提交
507
    if ((thread->stat & RT_THREAD_STAT_MASK) != RT_THREAD_INIT)
508 509 510 511
    {
        /* remove from schedule */
        rt_schedule_remove_thread(thread);
    }
512

513
    /* disable interrupt */
514
    level = rt_hw_interrupt_disable();
515

516 517
    /* release thread timer */
    rt_timer_detach(&(thread->thread_timer));
518

519 520 521
    /* change stat */
    thread->stat = RT_THREAD_CLOSE;

522
    /* insert to defunct thread list */
F
fenghuijie 已提交
523
    rt_thread_defunct_enqueue(thread);
524

525
    /* enable interrupt */
526
    rt_hw_interrupt_enable(level);
527

528
    return RT_EOK;
529
}
530
RTM_EXPORT(rt_thread_delete);
531
#endif /* RT_USING_HEAP */
532 533

/**
L
liukangcc 已提交
534 535 536
 * @brief   This function will let current thread yield processor, and scheduler will
 *          choose the highest thread to run. After yield processor, the current thread
 *          is still in READY state.
537
 *
L
liukangcc 已提交
538 539
 * @return  Return the operation status. If the return value is RT_EOK, the function is successfully executed.
 *          If the return value is any other values, it means this operation failed.
540
 */
D
dzzxzz 已提交
541
rt_err_t rt_thread_yield(void)
542
{
543
    struct rt_thread *thread;
544
    rt_base_t level;
545 546

    thread = rt_thread_self();
547
    level = rt_hw_interrupt_disable();
548 549
    thread->remaining_tick = thread->init_tick;
    thread->stat |= RT_THREAD_STAT_YIELD;
S
shaojinchun 已提交
550
    rt_schedule();
551
    rt_hw_interrupt_enable(level);
552

553
    return RT_EOK;
554
}
555
RTM_EXPORT(rt_thread_yield);
556 557

/**
558 559
 * @brief   This function will let current thread sleep for some ticks. Change current thread state to suspend,
 *          when the thread timer reaches the tick value, scheduler will awaken this thread.
560
 *
L
liukangcc 已提交
561
 * @param   tick is the sleep ticks.
562
 *
L
liukangcc 已提交
563 564
 * @return  Return the operation status. If the return value is RT_EOK, the function is successfully executed.
 *          If the return value is any other values, it means this operation failed.
565
 */
D
dzzxzz 已提交
566
rt_err_t rt_thread_sleep(rt_tick_t tick)
567
{
568
    rt_base_t level;
569
    struct rt_thread *thread;
570

571
    /* set to current thread */
S
shaojinchun 已提交
572
    thread = rt_thread_self();
573
    RT_ASSERT(thread != RT_NULL);
574
    RT_ASSERT(rt_object_get_type((rt_object_t)thread) == RT_Object_Class_Thread);
575

S
shaojinchun 已提交
576
    /* disable interrupt */
577
    level = rt_hw_interrupt_disable();
S
shaojinchun 已提交
578

579 580 581
    /* reset thread error */
    thread->error = RT_EOK;

582 583
    /* suspend thread */
    rt_thread_suspend(thread);
584

585 586 587
    /* reset the timeout of thread timer and start it */
    rt_timer_control(&(thread->thread_timer), RT_TIMER_CTRL_SET_TIME, &tick);
    rt_timer_start(&(thread->thread_timer));
588

589
    /* enable interrupt */
590
    rt_hw_interrupt_enable(level);
591

592
    rt_schedule();
593

594 595 596
    /* clear error number of this thread to RT_EOK */
    if (thread->error == -RT_ETIMEOUT)
        thread->error = RT_EOK;
597

598
    return thread->error;
599 600 601
}

/**
602
 * @brief   This function will let current thread delay for some ticks.
603
 *
L
liukangcc 已提交
604
 * @param   tick is the delay ticks.
605
 *
L
liukangcc 已提交
606 607
 * @return  Return the operation status. If the return value is RT_EOK, the function is successfully executed.
 *          If the return value is any other values, it means this operation failed.
608 609 610
 */
rt_err_t rt_thread_delay(rt_tick_t tick)
{
611
    return rt_thread_sleep(tick);
612
}
613
RTM_EXPORT(rt_thread_delay);
614

615
/**
616
 * @brief   This function will let current thread delay until (*tick + inc_tick).
617
 *
L
liukangcc 已提交
618
 * @param   tick is the tick of last wakeup.
619
 *
L
liukangcc 已提交
620
 * @param   inc_tick is the increment tick.
621
 *
L
liukangcc 已提交
622 623
 * @return  Return the operation status. If the return value is RT_EOK, the function is successfully executed.
 *          If the return value is any other values, it means this operation failed.
624
 */
B
Bernard Xiong 已提交
625
rt_err_t rt_thread_delay_until(rt_tick_t *tick, rt_tick_t inc_tick)
626
{
627
    rt_base_t level;
628
    struct rt_thread *thread;
629
    rt_tick_t cur_tick;
630

B
Bernard Xiong 已提交
631 632
    RT_ASSERT(tick != RT_NULL);

633 634 635 636 637 638 639 640
    /* set to current thread */
    thread = rt_thread_self();
    RT_ASSERT(thread != RT_NULL);
    RT_ASSERT(rt_object_get_type((rt_object_t)thread) == RT_Object_Class_Thread);

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

641 642 643
    /* reset thread error */
    thread->error = RT_EOK;

644
    cur_tick = rt_tick_get();
645
    if (cur_tick - *tick < inc_tick)
646
    {
647 648 649 650
        rt_tick_t left_tick;

        *tick += inc_tick;
        left_tick = *tick - cur_tick;
651 652 653 654 655

        /* suspend thread */
        rt_thread_suspend(thread);

        /* reset the timeout of thread timer and start it */
656
        rt_timer_control(&(thread->thread_timer), RT_TIMER_CTRL_SET_TIME, &left_tick);
657 658 659 660 661 662 663 664 665 666 667 668 669 670 671
        rt_timer_start(&(thread->thread_timer));

        /* enable interrupt */
        rt_hw_interrupt_enable(level);

        rt_schedule();

        /* clear error number of this thread to RT_EOK */
        if (thread->error == -RT_ETIMEOUT)
        {
            thread->error = RT_EOK;
        }
    }
    else
    {
672
        *tick = cur_tick;
673 674 675
        rt_hw_interrupt_enable(level);
    }

676
    return thread->error;
677
}
678
RTM_EXPORT(rt_thread_delay_until);
679

H
heyuanjie 已提交
680
/**
681
 * @brief   This function will let current thread delay for some milliseconds.
H
heyuanjie 已提交
682
 *
L
liukangcc 已提交
683
 * @param   ms is the delay ms time.
H
heyuanjie 已提交
684
 *
L
liukangcc 已提交
685 686
 * @return  Return the operation status. If the return value is RT_EOK, the function is successfully executed.
 *          If the return value is any other values, it means this operation failed.
H
heyuanjie 已提交
687 688 689 690 691 692 693 694 695 696 697
 */
rt_err_t rt_thread_mdelay(rt_int32_t ms)
{
    rt_tick_t tick;

    tick = rt_tick_from_millisecond(ms);

    return rt_thread_sleep(tick);
}
RTM_EXPORT(rt_thread_mdelay);

B
bernard.xiong@gmail.com 已提交
698
/**
699 700
 * @brief   This function will control thread behaviors according to control command.
 *
L
liukangcc 已提交
701
 * @param   thread is the specified thread to be controlled.
702
 *
L
liukangcc 已提交
703
 * @param   cmd is the control command, which includes.
B
bernard.xiong@gmail.com 已提交
704
 *
705
 *              RT_THREAD_CTRL_CHANGE_PRIORITY for changing priority level of thread.
B
bernard.xiong@gmail.com 已提交
706
 *
707 708 709 710 711 712
 *              RT_THREAD_CTRL_STARTUP for starting a thread.
 *
 *              RT_THREAD_CTRL_CLOSE for delete a thread.
 *
 *              RT_THREAD_CTRL_BIND_CPU for bind the thread to a CPU.
 *
L
liukangcc 已提交
713
 * @param   arg is the argument of control command.
714
 *
L
liukangcc 已提交
715 716
 * @return  Return the operation status. If the return value is RT_EOK, the function is successfully executed.
 *          If the return value is any other values, it means this operation failed.
B
bernard.xiong@gmail.com 已提交
717
 */
B
bernard 已提交
718
rt_err_t rt_thread_control(rt_thread_t thread, int cmd, void *arg)
719
{
720
    rt_base_t level;
721

mysterywolf's avatar
mysterywolf 已提交
722
    /* parameter check */
723
    RT_ASSERT(thread != RT_NULL);
724
    RT_ASSERT(rt_object_get_type((rt_object_t)thread) == RT_Object_Class_Thread);
725

726 727
    switch (cmd)
    {
728
        case RT_THREAD_CTRL_CHANGE_PRIORITY:
729
        {
730
            /* disable interrupt */
731
            level = rt_hw_interrupt_disable();
732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748

            /* for ready thread, change queue */
            if ((thread->stat & RT_THREAD_STAT_MASK) == RT_THREAD_READY)
            {
                /* remove thread from schedule queue first */
                rt_schedule_remove_thread(thread);

                /* change thread priority */
                thread->current_priority = *(rt_uint8_t *)arg;

                /* recalculate priority attribute */
    #if RT_THREAD_PRIORITY_MAX > 32
                thread->number      = thread->current_priority >> 3;            /* 5bit */
                thread->number_mask = 1 << thread->number;
                thread->high_mask   = 1 << (thread->current_priority & 0x07);   /* 3bit */
    #else
                thread->number_mask = 1 << thread->current_priority;
749
    #endif /* RT_THREAD_PRIORITY_MAX > 32 */
750 751 752 753 754 755 756 757 758 759 760 761 762 763 764

                /* insert thread to schedule queue again */
                rt_schedule_insert_thread(thread);
            }
            else
            {
                thread->current_priority = *(rt_uint8_t *)arg;

                /* recalculate priority attribute */
    #if RT_THREAD_PRIORITY_MAX > 32
                thread->number      = thread->current_priority >> 3;            /* 5bit */
                thread->number_mask = 1 << thread->number;
                thread->high_mask   = 1 << (thread->current_priority & 0x07);   /* 3bit */
    #else
                thread->number_mask = 1 << thread->current_priority;
765
    #endif /* RT_THREAD_PRIORITY_MAX > 32 */
766 767 768
            }

            /* enable interrupt */
769
            rt_hw_interrupt_enable(level);
770
            break;
771
        }
772

773 774 775
        case RT_THREAD_CTRL_STARTUP:
        {
            return rt_thread_startup(thread);
776
        }
777

778
        case RT_THREAD_CTRL_CLOSE:
779
        {
780 781 782 783 784 785 786 787 788 789 790
            rt_err_t rt_err;

            if (rt_object_is_systemobject((rt_object_t)thread) == RT_TRUE)
            {
                rt_err = rt_thread_detach(thread);
            }
    #ifdef RT_USING_HEAP
            else
            {
                rt_err = rt_thread_delete(thread);
            }
791
    #endif /* RT_USING_HEAP */
792 793
            rt_schedule();
            return rt_err;
794
        }
795 796 797

    #ifdef RT_USING_SMP
        case RT_THREAD_CTRL_BIND_CPU:
798
        {
799
            rt_uint8_t cpu;
800

801 802 803 804 805
            if ((thread->stat & RT_THREAD_STAT_MASK) != RT_THREAD_INIT)
            {
                /* we only support bind cpu before started phase. */
                return RT_ERROR;
            }
S
shaojinchun 已提交
806

M
mazhiyuan 已提交
807
            cpu = (rt_uint8_t)(rt_size_t)arg;
808 809
            thread->bind_cpu = cpu > RT_CPUS_NR? RT_CPUS_NR : cpu;
            break;
S
shaojinchun 已提交
810
        }
811
    #endif /* RT_USING_SMP */
S
shaojinchun 已提交
812

813 814
        default:
            break;
815
    }
816

817
    return RT_EOK;
818
}
819
RTM_EXPORT(rt_thread_control);
820 821

/**
822
 * @brief   This function will suspend the specified thread and change it to suspend state.
823
 *
824 825 826 827 828
 * @note    This function ONLY can suspend current thread itself.
 *          Do not use the rt_thread_suspend and rt_thread_resume functions to synchronize the activities of threads.
 *          You have no way of knowing what code a thread is executing when you suspend it.
 *          If you suspend a thread while it is executing a critical area which is protected by a mutex,
 *          other threads attempt to use that mutex and have to wait. Deadlocks can occur very easily.
829
 *
L
liukangcc 已提交
830
 * @param   thread is the thread to be suspended.
831
 *
L
liukangcc 已提交
832 833
 * @return  Return the operation status. If the return value is RT_EOK, the function is successfully executed.
 *          If the return value is any other values, it means this operation failed.
834
 */
D
dzzxzz 已提交
835
rt_err_t rt_thread_suspend(rt_thread_t thread)
836
{
837
    rt_base_t stat;
838
    rt_base_t level;
839

mysterywolf's avatar
mysterywolf 已提交
840
    /* parameter check */
841
    RT_ASSERT(thread != RT_NULL);
842
    RT_ASSERT(rt_object_get_type((rt_object_t)thread) == RT_Object_Class_Thread);
843
    RT_ASSERT(thread == rt_thread_self());
844

845
    RT_DEBUG_LOG(RT_DEBUG_THREAD, ("thread suspend:  %s\n", thread->name));
846

847 848
    stat = thread->stat & RT_THREAD_STAT_MASK;
    if ((stat != RT_THREAD_READY) && (stat != RT_THREAD_RUNNING))
849
    {
850
        RT_DEBUG_LOG(RT_DEBUG_THREAD, ("thread suspend: thread disorder, 0x%2x\n", thread->stat));
851 852
        return -RT_ERROR;
    }
853

854
    /* disable interrupt */
855
    level = rt_hw_interrupt_disable();
856

857 858
    /* change thread stat */
    rt_schedule_remove_thread(thread);
859
    thread->stat = RT_THREAD_SUSPEND | (thread->stat & ~RT_THREAD_STAT_MASK);
860

861 862 863
    /* stop thread timer anyway */
    rt_timer_stop(&(thread->thread_timer));

864
    /* enable interrupt */
865
    rt_hw_interrupt_enable(level);
866

867
    RT_OBJECT_HOOK_CALL(rt_thread_suspend_hook, (thread));
868
    return RT_EOK;
869
}
870
RTM_EXPORT(rt_thread_suspend);
871 872

/**
873
 * @brief   This function will resume a thread and put it to system ready queue.
874
 *
875 876
 * @note    Do not use the rt_thread_suspend and rt_thread_resume functions to synchronize the activities of threads.
 *
L
liukangcc 已提交
877
 * @param   thread is the thread to be resumed.
878
 *
L
liukangcc 已提交
879 880
 * @return  Return the operation status. If the return value is RT_EOK, the function is successfully executed.
 *          If the return value is any other values, it means this operation failed.
881
 */
D
dzzxzz 已提交
882
rt_err_t rt_thread_resume(rt_thread_t thread)
883
{
884
    rt_base_t level;
885

mysterywolf's avatar
mysterywolf 已提交
886
    /* parameter check */
887
    RT_ASSERT(thread != RT_NULL);
888
    RT_ASSERT(rt_object_get_type((rt_object_t)thread) == RT_Object_Class_Thread);
889

890
    RT_DEBUG_LOG(RT_DEBUG_THREAD, ("thread resume:  %s\n", thread->name));
891

B
bernard 已提交
892
    if ((thread->stat & RT_THREAD_STAT_MASK) != RT_THREAD_SUSPEND)
893 894
    {
        RT_DEBUG_LOG(RT_DEBUG_THREAD, ("thread resume: thread disorder, %d\n",
895
                                       thread->stat));
896

897 898
        return -RT_ERROR;
    }
899

900
    /* disable interrupt */
901
    level = rt_hw_interrupt_disable();
902

903 904
    /* remove from suspend list */
    rt_list_remove(&(thread->tlist));
905

906
    rt_timer_stop(&thread->thread_timer);
907

908 909
    /* insert to schedule ready list */
    rt_schedule_insert_thread(thread);
910

F
fenghuijie 已提交
911
    /* enable interrupt */
912
    rt_hw_interrupt_enable(level);
F
fenghuijie 已提交
913

914
    RT_OBJECT_HOOK_CALL(rt_thread_resume_hook, (thread));
915
    return RT_EOK;
916
}
917
RTM_EXPORT(rt_thread_resume);
918 919

/**
920
 * @brief   This function will find the specified thread.
921
 *
L
liukangcc 已提交
922
 * @note    Please don't invoke this function in interrupt status.
923
 *
L
liukangcc 已提交
924
 * @param   name is the name of thread finding.
B
bernard.xiong@gmail.com 已提交
925
 *
L
liukangcc 已提交
926 927
 * @return  If the return value is a rt_thread structure pointer, the function is successfully executed.
 *          If the return value is RT_NULL, it means this operation failed.
928
 */
D
dzzxzz 已提交
929
rt_thread_t rt_thread_find(char *name)
930
{
931
    return (rt_thread_t)rt_object_find(name, RT_Object_Class_Thread);
932
}
933

934
RTM_EXPORT(rt_thread_find);
935

D
dogandog 已提交
936
/**@}*/