thread.c 28.2 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 88
}

89
#endif /* RT_USING_HOOK */
90

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

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

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

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

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

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

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

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

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

121 122 123 124 125 126 127 128 129
/**
 * @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;
130
    rt_base_t level;
131 132 133

    thread = (struct rt_thread *)parameter;

mysterywolf's avatar
mysterywolf 已提交
134
    /* parameter check */
135 136 137 138 139
    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 */
140
    level = rt_hw_interrupt_disable();
141 142 143 144 145 146 147 148 149 150 151

    /* 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 */
152
    rt_hw_interrupt_enable(level);
153 154 155 156 157

    /* do schedule */
    rt_schedule();
}

mysterywolf's avatar
mysterywolf 已提交
158 159 160 161 162 163 164 165
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)
166
{
167 168 169 170 171 172 173 174
    /* init thread list */
    rt_list_init(&(thread->tlist));

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

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

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

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

193
    thread->number_mask = 0;
194 195 196 197 198 199

#ifdef RT_USING_EVENT
    thread->event_set = 0;
    thread->event_info = 0;
#endif

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

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

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

S
shaojinchun 已提交
213 214 215 216 217 218 219 220
#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 已提交
221
    thread->critical_lock_nest = 0;
222
#endif /* RT_USING_SMP */
S
shaojinchun 已提交
223

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

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

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

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

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

F
fenghuijie 已提交
252 253 254 255
#ifdef RT_USING_CPU_USAGE
    thread->duration_tick = 0;
#endif

256 257 258 259 260

#ifdef RT_USING_MODULE
    thread->module_id = 0;
#endif

261
    RT_OBJECT_HOOK_CALL(rt_thread_inited_hook, (thread));
B
Bernard Xiong 已提交
262

263
    return RT_EOK;
264 265 266 267 268 269
}

/**
 * @addtogroup Thread
 */

D
dogandog 已提交
270
/**@{*/
271 272

/**
L
liukangcc 已提交
273
 * @brief   This function will initialize a thread. It's used to initialize a
274 275
 *          static thread object.
 *
L
liukangcc 已提交
276
 * @param   thread is the static thread object.
277
 *
L
liukangcc 已提交
278
 * @param   name is the name of thread, which shall be unique.
279
 *
L
liukangcc 已提交
280
 * @param   entry is the entry function of thread.
281
 *
L
liukangcc 已提交
282
 * @param   parameter is the parameter of thread enter function.
283
 *
L
liukangcc 已提交
284
 * @param   stack_start is the start address of thread stack.
285
 *
L
liukangcc 已提交
286
 * @param   stack_size is the size of thread stack.
287
 *
L
liukangcc 已提交
288
 * @param   priority is the priority of thread.
289
 *
L
liukangcc 已提交
290
 * @param   tick is the time slice if there are same priority thread.
291
 *
L
liukangcc 已提交
292 293
 * @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.
294
 */
D
dzzxzz 已提交
295
rt_err_t rt_thread_init(struct rt_thread *thread,
296 297 298 299 300 301 302
                        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)
