support dynamic task create in cmsis
1. enable TOS_CFG_TASK_DYNAMIC_CREATE_EN 2. use osThreadDynamicDef to define a dynamic created cmsis task 3. use osThreadCreate/osThreadTerminate to create/destroy this cmsis task 4. see sample hello_world
This commit is contained in:
@@ -8,18 +8,31 @@ osThreadDef(task1, osPriorityNormal, 1, TASK1_STK_SIZE);
|
||||
void task2(void *arg);
|
||||
osThreadDef(task2, osPriorityNormal, 1, TASK2_STK_SIZE);
|
||||
|
||||
#define TASK3_STK_SIZE 512
|
||||
void task3(void *arg);
|
||||
|
||||
void task1(void *arg)
|
||||
{
|
||||
int count = 1;
|
||||
osThreadId task_dyn_created;
|
||||
|
||||
osThreadDynamicDef(task3, osPriorityNormal, 1, TASK3_STK_SIZE);
|
||||
task_dyn_created = osThreadCreate(osThread(task3), NULL);
|
||||
|
||||
int count = 0;
|
||||
|
||||
while (1) {
|
||||
printf("###This is task1, %d\r\n", count++);
|
||||
printf("###I am task1\r\n");
|
||||
osDelay(2000);
|
||||
|
||||
if (count++ == 3) {
|
||||
printf("###I am task1, kill the dynamic created task\r\n");
|
||||
osThreadTerminate(task_dyn_created);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void task2(void *arg)
|
||||
{
|
||||
int count = 1;
|
||||
while (1) {
|
||||
#if TOS_CFG_TASK_STACK_DRAUGHT_DEPTH_DETACT_EN > 0u
|
||||
k_err_t rc;
|
||||
@@ -29,13 +42,22 @@ void task2(void *arg)
|
||||
printf("%d %d\n", rc, depth);
|
||||
#endif
|
||||
|
||||
printf("***This is task2, %d\r\n", count++);
|
||||
printf("***I am task2\r\n");
|
||||
osDelay(1000);
|
||||
}
|
||||
}
|
||||
|
||||
void task3(void *arg)
|
||||
{
|
||||
while (1) {
|
||||
printf("$$$I am task3(dynamic created)\r\n");
|
||||
osDelay(2000);
|
||||
}
|
||||
}
|
||||
|
||||
void application_entry(void *arg)
|
||||
{
|
||||
osThreadCreate(osThread(task1), NULL); // Create task1
|
||||
osThreadCreate(osThread(task2), NULL); // Create task2
|
||||
}
|
||||
|
||||
|
@@ -176,7 +176,6 @@ __API__ k_err_t tos_task_destroy(k_task_t *task);
|
||||
* @brief Create a task with a dynamic allocated task handler and stack.
|
||||
* create a task with a dynamic allocated task handler and stack.
|
||||
*
|
||||
* @attention a task created by tos_task_create_dyn, should be destroyed by tos_task_destroy_dyn.
|
||||
* @param[out] task dynamic allocated task handler.
|
||||
* @param[in] name name of the task.
|
||||
* @param[in] entry running entry of the task.
|
||||
@@ -199,20 +198,6 @@ __API__ k_err_t tos_task_create_dyn(k_task_t **task,
|
||||
size_t stk_size,
|
||||
k_timeslice_t timeslice);
|
||||
|
||||
/**
|
||||
* @brief Destroy a dynamic created task.
|
||||
* delete a dynamic created task.
|
||||
*
|
||||
* @attention the API to destroy a dynamic created task.
|
||||
*
|
||||
* @param[in] task pointer to the handler of the task to be deleted.
|
||||
*
|
||||
* @return errcode
|
||||
* @retval #K_ERR_TASK_DESTROY_IDLE attempt to destroy idle task.
|
||||
* @retval #K_ERR_NONE return successfully.
|
||||
*/
|
||||
__API__ k_err_t tos_task_destroy_dyn(k_task_t *task);
|
||||
|
||||
#endif
|
||||
|
||||
/**
|
||||
|
@@ -184,7 +184,7 @@ __STATIC__ k_err_t task_do_destroy(k_task_t *task)
|
||||
return K_ERR_NONE;
|
||||
}
|
||||
|
||||
__API__ k_err_t tos_task_destroy(k_task_t *task)
|
||||
__STATIC__ k_err_t task_destroy_static(k_task_t *task)
|
||||
{
|
||||
TOS_IN_IRQ_CHECK();
|
||||
|
||||
@@ -198,7 +198,7 @@ __API__ k_err_t tos_task_destroy(k_task_t *task)
|
||||
return K_ERR_SCHED_LOCKED;
|
||||
}
|
||||
|
||||
#if TOS_CFG_TASK_DYNAMIC_CREATE_EN
|
||||
#if TOS_CFG_TASK_DYNAMIC_CREATE_EN > 0u
|
||||
if (!knl_object_alloc_is_static(&task->knl_obj)) {
|
||||
return K_ERR_OBJ_INVALID_ALLOC_TYPE;
|
||||
}
|
||||
@@ -284,26 +284,10 @@ __API__ k_err_t tos_task_create_dyn(k_task_t **task,
|
||||
return K_ERR_NONE;
|
||||
}
|
||||
|
||||
__API__ k_err_t tos_task_destroy_dyn(k_task_t *task)
|
||||
__STATIC__ k_err_t task_destroy_dyn(k_task_t *task)
|
||||
{
|
||||
k_err_t err;
|
||||
|
||||
TOS_IN_IRQ_CHECK();
|
||||
|
||||
if (unlikely(!task)) {
|
||||
task = k_curr_task;
|
||||
}
|
||||
|
||||
TOS_OBJ_VERIFY(task, KNL_OBJ_TYPE_TASK);
|
||||
|
||||
if (knl_is_self(task) && knl_is_sched_locked()) {
|
||||
return K_ERR_SCHED_LOCKED;
|
||||
}
|
||||
|
||||
if (!knl_object_alloc_is_dynamic(&task->knl_obj)) {
|
||||
return K_ERR_OBJ_INVALID_ALLOC_TYPE;
|
||||
}
|
||||
|
||||
tos_knl_sched_lock();
|
||||
|
||||
err = task_do_destroy(task);
|
||||
@@ -327,6 +311,29 @@ __API__ k_err_t tos_task_destroy_dyn(k_task_t *task)
|
||||
|
||||
#endif
|
||||
|
||||
__API__ k_err_t tos_task_destroy(k_task_t *task)
|
||||
{
|
||||
TOS_IN_IRQ_CHECK();
|
||||
|
||||
if (unlikely(!task)) {
|
||||
task = k_curr_task;
|
||||
}
|
||||
|
||||
TOS_OBJ_VERIFY(task, KNL_OBJ_TYPE_TASK);
|
||||
|
||||
if (knl_is_self(task) && knl_is_sched_locked()) {
|
||||
return K_ERR_SCHED_LOCKED;
|
||||
}
|
||||
|
||||
#if TOS_CFG_TASK_DYNAMIC_CREATE_EN > 0u
|
||||
if (knl_object_alloc_is_dynamic(&task->knl_obj)) {
|
||||
return task_destroy_dyn(task);
|
||||
}
|
||||
#endif
|
||||
|
||||
return task_destroy_static(task);
|
||||
}
|
||||
|
||||
__API__ void tos_task_yield(void)
|
||||
{
|
||||
TOS_CPU_CPSR_ALLOC();
|
||||
|
@@ -75,9 +75,20 @@ osThreadId osThreadCreate(const osThreadDef_t *thread_def, void *argument)
|
||||
return NULL;
|
||||
}
|
||||
|
||||
#if TOS_CFG_TASK_DYNAMIC_CREATE_EN > 0u
|
||||
if (!thread_def->stackbase && !thread_def->task) {
|
||||
k_task_t *task;
|
||||
err = tos_task_create_dyn(&task, thread_def->name, (k_task_entry_t)thread_def->pthread,
|
||||
argument, priority_cmsis2knl(thread_def->tpriority),
|
||||
thread_def->stacksize, thread_def->timeslice);
|
||||
return err == K_ERR_NONE ? task : NULL;
|
||||
}
|
||||
#endif
|
||||
|
||||
err = tos_task_create((k_task_t *)thread_def->task, thread_def->name, (k_task_entry_t)thread_def->pthread,
|
||||
argument, priority_cmsis2knl(thread_def->tpriority), thread_def->stackbase,
|
||||
thread_def->stacksize, thread_def->timeslice);
|
||||
|
||||
return err == K_ERR_NONE ? thread_def->task : NULL;
|
||||
}
|
||||
|
||||
|
@@ -398,12 +398,21 @@ uint32_t osKernelSysTick(void);
|
||||
#define osThreadDef(name, priority, instances, stacksz) \
|
||||
extern const osThreadDef_t os_thread_def_##name
|
||||
#else // define the object
|
||||
|
||||
#define osThreadDef(name, priority, instances, stacksz) \
|
||||
k_task_t task_handler_##name; \
|
||||
k_stack_t task_stack_##name[(stacksz)]; \
|
||||
const osThreadDef_t os_thread_def_##name = \
|
||||
{ #name, (os_pthread)(name), (osPriority)(priority), (instances), \
|
||||
(&((task_stack_##name)[0])), (stacksz), ((k_timeslice_t)0u), (&(task_handler_##name)) }
|
||||
|
||||
#if (TOS_CFG_TASK_DYNAMIC_CREATE_EN > 0u)
|
||||
#define osThreadDynamicDef(name, priority, instances, stacksz) \
|
||||
const osThreadDef_t os_thread_def_##name = \
|
||||
{ #name, (os_pthread)(name), (osPriority)(priority), (instances), \
|
||||
(K_NULL), (stacksz), ((k_timeslice_t)0u), (K_NULL) }
|
||||
#endif
|
||||
|
||||
#endif
|
||||
|
||||
/// Access a Thread definition.
|
||||
|
Reference in New Issue
Block a user