void lock_acquire(struct lock *lock) { KASSERT(lock != NULL); KASSERT(curthread->t_in_interrupt == false); spinlock_acquire(&lock->lk_spinlock); //if (lock_do_i_hold(lock)) //{ // spinlock_release(&lock->lk_spinlock); // return; // } //else //{ while(lock->lk_owner != NULL) { wchan_lock(lock->lk_wchan); spinlock_release(&lock->lk_spinlock); wchan_sleep(lock->lk_wchan); //returns from here spinlock_acquire(&lock->lk_spinlock); } KASSERT(lock->lk_owner == NULL); lock->lk_owner = curthread; // } spinlock_release(&lock->lk_spinlock); }
static void sleepalot_thread(void *junk, unsigned long num) { int i, j; (void)junk; for (i=0; i<SLEEPALOT_PRINTS; i++) { for (j=0; j<SLEEPALOT_ITERS; j++) { unsigned n; struct spinlock *lk; struct wchan *wc; n = random() % NWAITCHANS; lk = &spinlocks[n]; wc = waitchans[n]; spinlock_acquire(lk); wchan_sleep(wc, lk); spinlock_release(lk); } kprintf("[%lu]", num); } V(donesem); }
void lock_acquire(struct lock *lock) { //edit #if OPT_A1 KASSERT(lock != NULL); KASSERT(curthread->t_in_interrupt == false); spinlock_acquire(&lock->lock_lock); while((lock->lock_holder != NULL) && !(lock_do_i_hold(lock))){ wchan_lock(lock->lk_wchan); spinlock_release(&lock->lock_lock); wchan_sleep(lock->lk_wchan); spinlock_acquire(&lock->lock_lock); } if(lock_do_i_hold(lock)){} else{ KASSERT(lock->lock_holder == NULL); lock->lock_holder = curthread; } spinlock_release(&lock->lock_lock); #endif }
void lock_acquire(struct lock *lock) { #if OPT_A1 KASSERT(lock != NULL); KASSERT(curthread->t_in_interrupt == false); spinlock_acquire(&lock->lk_spinlock); // We sleep this thread on the wait channel // until the lock is available. while (!(lock->available)) { wchan_lock(lock->lock_wchan); spinlock_release(&lock->lk_spinlock); wchan_sleep(lock->lock_wchan); spinlock_acquire(&lock->lk_spinlock); } KASSERT(lock->available == true); lock->initial_thread = curthread; lock->available = false; spinlock_release(&lock->lk_spinlock); #else (void*)lock; #endif }
void lock_acquire(struct lock *lock) { // Write this KASSERT(lock!=NULL); //KASSERT(sem != NULL); KASSERT(curthread->t_in_interrupt == false); spinlock_acquire(&lock->mut_lock); while (lock->hold ) { wchan_lock(lock->mut_wchan); spinlock_release(&lock->mut_lock); wchan_sleep(lock->mut_wchan); spinlock_acquire(&lock->mut_lock); } KASSERT(lock->hold ==0 ); lock->hold=1; lock->holder=curthread; //sem->sem_count--; spinlock_release(&lock->mut_lock); //(void)lock; // suppress warning until code gets written }
void P(struct semaphore *sem) { KASSERT(sem != NULL); /* * May not block in an interrupt handler. * * For robustness, always check, even if we can actually * complete the P without blocking. */ KASSERT(curthread->t_in_interrupt == false); spinlock_acquire(&sem->sem_lock); while (sem->sem_count == 0) { wchan_lock(sem->sem_wchan); spinlock_release(&sem->sem_lock); wchan_sleep(sem->sem_wchan); spinlock_acquire(&sem->sem_lock); } KASSERT(sem->sem_count > 0); sem->sem_count--; spinlock_release(&sem->sem_lock); }
void cv_wait(struct cv *cv, struct lock *lock) { // Write this //make interupt thingy // make sure i own the lock? KASSERT(cv != NULL); KASSERT(lock != NULL); KASSERT(lock_do_i_hold(lock)); KASSERT(curthread->t_in_interrupt == false); wchan_lock(cv->cv_wchan); lock_release(lock); wchan_sleep(cv->cv_wchan); lock_acquire(lock); // (void)cv; // suppress warning until code gets written // (void)lock; // suppress warning until code gets written }
void cv_wait(struct cv *cv, struct lock *lock) { #if OPT_A1 KASSERT(lock != NULL); KASSERT(lock_do_i_hold(lock)); spinlock_acquire(&cv->cv_spinlock); lock_release(lock); wchan_lock(cv->cv_wchan); spinlock_release(&cv->cv_spinlock); wchan_sleep(cv->cv_wchan); lock_acquire(lock); #else (void)cv; // suppress warning until code gets written (void)lock; // suppress warning until code gets written #endif }
void lock_acquire(struct lock *lock) { // Write this #if OPT_A1 KASSERT(lock != NULL); KASSERT(curthread->t_in_interrupt == false); spinlock_acquire(&lock->lk_spinlock); while (lock->is_locked){ wchan_lock(lock->lk_wchan); spinlock_release(&lock->lk_spinlock); wchan_sleep(lock->lk_wchan); spinlock_acquire(&lock->lk_spinlock); } KASSERT(lock->is_locked == false); lock->is_locked = true; lock->lk_owner = curthread; spinlock_release(&lock->lk_spinlock); #else (void)lock; // suppress warning until code gets written #endif }
void P(struct semaphore *sem) { KASSERT(sem != NULL); /* * May not block in an interrupt handler. * * For robustness, always check, even if we can actually * complete the P without blocking. */ KASSERT(curthread->t_in_interrupt == false); /* Use the semaphore spinlock to protect the wchan as well. */ spinlock_acquire(&sem->sem_lock); while (sem->sem_count == 0) { /* * * Note that we don't maintain strict FIFO ordering of * threads going through the semaphore; that is, we * might "get" it on the first try even if other * threads are waiting. Apparently according to some * textbooks semaphores must for some reason have * strict ordering. Too bad. :-) * * Exercise: how would you implement strict FIFO * ordering? */ wchan_sleep(sem->sem_wchan, &sem->sem_lock); } KASSERT(sem->sem_count > 0); sem->sem_count--; spinlock_release(&sem->sem_lock); }
void lock_acquire(struct lock *lock) { // Write this //Peng 2.19.2016 KASSERT(lock != NULL); /* * May not block in an interrupt handler. * * For robustness, always check, even if we can actually * complete the lock_acquire without blocking. */ KASSERT(curthread->t_in_interrupt == false); KASSERT(!lock_do_i_hold(lock)); /* Use the lock spinlock to protect the wchan. */ spinlock_acquire(&lock->lock_splk); while (lock->held) { /* * Note that we don't maintain strict FIFO ordering of * threads going through the lock; that is, we * might "get" it on the first try even if other * threads are waiting. */ //spinlock_release(&lock->lock_splk); wchan_sleep(lock->lock_wchan, &lock->lock_splk); //spinlock_acquire(&lock->lock_splk); } KASSERT(!lock->held); lock->held=true; lock->holder=curthread; spinlock_release(&lock->lock_splk); //Peng //(void)lock; // suppress warning until code gets written }
/* * Wait for the thread count to equal tc. */ void thread_wait_for_count(unsigned tc) { spinlock_acquire(&thread_count_lock); while (thread_count != tc) { wchan_sleep(thread_count_wchan, &thread_count_lock); } spinlock_release(&thread_count_lock); }
/* * coremap_pinwait: wait for a pinned page to unpin. */ static void coremap_pinwait(void) { wchan_lock(coremap_pinchan); spinlock_release(&coremap_spinlock); wchan_sleep(coremap_pinchan); spinlock_acquire(&coremap_spinlock); }
void cv_wait(struct cv *cv, struct lock *lock) { KASSERT( cv != NULL ); wchan_lock(cv->cv_wchan); lock_release(lock); wchan_sleep(cv->cv_wchan); lock_acquire(lock); }
/* * Wait for shootdown to complete. */ static void tlb_shootwait(void) { wchan_lock(coremap_shootchan); spinlock_release(&coremap_spinlock); wchan_sleep(coremap_shootchan); spinlock_acquire(&coremap_spinlock); }
/*when cv wait is been called, it means the condition is currently not meet. Then we release the lock the thread is holding and go to sleep, after the thread weaks up, it must reacquire the lock before exit from this func*/ void cv_wait(struct cv *cv, struct lock *lock) { // spinlock_acquire(&cv->cv_lock); lock_release(lock); wchan_lock(cv->cv_wchan); //spinlock_release(&cv->cv_lock); wchan_sleep(cv->cv_wchan); lock_acquire(lock); }
void cv_wait(struct cv *cv, struct lock *lock) { // Write this wchan_lock(cv->cv_wchan); lock_release(lock); wchan_sleep(cv->cv_wchan); // after wake up, lock should be acquired again lock_acquire(lock); }
void cv_wait(struct cv *cv, struct lock *lock) { KASSERT(cv); KASSERT(lock); spinlock_acquire(&cv->cv_lock); lock_release(lock); wchan_sleep(cv->cv_wchan, &cv->cv_lock); spinlock_release(&cv->cv_lock); lock_acquire(lock); }
void lock_acquire(struct lock *lock) { // Write this KASSERT(!lock_do_i_hold(lock)); spinlock_acquire(&lock->lk_lock); while (lock->lk_holder != NULL) { wchan_sleep(lock->lk_wchan, &lock->lk_lock); } lock->lk_holder = curthread; spinlock_release(&lock->lk_lock); }
void cv_wait(struct cv *cv, struct lock *lock) { KASSERT(cv != NULL); KASSERT(lock != NULL); if(lock_do_i_hold(lock)) { spinlock_acquire(&cv->spin_lock); lock_release(lock); wchan_sleep(cv->cv_wchan, &cv->spin_lock); spinlock_release(&cv->spin_lock); lock_acquire(lock); } }
void lock_acquire(struct lock *lock) { if(!lock_do_i_hold(lock)) { spinlock_acquire(&lock->spin_lock); while(spinlock_data_testandset(&lock->lk_busy) != 0) { wchan_sleep(lock->lk_wchan, &lock->spin_lock); } lock->lk_cpu = curcpu; lock->lk_thread = curthread; spinlock_release(&lock->spin_lock); } }
void cv_wait(struct cv *cv, struct lock *lock) { // Write this // spinlock_acquire(&cv->cv_lock); KASSERT(lock_do_i_hold(lock)); spinlock_acquire(&cv->cv_lock); lock_release(lock); wchan_sleep(cv->cv_wchan, &cv->cv_lock); spinlock_release(&cv->cv_lock); lock_acquire(lock); }
void cv_wait(struct cv *cv, struct lock *lock) { // Write this #if OPT_A1 KASSERT(lock != NULL); wchan_lock(cv->cv_wchan); lock_release(lock); wchan_sleep(cv->cv_wchan); lock_acquire(lock); #endif // (void)cv; // suppress warning until code gets written // (void)lock; // suppress warning until code gets written }
void cv_wait(struct cv *cv, struct lock *lock) { // Write this KASSERT(cv != NULL); KASSERT(lock != NULL); KASSERT(lock_do_i_hold(lock) == true); spinlock_acquire(&cv->cv_spinlock); lock_release(lock); wchan_sleep(cv->cv_wchan, &cv->cv_spinlock); spinlock_release(&cv->cv_spinlock); lock_acquire(lock); // acquiring lock after releaseing cv_spinlock as cpu can hold only one spinlock at a time, Also this is correct as the thread should continue running // immediately after waking up }
void cv_wait(struct cv *mycv, struct lock *mylock) { // Write this spinlock_acquire(&mycv->cv_spinlock); lock_release(mylock); wchan_lock(mycv->cv_wchan); spinlock_release(&mycv->cv_spinlock); wchan_sleep(mycv->cv_wchan); lock_acquire(mylock); //end of added stuff // (void)mycv; // suppress warning until code gets written // (void)mylock; // suppress warning until code gets written }
void cv_wait(struct cv *cv, struct lock *lock) { // Write this //Peng 2.21.2016 KASSERT(cv!=NULL); KASSERT(lock!=NULL); spinlock_acquire(&cv->cv_splk); lock_release(lock); wchan_sleep(cv->cv_wchan, &cv->cv_splk); spinlock_release(&cv->cv_splk); lock_acquire(lock); //Peng //(void)cv; // suppress warning until code gets written //(void)lock; // suppress warning until code gets written }
void cv_wait(struct cv *cv, struct lock *lock) { KASSERT(cv!=NULL); KASSERT(lock!=NULL); //KASSERT(lock_do_i_hold(lock)); wchan_lock(cv->cv_wchan); lock_release(lock); wchan_sleep(cv->cv_wchan); lock_acquire(lock); // Write this //(void)cv; // suppress warning until code gets written //(void)lock; // suppress warning until code gets written }
/* when acquire a lcok it must in critical section, and we check if the lock is been using or not. If the lock already been using then the coming thread will be blocked in wchan. If the lock is avaliable the change the lock_holder to the curthread's name, this is important flag help us later to check who holding this lock, and is this lock is avaliable to acquire */ void lock_acquire(struct lock *lock) { KASSERT(lock != NULL); KASSERT(curthread->t_in_interrupt == false); spinlock_acquire(&lock->lock_lock); while (lock->lock_holder != NULL) { wchan_lock(lock->lock_wchan); spinlock_release(&lock->lock_lock); wchan_sleep(lock->lock_wchan); spinlock_acquire(&lock->lock_lock); } KASSERT(lock->lock_holder == NULL); lock->lock_holder = curthread; spinlock_release(&lock->lock_lock); }
void lock_acquire(struct lock *lock) { KASSERT(lock != NULL); spinlock_acquire(&lock->lk_spinlock); while(lock->lk_owner != NULL) { wchan_lock(lock->lk_wchan); spinlock_release(&lock->lk_spinlock); wchan_sleep(lock->lk_wchan); spinlock_acquire(&lock->lk_spinlock); } KASSERT(lock->lk_owner == NULL); lock->lk_owner = curthread; spinlock_release(&lock->lk_spinlock); }
void lock_acquire(struct lock *lock) { // Write this /*DEBUG(DB_THREADS, "Inside lock_acquire");*/ struct thread *mythread; KASSERT(lock != NULL); //KASSERT(curthread->t_in_interrupt == false); spinlock_acquire(&lock->lk_spinlock); while(lock->lock_hold==1){ wchan_lock(lock->lock_wchan); spinlock_release(&lock->lk_spinlock); wchan_sleep(lock->lock_wchan); spinlock_acquire(&lock->lk_spinlock); } /* else if(lock->lock_hold==0){ if (CURCPU_EXISTS()) { mythread = curthread; if (lock->lk_thread == mythread) { panic("Deadlock on thread %p\n", lock); } } else { mythread = NULL; } */ if(curthread!= NULL){ mythread = curthread; lock->lk_thread = mythread; } lock->lock_hold= 1; spinlock_release(&lock->lk_spinlock); //KASSERT(lock->lock_count > 0); //(void)lock; // suppress warning until code gets written /*DEBUG(DB_THREADS, "Exiting lock_acquire");*/ }