/* * uthread_setprio * * Changes the priority of the indicated thread. Note that if the thread * is in the UT_RUNNABLE state (it's runnable but not on cpu) you should * change the list it's waiting on so the effect of this call is * immediate. */ void uthread_setprio(uthread_id_t id, int prio) { uthread_t* thread_to_change = &uthreads[id]; if(thread_to_change->ut_state == UT_RUNNABLE){ int previous_prio = thread_to_change->ut_prio; thread_to_change->ut_prio = prio; utqueue_remove(&runq_table[previous_prio], thread_to_change); utqueue_enqueue(&runq_table[prio], thread_to_change); } else{ //change directly thread_to_change->ut_prio = prio; } return; }
/* * uthread_block * * Put the current thread to sleep, pending an appropriate call to * uthread_wake(). */ void uthread_block(void) { /* Already waiting? deadlock */ if (ut_curthr->ut_state == UT_WAIT) { uthread_switch(); } /* If the current thread is on cpu, then do context switch */ if (ut_curthr->ut_state == UT_ON_CPU) { ut_curthr->ut_state = UT_WAIT; uthread_switch(); } /* If the current thread is in the queue, remove it from the queue */ else if (ut_curthr->ut_state == UT_RUNNABLE) { ut_curthr->ut_state = UT_WAIT; utqueue_remove(&runq_table[ut_curthr->ut_prio], ut_curthr); } }
/* * uthread_setprio * * Changes the priority of the indicated thread. * param id: the uthread id * param prio: the priority to be set */ int uthread_setprio(uthread_id_t id, int prio) { /* If prio is invalid */ if (prio < 0 || prio > UTH_MAXPRIO) { ut_curthr->ut_errno = EINVAL; return EINVAL; } /* Invalid thread id */ if (id < 0 || id >= UTH_MAX_UTHREADS || uthreads[id].ut_state == UT_NO_STATE || uthreads[id].ut_state == UT_ZOMBIE) { ut_curthr->ut_errno = ESRCH; return ESRCH; } /* If the current state is runnable */ if (uthreads[id].ut_state == UT_RUNNABLE) { if (uthreads[id].ut_link.l_prev != NULL && uthreads[id].ut_link.l_next != NULL) utqueue_remove(&runq_table[uthreads[id].ut_prio], &uthreads[id]); uthreads[id].ut_prio = prio; utqueue_enqueue(&runq_table[prio], &uthreads[id]); } else { /* If it is waiting or on CPU, just set the priority */ /* Because it's not in the queue! */ uthreads[id].ut_prio = prio; } return 0; }