303
{
mysterywolf's avatar
mysterywolf 已提交
304
    /* parameter check */
305 306 307
    RT_ASSERT(thread != RT_NULL);
    RT_ASSERT(stack_start != RT_NULL);

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

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

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

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

/**
346
 * @brief   This function will start a thread and put it to system ready queue.
347
 *
L
liukangcc 已提交
348
 * @param   thread is the thread to be started.
349
 *
L
liukangcc 已提交
350 351
 * @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.
352
 */
D
dzzxzz 已提交
353
rt_err_t rt_thread_startup(rt_thread_t thread)
354
{
mysterywolf's avatar
mysterywolf 已提交
355
    /* parameter check */
356
    RT_ASSERT(thread != RT_NULL);
B
bernard 已提交
357
    RT_ASSERT((thread->stat & RT_THREAD_STAT_MASK) == RT_THREAD_INIT);
358
    RT_ASSERT(rt_object_get_type((rt_object_t)thread) == RT_Object_Class_Thread);
359

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

369
    RT_DEBUG_LOG(RT_DEBUG_THREAD, ("startup a thread:%s with priority:%d\n",
mysterywolf's avatar
mysterywolf 已提交
370
                                   thread->name, thread->current_priority));
371 372 373 374 375 376 377 378 379 380 381
    /* 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;
382
}
383
RTM_EXPORT(rt_thread_startup);
384 385

/**
386
 * @brief   This function will detach a thread. The thread object will be removed from
L
liukangcc 已提交
387
 *          thread queue and detached/deleted from the system object management.
388
 *
L
liukangcc 已提交
389
 * @param   thread is the thread to be deleted.
390
 *
L
liukangcc 已提交
391 392
 * @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.
393
 */
D
dzzxzz 已提交
394
rt_err_t rt_thread_detach(rt_thread_t thread)
395
{
396
    rt_base_t level;
T
tangyuxin 已提交
397

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

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

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

412
    /* disable interrupt */
413
    level = rt_hw_interrupt_disable();
414

415 416
    /* release thread timer */
    rt_timer_detach(&(thread->thread_timer));
417

418 419
    /* change stat */
    thread->stat = RT_THREAD_CLOSE;
420

421 422 423 424
    /* insert to defunct thread list */
    rt_thread_defunct_enqueue(thread);

    /* enable interrupt */
425
    rt_hw_interrupt_enable(level);
T
tangyuxin 已提交
426

427
    return RT_EOK;
428
}
429 430
RTM_EXPORT(rt_thread_detach);

431
#ifdef RT_USING_HEAP
432
/**
433 434 435
 * @brief   This function will create a thread object and allocate thread object memory.
 *          and stack.
 *
L
liukangcc 已提交
436
 * @param   name is the name of thread, which shall be unique.
437
 *
L
liukangcc 已提交
438
 * @param   entry is the entry function of thread.
439
 *
L
liukangcc 已提交
440
 * @param   parameter is the parameter of thread enter function.
441
 *
L
liukangcc 已提交
442
 * @param   stack_size is the size of thread stack.
443
 *
L
liukangcc 已提交
444
 * @param   priority is the priority of thread.
445
 *
L
liukangcc 已提交
446
 * @param   tick is the time slice if there are same priority thread.
447
 *
L
liukangcc 已提交
448 449
 * @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.
450 451
 */
rt_thread_t rt_thread_create(const char *name,
452 453 454 455 456
                             void (*entry)(void *parameter),
                             void       *parameter,
                             rt_uint32_t stack_size,
                             rt_uint8_t  priority,
                             rt_uint32_t tick)
457
{
458 459 460 461 462 463 464 465
    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 已提交
466
    stack_start = (void *)RT_KERNEL_MALLOC(stack_size);
467 468 469 470 471 472 473 474
    if (stack_start == RT_NULL)
    {
        /* allocate stack failure */
        rt_object_delete((rt_object_t)thread);

        return RT_NULL;
    }

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

    return thread;
485
}
486
RTM_EXPORT(rt_thread_create);
487

488
/**
489 490
 * @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.
491
 *
L
liukangcc 已提交
492
 * @param   thread is the thread to be deleted.
493
 *
L
liukangcc 已提交
494 495
 * @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.
496
 */
D
dzzxzz 已提交
497
rt_err_t rt_thread_delete(rt_thread_t thread)
498
{
499
    rt_base_t level;
500

mysterywolf's avatar
mysterywolf 已提交
501
    /* parameter check */
502
    RT_ASSERT(thread != RT_NULL);
503 504
    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);
505

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

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

515
    /* disable interrupt */
516
    level = rt_hw_interrupt_disable();
517

518 519
    /* release thread timer */
    rt_timer_detach(&(thread->thread_timer));
520

521 522 523
    /* change stat */
    thread->stat = RT_THREAD_CLOSE;

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

527
    /* enable interrupt */
528
    rt_hw_interrupt_enable(level);
529

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

/**
L
liukangcc 已提交
536 537 538
 * @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.
539
 *
L
liukangcc 已提交
540 541
 * @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.
542
 */
D
dzzxzz 已提交
543
rt_err_t rt_thread_yield(void)
544
{
545
    struct rt_thread *thread;
546
    rt_base_t level;
547 548

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

555
    return RT_EOK;
556
}
557
RTM_EXPORT(rt_thread_yield);
558 559

/**
560 561
 * @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.
562
 *
L
liukangcc 已提交
563
 * @param   tick is the sleep ticks.
564
 *
L
liukangcc 已提交
565 566
 * @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.
567
 */
D
dzzxzz 已提交
568
rt_err_t rt_thread_sleep(rt_tick_t tick)
569
{
570
    rt_base_t level;
571
    struct rt_thread *thread;
572

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

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

581 582 583
    /* reset thread error */
    thread->error = RT_EOK;

584 585
    /* suspend thread */
    rt_thread_suspend(thread);
586

587 588 589
    /* 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));
590

591
    /* enable interrupt */
592
    rt_hw_interrupt_enable(level);
593

594
    rt_schedule();
595

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

600
    return thread->error;
601 602 603
}

/**
604
 * @brief   This function will let current thread delay for some ticks.
605
 *
L
liukangcc 已提交
606
 * @param   tick is the delay ticks.
607
 *
L
liukangcc 已提交
608 609
 * @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.
610 611 612
 */
rt_err_t rt_thread_delay(rt_tick_t tick)
{
613
    return rt_thread_sleep(tick);
614
}
615
RTM_EXPORT(rt_thread_delay);
616

617
/**
618
 * @brief   This function will let current thread delay until (*tick + inc_tick).
619
 *
L
liukangcc 已提交
620
 * @param   tick is the tick of last wakeup.
621
 *
L
liukangcc 已提交
622
 * @param   inc_tick is the increment tick.
623
 *
L
liukangcc 已提交
624 625
 * @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.
626
 */
B
Bernard Xiong 已提交
627
rt_err_t rt_thread_delay_until(rt_tick_t *tick, rt_tick_t inc_tick)
628
{
629
    rt_base_t level;
630
    struct rt_thread *thread;
631
    rt_tick_t cur_tick;
632

B
Bernard Xiong 已提交
633 634
    RT_ASSERT(tick != RT_NULL);

635 636 637 638 639 640 641 642
    /* 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();

643 644 645
    /* reset thread error */
    thread->error = RT_EOK;

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

        *tick += inc_tick;
        left_tick = *tick - cur_tick;
653 654 655 656 657

        /* suspend thread */
        rt_thread_suspend(thread);

        /* reset the timeout of thread timer and start it */
658
        rt_timer_control(&(thread->thread_timer), RT_TIMER_CTRL_SET_TIME, &left_tick);
659 660 661 662 663 664 665 666 667 668 669 670 671 672 673
        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
    {
674
        *tick = cur_tick;
675 676 677
        rt_hw_interrupt_enable(level);
    }

678
    return thread->error;
679
}
680
RTM_EXPORT(rt_thread_delay_until);
681

H
heyuanjie 已提交
682
/**
683
 * @brief   This function will let current thread delay for some milliseconds.
H
heyuanjie 已提交
684
 *
L
liukangcc 已提交
685
 * @param   ms is the delay ms time.
H
heyuanjie 已提交
686
 *
L
liukangcc 已提交
687 688
 * @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 已提交
689 690 691 692 693 694 695 696 697 698 699
 */
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 已提交
700
/**
701 702
 * @brief   This function will control thread behaviors according to control command.
 *
L
liukangcc 已提交
703
 * @param   thread is the specified thread to be controlled.
704
 *
L
liukangcc 已提交
705
 * @param   cmd is the control command, which includes.
B
bernard.xiong@gmail.com 已提交
706
 *
707
 *              RT_THREAD_CTRL_CHANGE_PRIORITY for changing priority level of thread.
B
bernard.xiong@gmail.com 已提交
708
 *
709 710 711 712 713 714
 *              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 已提交
715
 * @param   arg is the argument of control command.
716
 *
L
liukangcc 已提交
717 718
 * @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 已提交
719
 */
B
bernard 已提交
720
rt_err_t rt_thread_control(rt_thread_t thread, int cmd, void *arg)
721
{
722
    rt_base_t level;
723

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

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

            /* 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;
751
    #endif /* RT_THREAD_PRIORITY_MAX > 32 */
752 753 754 755 756 757 758 759 760 761 762 763 764 765 766

                /* 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;
767
    #endif /* RT_THREAD_PRIORITY_MAX > 32 */
768 769 770
            }

            /* enable interrupt */
771
            rt_hw_interrupt_enable(level);
772
            break;
773
        }
774

775 776 777
        case RT_THREAD_CTRL_STARTUP:
        {
            return rt_thread_startup(thread);
778
        }
779

780
        case RT_THREAD_CTRL_CLOSE:
781
        {
782 783 784 785 786 787 788 789 790 791 792
            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);
            }
