/** * * @param timeout * * @return TN_RETVAL */ TN_RETVAL tnnc_task_sleep (TN_TIMEOUT timeout) { TN_UWORD tn_save_status_reg TN_UNUSED; /* for SR save */ TN_RETVAL rc; /* Not check parameter error if (timeout == 0) return TERR_WRONG_PARAM; */ if (tn_is_non_task_context()) { return TERR_WCONTEXT; } tn_disable_interrupt(); if (tn_curr_run_task->wakeup_count > 0) { tn_curr_run_task->wakeup_count--; rc = TERR_NO_ERR; } else { task_curr_to_wait_action(TN_NULL, TSK_WAIT_REASON_SLEEP, timeout); tn_enable_interrupt(); tn_switch_context(); return TERR_NO_ERR; } tn_enable_interrupt(); return rc; }
//---------------------------------------------------------------------------- int tn_task_sleep(unsigned long timeout) { TN_INTSAVE_DATA int rc; if(timeout == 0) return TERR_WRONG_PARAM; TN_CHECK_NON_INT_CONTEXT tn_disable_interrupt(); if(tn_curr_run_task->wakeup_count > 0) { tn_curr_run_task->wakeup_count--; rc = TERR_NO_ERR; } else { task_curr_to_wait_action(NULL, TSK_WAIT_REASON_SLEEP, timeout); tn_enable_interrupt(); tn_switch_context(); return TERR_NO_ERR; } tn_enable_interrupt(); return rc; }
/** * * @param sem * @param timeout * * @return TN_RETVAL */ TN_RETVAL tnec_sem_acquire (TN_SEM_S *sem, TN_TIMEOUT timeout) { TN_UWORD tn_save_status_reg TN_UNUSED; /* for SR save */ TN_RETVAL rc; if (sem == TN_NULL || timeout == 0 || sem->max_count == 0) return TERR_WRONG_PARAM; if (sem->id_sem != TN_ID_SEMAPHORE) return TERR_NOEXS; if (tn_is_non_task_context()) { return TERR_WCONTEXT; } tn_disable_interrupt(); if (sem->count >= 1) { sem->count--; rc = TERR_NO_ERR; } else { task_curr_to_wait_action(&(sem->wait_queue),TSK_WAIT_REASON_SEM,timeout); tn_enable_interrupt(); tn_switch_context(); return tn_curr_run_task->task_wait_rc; } tn_enable_interrupt(); return rc; }
/** * * @param dque * @param data_ptr * @param timeout * * @return TN_RETVAL */ TN_RETVAL tnnc_queue_send (TN_DQUE_S *dque, void *data_ptr, TN_TIMEOUT timeout) { TN_UWORD tn_save_status_reg TN_UNUSED; /* for SR save */ TN_RETVAL rc; CDLL_QUEUE_S *que; TN_TCB_S *task; /* Not check parameter error if (dque == TN_NULL || timeout == 0) return TERR_WRONG_PARAM; */ if (dque->id_dque != TN_ID_DATAQUEUE) return TERR_NOEXS; if (tn_is_non_task_context()) { return TERR_WCONTEXT; } tn_disable_interrupt(); if (!is_queue_empty(&(dque->wait_receive_list))) { que = queue_remove_head(&(dque->wait_receive_list)); task = get_task_by_tsk_queue(que); task->data_elem = data_ptr; if (task_wait_complete(task, TN_FALSE)) { tn_enable_interrupt(); tn_switch_context(); return TERR_NO_ERR; } rc = TERR_NO_ERR; } else { rc = dque_fifo_write(dque,data_ptr); if (rc != TERR_NO_ERR) { tn_curr_run_task->data_elem = data_ptr; task_curr_to_wait_action(&(dque->wait_send_list), TSK_WAIT_REASON_DQUE_WSEND, timeout); tn_enable_interrupt(); tn_switch_context(); return tn_curr_run_task->task_wait_rc; } } tn_enable_interrupt(); return rc; }
//---------------------------------------------------------------------------- int tn_event_wait(TN_EVENT * evf, unsigned int wait_pattern, int wait_mode, unsigned int * p_flags_pattern, unsigned int timeout) { TN_INTSAVE_DATA int rc; //-- return code int fCond; if(evf == NULL || wait_pattern == 0 || p_flags_pattern == NULL || timeout == 0) return TERR_WRONG_PARAM; if(evf->id_event != TN_ID_EVENT) return TERR_NOEXS; TN_CHECK_NON_INT_CONTEXT tn_disable_interrupt(); //-- If event attr is TN_EVENT_ATTR_SINGLE and another task already //-- in event wait queue - return ERROR without checking release condition if((evf->attr & TN_EVENT_ATTR_SINGLE) && !is_queue_empty(&(evf->wait_queue))) { rc = TERR_ILUSE; } else { //-- Check release condition if(wait_mode & TN_EVENT_WCOND_OR) // any setted bit is enough for release condition fCond = ((evf->pattern & wait_pattern) != 0); else // TN_EVENT_WCOND_AND is default mode fCond = ((evf->pattern & wait_pattern) == wait_pattern); if(fCond) { *p_flags_pattern = evf->pattern; if(evf->attr & TN_EVENT_ATTR_CLR) evf->pattern = 0; rc = TERR_NO_ERR; } else { tn_curr_run_task->ewait_mode = wait_mode; tn_curr_run_task->ewait_pattern = wait_pattern; task_curr_to_wait_action(&(evf->wait_queue),TSK_WAIT_REASON_EVENT,timeout); tn_enable_interrupt(); tn_switch_context(); if(tn_curr_run_task->task_wait_rc == TERR_NO_ERR) *p_flags_pattern = tn_curr_run_task->ewait_pattern; return tn_curr_run_task->task_wait_rc; } } tn_enable_interrupt(); return rc; }
//---------------------------------------------------------------------------- int tn_queue_send(TN_DQUE * dque, void * data_ptr, unsigned long timeout) { TN_INTSAVE_DATA int rc; CDLL_QUEUE * que; TN_TCB * task; #if TN_CHECK_PARAM if(dque == NULL || timeout == 0) return TERR_WRONG_PARAM; if(dque->id_dque != TN_ID_DATAQUEUE) return TERR_NOEXS; #endif TN_CHECK_NON_INT_CONTEXT tn_disable_interrupt(); //-- there are task(s) in the data queue's wait_receive list if(!is_queue_empty(&(dque->wait_receive_list))) { que = queue_remove_head(&(dque->wait_receive_list)); task = get_task_by_tsk_queue(que); task->data_elem = data_ptr; if(task_wait_complete(task)) { tn_enable_interrupt(); tn_switch_context(); return TERR_NO_ERR; } rc = TERR_NO_ERR; } else //-- the data queue's wait_receive list is empty { rc = dque_fifo_write(dque,data_ptr); if(rc != TERR_NO_ERR) //-- No free entries in the data queue { tn_curr_run_task->data_elem = data_ptr; //-- Store data_ptr task_curr_to_wait_action(&(dque->wait_send_list), TSK_WAIT_REASON_DQUE_WSEND, timeout); tn_enable_interrupt(); tn_switch_context(); return tn_curr_run_task->task_wait_rc; } } tn_enable_interrupt(); return rc; }
//---------------------------------------------------------------------------- static void tn_timer_task_func(void * par) { TN_INTSAVE_DATA volatile TN_TCB * task; volatile CDLL_QUEUE * curr_que; //-- User application init - user's objects initial (tasks etc.) creation tn_app_init(); //-- Enable interrupt here ( include tick int) tn_cpu_int_enable(); //------------------------------------------------------------------------- for(;;) { //------------ OS timer tick ------------------------------------- tn_disable_interrupt(); curr_que = tn_wait_timeout_list.next; while(curr_que != &tn_wait_timeout_list) { task = get_task_by_timer_queque((CDLL_QUEUE*)curr_que); if(task->tick_count != TN_WAIT_INFINITE) { if(task->tick_count > 0) { task->tick_count--; if(task->tick_count == 0) //-- Time out expiried { queue_remove_entry(&(((TN_TCB*)task)->task_queue)); task_wait_complete((TN_TCB*)task); task->task_wait_rc = TERR_TIMEOUT; } } } curr_que = curr_que->next; } task_curr_to_wait_action(NULL, TSK_WAIT_REASON_SLEEP, TN_WAIT_INFINITE); tn_enable_interrupt(); tn_switch_context(); } }
/** * * @param fmp * @param p_data * @param timeout * * @return TN_RETVAL */ TN_RETVAL tnnc_fmem_get (TN_FMP_S *fmp, void ** p_data, TN_TIMEOUT timeout) { TN_UWORD tn_save_status_reg TN_UNUSED; /* for SR save */ TN_RETVAL rc; void *ptr; /* Not check parameter error if (fmp == TN_NULL || p_data == TN_NULL || timeout == 0) return TERR_WRONG_PARAM; */ if (fmp->id_fmp != TN_ID_FSMEMORYPOOL) return TERR_NOEXS; if (tn_is_non_task_context()) { return TERR_WCONTEXT; } tn_disable_interrupt(); ptr = fm_get(fmp); if (ptr != TN_NULL) { *p_data = ptr; rc = TERR_NO_ERR; } else { task_curr_to_wait_action(&(fmp->wait_queue), TSK_WAIT_REASON_WFIXMEM,timeout); tn_enable_interrupt(); tn_switch_context(); /* When return to this point,in data_elem have to be valid value */ *p_data = tn_curr_run_task->data_elem; return tn_curr_run_task->task_wait_rc; } tn_enable_interrupt(); return rc; }
//---------------------------------------------------------------------------- int tn_fmem_get(TN_FMP * fmp, void ** p_data, unsigned long timeout) { TN_INTSAVE_DATA int rc; void * ptr; #if TN_CHECK_PARAM if(fmp == NULL || p_data == NULL || timeout == 0) return TERR_WRONG_PARAM; if(fmp->id_fmp != TN_ID_FSMEMORYPOOL) return TERR_NOEXS; #endif TN_CHECK_NON_INT_CONTEXT tn_disable_interrupt(); ptr = fm_get(fmp); if(ptr != NULL) //-- Get memory { *p_data = ptr; rc = TERR_NO_ERR; } else { task_curr_to_wait_action(&(fmp->wait_queue), TSK_WAIT_REASON_WFIXMEM, timeout); tn_enable_interrupt(); tn_switch_context(); //-- When returns to this point, in the 'data_elem' have to be valid value *p_data = tn_curr_run_task->data_elem; //-- Return to caller return tn_curr_run_task->task_wait_rc; } tn_enable_interrupt(); return rc; }
//---------------------------------------------------------------------------- // Acquire Semaphore Resource //---------------------------------------------------------------------------- int tn_sem_acquire(TN_SEM * sem, unsigned long timeout) { TN_INTSAVE_DATA int rc; //-- return code #if TN_CHECK_PARAM if(sem == NULL || timeout == 0) return TERR_WRONG_PARAM; if(sem->max_count == 0) return TERR_WRONG_PARAM; if(sem->id_sem != TN_ID_SEMAPHORE) return TERR_NOEXS; #endif TN_CHECK_NON_INT_CONTEXT tn_disable_interrupt(); if(sem->count >= 1) { sem->count--; rc = TERR_NO_ERR; } else { task_curr_to_wait_action(&(sem->wait_queue), TSK_WAIT_REASON_SEM, timeout); tn_enable_interrupt(); tn_switch_context(); return tn_curr_run_task->task_wait_rc; } tn_enable_interrupt(); return rc; }
//---------------------------------------------------------------------------- int tn_queue_receive(TN_DQUE * dque,void ** data_ptr,unsigned long timeout) { TN_INTSAVE_DATA int rc; //-- return code CDLL_QUEUE * que; TN_TCB * task; #if TN_CHECK_PARAM if(dque == NULL || timeout == 0 || data_ptr == NULL) return TERR_WRONG_PARAM; if(dque->id_dque != TN_ID_DATAQUEUE) return TERR_NOEXS; #endif TN_CHECK_NON_INT_CONTEXT tn_disable_interrupt(); rc = dque_fifo_read(dque,data_ptr); if(rc == TERR_NO_ERR) //-- There was entry(s) in data queue { if(!is_queue_empty(&(dque->wait_send_list))) { que = queue_remove_head(&(dque->wait_send_list)); task = get_task_by_tsk_queue(que); dque_fifo_write(dque,task->data_elem); //-- Put to data FIFO if(task_wait_complete(task)) { tn_enable_interrupt(); tn_switch_context(); return TERR_NO_ERR; } } } else //-- data FIFO is empty { if(!is_queue_empty(&(dque->wait_send_list))) { que = queue_remove_head(&(dque->wait_send_list)); task = get_task_by_tsk_queue(que); *data_ptr = task->data_elem; //-- Return to caller if(task_wait_complete(task)) { tn_enable_interrupt(); tn_switch_context(); return TERR_NO_ERR; } rc = TERR_NO_ERR; } else //-- wait_send_list is empty { task_curr_to_wait_action(&(dque->wait_receive_list), TSK_WAIT_REASON_DQUE_WRECEIVE,timeout); tn_enable_interrupt(); tn_switch_context(); //-- When returns to this point, in the data_elem have to be valid value *data_ptr = tn_curr_run_task->data_elem; //-- Return to caller return tn_curr_run_task->task_wait_rc; } } tn_enable_interrupt(); return rc; }
/** * * @param mutex * @param timeout * * @return TN_RETVAL */ TN_RETVAL tnec_mutex_lock (TN_MUTEX_S *mutex, TN_TIMEOUT timeout) { TN_UWORD tn_save_status_reg TN_UNUSED; /* for SR save */ TN_TCB_S *blk_task; TN_RETVAL rc; if (mutex == TN_NULL || timeout == 0) return TERR_WRONG_PARAM; if (mutex->id_mutex != TN_ID_MUTEX) return TERR_NOEXS; if (tn_is_non_task_context()) { return TERR_WCONTEXT; } tn_disable_interrupt(); rc = TERR_NO_ERR; for (;;) { if (tn_curr_run_task == mutex->holder) { rc = TERR_ILUSE; break; } if (mutex->attr == TN_MUTEX_ATTR_CEILING && tn_curr_run_task->base_priority < mutex->ceil_priority) { rc = TERR_ILUSE; break; } if (mutex->attr == TN_MUTEX_ATTR_CEILING) { if (mutex->holder == TN_NULL) { if (enable_lock_mutex(tn_curr_run_task, &blk_task)) { mutex->holder = tn_curr_run_task; tn_curr_run_task->blk_task = TN_NULL; queue_add_tail(&(tn_curr_run_task->mutex_queue), &(mutex->mutex_queue)); queue_add_tail(&tn_locked_mutexes_list, &(mutex->lock_mutex_queue)); } else { /* Невозможно заблокировать мютекс Протокол наследования: Если текущий приоритет выполняемой задачи больше приоритета задачи, которая блокирует текущую (удерживает мютекс), то блокирующая задача наследует приоритет текущей */ if (blk_task != TN_NULL) { if (tn_curr_run_task->priority < blk_task->priority) { set_current_priority(blk_task, tn_curr_run_task->priority); } } queue_add_tail(&tn_blocked_tasks_list, &(tn_curr_run_task->block_queue)); tn_curr_run_task->blk_task = blk_task; task_curr_to_wait_action(&(mutex->wait_queue), TSK_WAIT_REASON_MUTEX_C_BLK, timeout); tn_enable_interrupt(); tn_switch_context(); return tn_curr_run_task->task_wait_rc; } } else { if (tn_curr_run_task->priority < mutex->holder->priority) { set_current_priority(mutex->holder,tn_curr_run_task->priority); } task_curr_to_wait_action(&(mutex->wait_queue), TSK_WAIT_REASON_MUTEX_C, timeout); tn_enable_interrupt(); tn_switch_context(); return tn_curr_run_task->task_wait_rc; } } else if (mutex->attr == TN_MUTEX_ATTR_INHERIT) { if (mutex->holder == TN_NULL) { mutex->holder = tn_curr_run_task; queue_add_tail(&(tn_curr_run_task->mutex_queue),&(mutex->mutex_queue)); /* --- v.2.3 */ queue_add_tail(&tn_locked_mutexes_list,&(mutex->lock_mutex_queue)); /* --- */ rc = TERR_NO_ERR; } else { if (tn_curr_run_task->priority < mutex->holder->priority) { set_current_priority(mutex->holder, tn_curr_run_task->priority); } task_curr_to_wait_action(&(mutex->wait_queue), TSK_WAIT_REASON_MUTEX_I,timeout); tn_enable_interrupt(); tn_switch_context(); return tn_curr_run_task->task_wait_rc; } } break; } tn_enable_interrupt(); return rc; }