/*---------------------------------------------------------------------------- * Task 2: RTX Kernel starts this task with os_tsk_create (consumer_task, 0) *---------------------------------------------------------------------------*/ __task void consumer_task (void) { OS_TID consumer_tskid; /* assigned identification for consumer */ consumer_tskid = os_tsk_self(); /* get it's own task ID */ T_NUM *random_num_rx; while( msg_counter_rec < N ) { os_mbx_wait (MsgBox, (void **)&random_num_rx, 0xffff); /* wait for the message */ os_mut_wait(g_mut_uart, 0xFFFF); printf("[consumer_task [pid:(%d)] ]: Received %u\n", consumer_tskid, random_num_rx->number); msg_counter_rec += 1; os_mut_release(g_mut_uart); if( _free_box (mpool, random_num_rx) ) /* free memory allocated for message */ { os_mut_wait(g_mut_uart, 0xFFFF); printf("_free_box failed because memory couldn't be freed\n"); os_mut_release(g_mut_uart); exit(1); } } //Get Time C time_c = os_time_get(); os_mut_wait(g_mut_uart, 0xFFFF); printf("Time to initialize system: %0.6f\n", (float)(((float)time_b - time_a)/1000000) ); printf("Time to transmit data: %0.6f\n", (float)(((float)time_c - time_b)/1000000) ); os_mut_release(g_mut_uart); // os_dly_wait(10); os_tsk_delete_self (); /* We are done here, delete this task */ }
void deallocate(T* p){ __disable_irq(); assert(!_free_box(pool, p)); //return 1 => error ++remaining; __enable_irq(); }
OS_RESULT os_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; tsk_lock(); if (task_id == 0 || task_id == os_runtask->task_id) { /* Terminate itself. */ os_runtask->state = INACTIVE; os_stk_check (0); p_MCB = os_runtask->p_mlnk; while (p_MCB) { /* Release mutexes owned by this task */ if (p_MCB->p_lnk) { /* A task is waiting for mutex. */ p_TCB = os_get_first ((P_XCB)p_MCB); p_TCB->ret_val = OS_R_MUT; os_rmv_dly (p_TCB); p_TCB->state = READY; os_put_prio (&os_rdy, p_TCB); /* A waiting task becomes the owner of this mutex. */ p_MCB0 = p_MCB; p_MCB->level = 1; p_MCB->owner = p_TCB; p_MCB->p_mlnk = p_TCB->p_mlnk; p_TCB->p_mlnk = p_MCB; p_MCB = p_MCB0->p_mlnk; } else { p_MCB = p_MCB->p_mlnk; } } os_active_TCB[os_runtask->task_id-1] = NULL; _free_box (mp_stk, os_runtask->stack); os_runtask->stack = NULL; rt_notify (os_runtask->ptask, (__FALSE << 8) | os_runtask->task_id); _free_box (mp_tcb, os_runtask); os_runtask = NULL; os_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. */ tsk_unlock (); return (OS_R_NOK); } task_context = os_active_TCB[task_id-1]; os_rmv_list (task_context); os_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 = os_get_first ((P_XCB)p_MCB); p_TCB->ret_val = OS_R_MUT; os_rmv_dly (p_TCB); p_TCB->state = READY; os_put_prio (&os_rdy, p_TCB); /* A waiting task becomes the owner of this mutex. */ p_MCB0 = p_MCB; p_MCB->level = 1; p_MCB->owner = p_TCB; p_MCB->p_mlnk = p_TCB->p_mlnk; p_TCB->p_mlnk = p_MCB; p_MCB = p_MCB0->p_mlnk; } else { p_MCB = p_MCB->p_mlnk; } } os_active_TCB[task_id-1] = NULL; _free_box (mp_stk, task_context->stack); task_context->stack = NULL; rt_notify (task_context->ptask, (__FALSE << 8) | task_context->task_id); _free_box (mp_tcb, task_context); tsk_unlock (); if (os_rdy_prio() > os_runtask->prio) { /* Ready task has higher priority than running task. */ os_runtask->state = READY; os_put_prio (&os_rdy, os_runtask); os_dispatch (NULL); } } return (OS_R_OK); }