OS_RESULT rt_tsk_delete (OS_TID task_id) { /* Terminate the task identified with "task_id". */ P_TCB task_context; if (task_id == 0 || task_id == os_tsk.run->task_id) { /* Terminate itself. */ os_tsk.run->state = INACTIVE; os_tsk.run->tsk_stack = rt_get_PSP (); rt_stk_check (); os_active_TCB[os_tsk.run->task_id-1] = NULL; os_tsk.run->stack = NULL; DBG_TASK_NOTIFY(os_tsk.run, __FALSE); os_tsk.run = NULL; rt_dispatch (NULL); /* The program should never come to this point. */ } else { /* Find the task in the "os_active_TCB" array. */ if (task_id > os_maxtaskrun || os_active_TCB[task_id-1] == NULL) { /* Task with "task_id" not found or not started. */ return (OS_R_NOK); } task_context = os_active_TCB[task_id-1]; rt_rmv_list (task_context); rt_rmv_dly (task_context); os_active_TCB[task_id-1] = NULL; task_context->stack = NULL; DBG_TASK_NOTIFY(task_context, __FALSE); } return (OS_R_OK); }
OS_RESULT rt_tsk_get(OS_TID task_id, RL_TASK_INFO *buffer) { U16 stack_size; U32 start_stack_ptr; U32 current_stack_ptr; U32 end_stack_ptr; P_TCB p_task; p_task = os_active_TCB[task_id-1]; if(p_task->state == 0 || p_task->prio == 0 || p_task->tsk_stack == 0 || p_task->task_id == 0) return OS_R_NOK; buffer->state = p_task->state; buffer->prio = p_task->prio; buffer->task_id = p_task->task_id; end_stack_ptr = (U32) p_task->stack; stack_size = (U16)os_stackinfo; if(p_task->state == 2) { current_stack_ptr = rt_get_PSP(); } else { current_stack_ptr = p_task->tsk_stack; } start_stack_ptr = end_stack_ptr + stack_size; buffer->stack_usage = ((( start_stack_ptr - current_stack_ptr ) * 100) / ( stack_size)); buffer->ptask = p_task->ptask; /* printf("(start_stack_ptr - current_stack_ptr) : %d\n",( start_stack_ptr - current_stack_ptr )); printf("(( start_stack_ptr - current_stack_ptr ) * 100) : %d\n",(( start_stack_ptr - current_stack_ptr ) * 100)); printf("((( start_stack_ptr - current_stack_ptr ) * 100) / ( stack_size)) : %d\n",((( start_stack_ptr - current_stack_ptr ) * 100) / ( stack_size))); printf("100 - ((( start_stack_ptr - current_stack_ptr ) * 100) / ( stack_size)) : %d\n",100-((( start_stack_ptr - current_stack_ptr ) * 100) / ( stack_size))); printf("start_stack_ptr = %d\n", start_stack_ptr); printf("stack_size = %d\n", stack_size ); printf("current_stack_ptr = %d\n", current_stack_ptr); printf("end_stack_ptr = %d\n", end_stack_ptr); */ // printf("( rt_get_PSP() - (p_task->tsk_stack) ) : %d\n", ( rt_get_PSP() - (p_task->tsk_stack) ) ); // printf("(( ( rt_get_PSP() - (p_task->tsk_stack) ) * 100 ) : %d\n", ( ( rt_get_PSP() - (p_task->tsk_stack) ) * 100 ) ); // printf("( ( ( rt_get_PSP() - (p_task->tsk_stack) ) * 100 ) / (stack_size) ): %d\n", ( ( ( rt_get_PSP() - (p_task->tsk_stack) ) * 100 ) / (stack_size) ) ); //printf("State = %d\n Priority = %d\n task_id = %d\n stack_usage = %d\n", buffer->state, buffer->prio, buffer->task_id, buffer->stack_usage); return OS_R_OK; }
OS_RESULT rt_tsk_delete (OS_TID task_id) { /* Terminate the task identified with "task_id". */ P_TCB task_context; P_TCB p_TCB; P_MUCB p_MCB, p_MCB0; if ((task_id == 0U) || (task_id == os_tsk.run->task_id)) { /* Terminate itself. */ os_tsk.run->state = INACTIVE; os_tsk.run->tsk_stack = rt_get_PSP (); rt_stk_check (); p_MCB = os_tsk.run->p_mlnk; while (p_MCB) { /* Release mutexes owned by this task */ if (p_MCB->p_lnk) { /* A task is waiting for mutex. */ p_TCB = rt_get_first ((P_XCB)p_MCB); #ifdef __CMSIS_RTOS rt_ret_val (p_TCB, 0U/*osOK*/); #else rt_ret_val (p_TCB, OS_R_MUT); #endif rt_rmv_dly (p_TCB); p_TCB->state = READY; rt_put_prio (&os_rdy, p_TCB); /* A waiting task becomes the owner of this mutex. */ p_MCB0 = p_MCB->p_mlnk; p_MCB->level = 1U; p_MCB->owner = p_TCB; p_MCB->p_mlnk = p_TCB->p_mlnk; p_TCB->p_mlnk = p_MCB; p_MCB = p_MCB0; } else { p_MCB0 = p_MCB->p_mlnk; p_MCB->level = 0U; p_MCB->owner = NULL; p_MCB->p_mlnk = NULL; p_MCB = p_MCB0; } } os_active_TCB[os_tsk.run->task_id-1U] = NULL; rt_free_box (mp_stk, os_tsk.run->stack); os_tsk.run->stack = NULL; DBG_TASK_NOTIFY(os_tsk.run, __FALSE); rt_free_box (mp_tcb, os_tsk.run); os_tsk.run = NULL; rt_dispatch (NULL); /* The program should never come to this point. */ } else { /* Find the task in the "os_active_TCB" array. */ if ((task_id > os_maxtaskrun) || (os_active_TCB[task_id-1U] == NULL)) { /* Task with "task_id" not found or not started. */ return (OS_R_NOK); } task_context = os_active_TCB[task_id-1U]; rt_rmv_list (task_context); rt_rmv_dly (task_context); p_MCB = task_context->p_mlnk; while (p_MCB) { /* Release mutexes owned by this task */ if (p_MCB->p_lnk) { /* A task is waiting for mutex. */ p_TCB = rt_get_first ((P_XCB)p_MCB); #ifdef __CMSIS_RTOS rt_ret_val (p_TCB, 0U/*osOK*/); #else rt_ret_val (p_TCB, OS_R_MUT); #endif rt_rmv_dly (p_TCB); p_TCB->state = READY; rt_put_prio (&os_rdy, p_TCB); /* A waiting task becomes the owner of this mutex. */ p_MCB0 = p_MCB->p_mlnk; p_MCB->level = 1U; p_MCB->owner = p_TCB; p_MCB->p_mlnk = p_TCB->p_mlnk; p_TCB->p_mlnk = p_MCB; p_MCB = p_MCB0; } else { p_MCB0 = p_MCB->p_mlnk; p_MCB->level = 0U; p_MCB->owner = NULL; p_MCB->p_mlnk = NULL; p_MCB = p_MCB0; } } os_active_TCB[task_id-1U] = NULL; rt_free_box (mp_stk, task_context->stack); task_context->stack = NULL; DBG_TASK_NOTIFY(task_context, __FALSE); rt_free_box (mp_tcb, task_context); if (rt_rdy_prio() > os_tsk.run->prio) { /* Ready task has higher priority than running task. */ os_tsk.run->state = READY; rt_put_prio (&os_rdy, os_tsk.run); rt_dispatch (NULL); } } return (OS_R_OK); }