* @brief This function will create a thread object and allocate thread object memory. * and stack. * * @param name The name of the thread (shall be unique.); the maximum length of * the thread name is specified by macro `RT_NAME_MAX` in `rtconfig.h`, * and the extra part is automatically truncated. * * @param entry Entry function of thread. * * @
| 548 | * If the return value is `RT_NULL`, it means this operation failed. |
| 549 | */ |
| 550 | rt_thread_t rt_thread_create(const char *name, |
| 551 | void (*entry)(void *parameter), |
| 552 | void *parameter, |
| 553 | rt_uint32_t stack_size, |
| 554 | rt_uint8_t priority, |
| 555 | rt_uint32_t tick) |
| 556 | { |
| 557 | /* parameter check */ |
| 558 | RT_ASSERT(tick != 0); |
| 559 | |
| 560 | struct rt_thread *thread; |
| 561 | void *stack_start; |
| 562 | |
| 563 | thread = (struct rt_thread *)rt_object_allocate(RT_Object_Class_Thread, |
| 564 | name); |
| 565 | if (thread == RT_NULL) |
| 566 | return RT_NULL; |
| 567 | |
| 568 | stack_start = (void *)RT_KERNEL_MALLOC(stack_size); |
| 569 | if (stack_start == RT_NULL) |
| 570 | { |
| 571 | /* allocate stack failure */ |
| 572 | rt_object_delete((rt_object_t)thread); |
| 573 | |
| 574 | return RT_NULL; |
| 575 | } |
| 576 | |
| 577 | _thread_init(thread, |
| 578 | name, |
| 579 | entry, |
| 580 | parameter, |
| 581 | stack_start, |
| 582 | stack_size, |
| 583 | priority, |
| 584 | tick); |
| 585 | |
| 586 | return thread; |
| 587 | } |
| 588 | RTM_EXPORT(rt_thread_create); |
| 589 | |
| 590 | /** |