/* TODO: Release lock m and block the thread (enqueued on c). When unblocked, re-acquire m */ void condition_wait(lock_t * m, condition_t * c){ enter_critical(); lock_release_helper(m); block(&c->wait_queue); lock_acquire_helper(m); leave_critical(); }
/* Extra credit */ void do_setpriority(priority_t priority){ if( priority >= 1 ){ enter_critical(); current_running->priority = priority; leave_critical(); } }
// Return 0 on succes // Return 1 on failure due to deadlock (extra credit) int lock_acquire(lock_t * l){ enter_critical(); int result = lock_acquire_helper(l); leave_critical(); return result; }
/* TODO: Block until the semaphore value is greater than zero and decrement it */ void semaphore_down(semaphore_t * s){ enter_critical(); while(s->val <= 0) block(&s->wait_queue); s->val--; leave_critical(); }
/* Unblock the first thread waiting on c, if extant */ void condition_signal(condition_t * c) { enter_critical(); ASSERT(disable_count); unblock_one( &c->wait_queue ); leave_critical(); }
pid_t do_getpid(){ pid_t pid; enter_critical(); pid = current_running->pid; leave_critical(); return pid; }
/* Extra credit */ priority_t do_getpriority(){ priority_t priority; enter_critical(); priority = current_running->priority; leave_critical(); return priority; }
int main() { TIL311 = 0x98; srand(); sem_init(&lcd_sem); lcd_init(); serial_start(SERIAL_SAFE); puts("Hello! Starting tasks.\n"); enter_critical(); create_task(&task_time,0); create_task(&task_echo,0); create_task(&task_scroller,0); create_task(&task_wav_play,0); /* for (int i = 0; i < 16; i++) create_task(&breeder_task,0);*/ leave_critical(); yield(); puts("Tasks started, main() returning.\n"); return 0; }
/* Unblock all threads waiting on c */ void condition_broadcast(condition_t * c) { enter_critical(); ASSERT(disable_count); unblock_all( &c->wait_queue ); leave_critical(); }
/* Call scheduler to run the 'next' process */ void yield(void) { enter_critical(); current_running->yield_count++; scheduler_entry(); leave_critical(); }
/* TODO: Block until the semaphore value is greater than zero and decrement it */ void semaphore_down(semaphore_t * s){ enter_critical(); if (s->value > 0) --s->value; else block(&s->wait_queue); leave_critical(); }
/* TODO: Unblock the first thread waiting on c, if it exists */ void condition_signal(condition_t * c){ // disable int enter_critical(); // wake up unblock_one(&c->blocked); // enable int leave_critical(); }
/* TODO: Unblock all threads waiting on c */ void condition_broadcast(condition_t * c){ // disable int enter_critical(); // wake up unblock_all(&c->blocked); // enable int leave_critical(); }
/* TODO: Block until all n threads have called barrier_wait */ void barrier_wait(barrier_t * b){ enter_critical(); if(--b->num_left == 0) { unblock_all(&b->wait_queue); b->num_left = b->num_start; } else block(&b->wait_queue); leave_critical(); }
/* Increment the semaphore value atomically */ void semaphore_up(semaphore_t * s) { enter_critical(); if(s->value < 1 && !queue_empty(&s->wait_queue)) unblock_one(&s->wait_queue); else s->value++; leave_critical(); }
/* TODO: Block until all n threads have called barrier_wait */ void barrier_wait(barrier_t * b){ enter_critical(); if (++b->curr < b->n) { block(&b->wait_queue); } else { unblock_all(&b->wait_queue); b->curr = 0; } leave_critical(); }
/* Block until the semaphore value is greater than zero and decrement it */ void semaphore_down(semaphore_t * s) { enter_critical(); if( s->value < 1 ) block( &s->wait_queue ); else s->value--; leave_critical(); }
/* Change current_running to the next task */ void scheduler(){ ASSERT(disable_count); check_sleeping(); // wake up sleeping processes while (is_empty(&ready_queue)){ leave_critical(); enter_critical(); check_sleeping(); } current_running = (pcb_t *) dequeue(&ready_queue); ASSERT(NULL != current_running); ++current_running->entry_count; }
void do_sleep(int milliseconds) { enter_critical(); current_running->sleep_until = do_gettimeofday() + milliseconds; current_running->status = BLOCKED; queue_put_sort( &sleep_queue, (node_t*) current_running, &order_by_wake_up); scheduler_entry(); leave_critical(); }
int my_thread( void *dummy ) { int i, temp; for (i = 0; i < maximum; i++) { enter_critical(); temp = counter; temp += 1; counter = temp; leave_critical(); } return 0; }
/* TODO: Release lock m and block the thread (enqueued on c). When unblocked, re-acquire m */ void condition_wait(lock_t * m, condition_t * c){ // disable interrupts //enter_critical(); // release lock lock_release(m); enter_critical(); // block block(&c->blocked); leave_critical(); // acquire lock lock_acquire(m); // enable interrupts //leave_critical(); }
/* TODO: Blocking sleep. Caution: this function currently cannot be pre-empted! */ void do_sleep(int milliseconds){ ASSERT( !disable_count ); enter_critical(); uint64_t deadline; deadline = time_elapsed + milliseconds; current_running->deadline = deadline; current_running->status = SLEEPING; enqueue_sort(&sleep_wait_queue, (node_t *)current_running, (node_lte)<e_deadline); scheduler_entry(); leave_critical(); }
/* Block until all n threads have called barrier_wait */ void barrier_wait(barrier_t * b) { enter_critical(); if( b->size + 1 >= b->quorum ) { b->size = 0; unblock_all( &b->wait_queue ); } else { b->size ++; block( &b->wait_queue ); } leave_critical(); }
/* * q is a pointer to the waiting list where current_running should be * inserted. */ void block(pcb_t ** q, int *spinlock) { pcb_t *tmp; enter_critical(); if (spinlock) spinlock_release(spinlock); /* mark the job as blocked */ current_running->status = BLOCKED; /* Insert into waiting list */ tmp = *q; (*q) = current_running; current_running->next_blocked = tmp; /* remove job from ready queue, pick next job to run and dispatch it */ scheduler_entry(); leave_critical(); }
void lock_release(lock_t * l){ enter_critical(); lock_release_helper(l); leave_critical(); }
/* TODO: Unblock the first thread waiting on c, if it exists */ void condition_signal(condition_t * c){ enter_critical(); unblock_one(&c->wait_queue); leave_critical(); }
/* TODO: Increment the semaphore value atomically */ void semaphore_up(semaphore_t * s){ enter_critical(); if(s->val++ == 0) unblock_one(&s->wait_queue); leave_critical(); }
/* TODO: Unblock all threads waiting on c */ void condition_broadcast(condition_t * c){ enter_critical(); unblock_all(&c->wait_queue); leave_critical(); }
/* TODO: Increment the semaphore value atomically */ void semaphore_up(semaphore_t * s){ enter_critical(); if (!unblock_one(&s->wait_queue)) ++s->value; leave_critical(); }
void do_yield(){ enter_critical(); put_current_running(); scheduler_entry(); leave_critical(); }