793
    #endif /* RT_USING_HEAP */
794 795
            rt_schedule();
            return rt_err;
796
        }
797 798 799

    #ifdef RT_USING_SMP
        case RT_THREAD_CTRL_BIND_CPU:
800
        {
801
            rt_uint8_t cpu;
802

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

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

815 816
        default:
            break;
817
    }
818

819
    return RT_EOK;
820
}
821
RTM_EXPORT(rt_thread_control);
822 823

/**
824
 * @brief   This function will suspend the specified thread and change it to suspend state.
825
 *
826 827 828 829 830
 * @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.
831
 *
L
liukangcc 已提交
832
 * @param   thread is the thread to be suspended.
833
 *
L
liukangcc 已提交
834 835
 * @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.
836
 */
D
dzzxzz 已提交
837
rt_err_t rt_thread_suspend(rt_thread_t thread)
838
{
839
    rt_base_t stat;
840
    rt_base_t level;
841

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

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

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

856
    /* disable interrupt */
857
    level = rt_hw_interrupt_disable();
858

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

863 864 865
    /* stop thread timer anyway */
    rt_timer_stop(&(thread->thread_timer));

866
    /* enable interrupt */
867
    rt_hw_interrupt_enable(level);
868

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

/**
875
 * @brief   This function will resume a thread and put it to system ready queue.
876
 *
877 878
 * @note    Do not use the rt_thread_suspend and rt_thread_resume functions to synchronize the activities of threads.
 *
L
liukangcc 已提交
879
 * @param   thread is the thread to be resumed.
880
 *
L
liukangcc 已提交
881 882
 * @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.
883
 */
D
dzzxzz 已提交
884
rt_err_t rt_thread_resume(rt_thread_t thread)
885
{
886
    rt_base_t level;
887

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

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

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

899 900
        return -RT_ERROR;
    }
901

902
    /* disable interrupt */
903
    level = rt_hw_interrupt_disable();
904

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

908
    rt_timer_stop(&thread->thread_timer);
909

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

F
fenghuijie 已提交
913
    /* enable interrupt */
914
    rt_hw_interrupt_enable(level);
F
fenghuijie 已提交
915

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

/**
922
 * @brief   This function will find the specified thread.
923
 *
L
liukangcc 已提交
924
 * @note    Please don't invoke this function in interrupt status.
925
 *
L
liukangcc 已提交
926
 * @param   name is the name of thread finding.
B
bernard.xiong@gmail.com 已提交
927
 *
L
liukangcc 已提交
928 929
 * @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.
930
 */
D
dzzxzz 已提交
931
rt_thread_t rt_thread_find(char *name)
932
{
933
    return (rt_thread_t)rt_object_find(name, RT_Object_Class_Thread);
934
}
935

936
RTM_EXPORT(rt_thread_find);
937

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