和很多通用的操作系统相比, 实时操作系统有自己的一个特点,那就是实时调度。通用操作系统的线程优先级一般是可以变化的,而实时系统的线程优先级却是不变的。之所以这么设计,是为了保证高优先级的任务在第一时间获得调度,这样才能保证调度的实时性。因为实时系统是严格按照优先级搞定调度的,所以不管什么时候,我们只要寻找到最高优先级的任务即可。
rawos系统可以支持256个优先级,对任务的创建个数也没有限制,所以就会出现多个任务共享一个优先级的情况。因此系统本身对同优先级的任务分配了定额的时间片,一旦该任务时间片用完,就会被放到优先级的末尾,直到获得下一次的调度机会,下面的代码就说明了这一情况,它是在时钟中断的时候被调度的,
void caculate_time_slice() { RAW_TASK_OBJ *task_ptr; LIST *head; RAW_SR_ALLOC(); task_ptr = raw_task_active; head = &raw_ready_queue.task_ready_list[task_ptr->priority]; RAW_CRITICAL_ENTER(); if (is_list_empty(head)) { RAW_CRITICAL_EXIT(); return; } /*there is only one task on this ready list, so do not need to caculate time slice*/ if (head->next->next == head) { RAW_CRITICAL_EXIT(); return; } if (task_ptr->time_slice) { task_ptr->time_slice--; } /*if current active task has time_slice, just return*/ if (task_ptr->time_slice) { RAW_CRITICAL_EXIT(); return; } /*Move current active task to the end of ready list for the same priority*/ move_to_ready_list_end(&raw_ready_queue, task_ptr); /*restore the task time slice*/ task_ptr->time_slice = task_ptr->time_total; RAW_CRITICAL_EXIT(); } |
上面说的是一个优先级下面有多个任务的情况,如果优先级本身只有一个任务,那么就很抱歉了,下面还得继续运行这个任务。另外,我们在windows上面编程的时候喜欢暂时释放线程的运行权利,调用sleep(0)即可,那么这在rawos上是怎么实现的呢,
RAW_U16 raw_sleep(RAW_U32 dly) { RAW_U16 error_status; RAW_SR_ALLOC(); #if (RAW_TASK_FUNCTION_CHECK > 0) if (raw_int_nesting) { return RAW_NOT_CALLED_BY_ISR; } #endif RAW_CRITICAL_ENTER(); if (dly) { /*system is locked so task can not sleep just return immediately*/ if (raw_sched_lock) { RAW_CRITICAL_EXIT(); return RAW_SCHED_DISABLE; } raw_task_active->task_state = RAW_DLY; tick_list_insert(raw_task_active, dly); remove_ready_list(&raw_ready_queue, raw_task_active); } else { /*make current task to the end of ready list*/ move_to_ready_list_end(&raw_ready_queue, raw_task_active); } RAW_CRITICAL_EXIT(); raw_sched(); if (dly) { /*task is timeout after sleep*/ error_status = block_state_post_process(raw_task_active, 0); } else { error_status = RAW_SUCCESS; } return error_status; } |