T* allocate(){ __disable_irq(); assert(remaining-->0); T* ret=(T*)_alloc_box(pool); __enable_irq(); return ret; }
OS_TID os_tsk_create0 (FUNCP task, U32 prio_stksz, void *stk, void *argv) { /* Start a new task declared with "task". */ P_TCB task_context; tsk_lock(); /* Priority 0 is reserved for idle task! */ if ((prio_stksz & 0xFF) == 0) { prio_stksz += 1; } task_context = _alloc_box (mp_tcb); if (task_context == NULL) { tsk_unlock(); return (0); } /* If "size != 0" use a private user provided stack. */ task_context->stack = stk; task_context->priv_stack = prio_stksz >> 8; /* Pass parameter 'argv' to 'os_init_context' */ task_context->p_msg = argv; /* For 'size == 0' system allocates the user stack from the memory pool. */ os_init_context (task_context, prio_stksz & 0xFF, task, __TRUE); /* Find a free entry in 'os_active_TCB' table. */ os_set_TID (task_context); rt_notify (task_context->ptask, (__TRUE << 8) | task_context->task_id); os_dispatch (task_context); tsk_unlock(); return ((OS_TID)task_context->task_id); }
void *_calloc_box (void *box_mem) { /* Allocate a 0-initialized memory block and return start address. */ void *free; U32 *p; U32 i; free = _alloc_box (box_mem); if (free) { p = free; for (i = ((P_BM) box_mem)->blk_size; i; i -= 4) { *p = 0; p++; } } return (free); }
static void os_init_context (P_TCB p_TCB, U8 prio, FUNCP task_body, U8 full_ctx) { /* Initialize general part of the Task Control Block. */ p_TCB->cb_type = TCB; p_TCB->state = READY; p_TCB->prio = prio; p_TCB->prio_base = prio; p_TCB->p_lnk = NULL; p_TCB->p_rlnk = NULL; p_TCB->p_dlnk = NULL; p_TCB->p_blnk = NULL; p_TCB->p_mlnk = NULL; p_TCB->delta_time = 0; p_TCB->interval_time = 0; p_TCB->events = 0; p_TCB->waits = 0; p_TCB->ret_val = OS_R_OK; p_TCB->full_ctx= full_ctx; if (p_TCB->priv_stack == 0) { /* Allocate the memory space for the stack. */ p_TCB->stack = _alloc_box (mp_stk); } os_init_stack (p_TCB, task_body); }
/*---------------------------------------------------------------------------- * Task Producer: RTX Kernel starts this task with os_sys_init (producer_task) *---------------------------------------------------------------------------*/ __task void producer_task (void) { OS_TID producer_tskid; /* assigned identification for producer */ T_NUM *random_num_tx; //Get Time A //time_a = os_time_get(); //Get current task ID producer_tskid = os_tsk_self (); // fork the child process os_tsk_create (consumer_task, 0); /* start task 2 */ os_mut_wait(procon, 0xFFFF); if(cur_producer == 0) { os_mbx_init (MsgBox, sizeof(MsgBox));/* initialize the mailbox */ os_mut_wait(g_mut_uart, 0xFFFF); printf("[producer_task [pid:(%d)] ]: Mailbox Created\n", producer_tskid); os_mut_release(g_mut_uart); cur_producer = 1; } os_mut_release(procon); //Get Time B //time_b = os_time_get() ; // Check if the number is the required one // .. And also make sure that we still have to send numbers. while (msg_counter_send < N && ( (msg_counter_send % P) == producer_tskid ) ) { random_num_tx = _alloc_box(mpool); /* Allocate a memory for the message */ if( random_num_tx == NULL ) { os_mut_wait(g_mut_uart, 0xFFFF); printf("_alloc_box failed because of no mem available!\n"); os_mut_release(g_mut_uart); exit(1); } random_num_tx->number = (U32) ( msg_counter_send ); os_mbx_send (MsgBox, random_num_tx, 0xffff); /* Send the message to the mailbox */ os_mut_wait(g_mut_uart, 0xFFFF); printf("[producer_task [pid:(%d)] ]: Sent %u\n", producer_tskid, random_num_tx->number); msg_counter_send++; os_mut_release(g_mut_uart); // os_dly_wait (100); } //msg_counter_send = 0; os_tsk_delete_self (); /* We are done here, delete this task */ }