lab3.2: cv

This commit is contained in:
QcFe [PDS] 2020-05-28 22:06:08 +02:00
parent 6733fafc29
commit b79977ab99
2 changed files with 46 additions and 8 deletions

View File

@ -127,6 +127,8 @@ struct cv {
char *cv_name;
// add what you need here
// (don't forget to mark things volatile as needed)
struct wchan *wchan;
struct spinlock slk;
};
struct cv *cv_create(const char *name);

View File

@ -227,7 +227,7 @@ lock_release(struct lock *lock)
#elif OPT_WCHANLOCK
spinlock_acquire(&lock->slk);
lock->owner = NULL;
wchan_wakeall(lock->wchan, &lock->slk);
wchan_wakeone(lock->wchan, &lock->slk);
spinlock_release(&lock->slk);
#else
lock->owner = NULL;
@ -272,8 +272,15 @@ cv_create(const char *name)
return NULL;
}
// add stuff here as needed
#if OPT_WCHANLOCK
cv->wchan = wchan_create(name);
if (cv->wchan == NULL) {
kfree(cv->cv_name);
kfree(cv);
return NULL;
}
spinlock_init(&cv->slk);
#endif
return cv;
}
@ -282,8 +289,10 @@ cv_destroy(struct cv *cv)
{
KASSERT(cv != NULL);
// add stuff here as needed
#if OPT_WCHANLOCK
spinlock_cleanup(&cv->slk);
wchan_destroy(cv->wchan);
#endif
kfree(cv->cv_name);
kfree(cv);
}
@ -291,23 +300,50 @@ cv_destroy(struct cv *cv)
void
cv_wait(struct cv *cv, struct lock *lock)
{
// Write this
#if OPT_WCHANLOCK
KASSERT(cv != NULL);
KASSERT(lock_do_i_hold(lock));
spinlock_acquire(&cv->slk);
lock_release(lock);
wchan_sleep(cv->wchan,&cv->slk);
spinlock_release(&cv->slk);
lock_acquire(lock);
#else
(void)cv; // suppress warning until code gets written
(void)lock; // suppress warning until code gets written
#endif
}
void
cv_signal(struct cv *cv, struct lock *lock)
{
// Write this
#if OPT_WCHANLOCK
KASSERT(cv != NULL);
KASSERT(lock_do_i_hold(lock));
spinlock_acquire(&cv->slk);
wchan_wakeone(cv->wchan, &cv->slk);
spinlock_release(&cv->slk);
#else
(void)cv; // suppress warning until code gets written
(void)lock; // suppress warning until code gets written
#endif
}
void
cv_broadcast(struct cv *cv, struct lock *lock)
{
// Write this
#if OPT_WCHANLOCK
KASSERT(cv != NULL);
KASSERT(lock_do_i_hold(lock));
spinlock_acquire(&cv->slk);
wchan_wakeall(cv->wchan, &cv->slk);
spinlock_release(&cv->slk);
#else
(void)cv; // suppress warning until code gets written
(void)lock; // suppress warning until code gets written
#endif
}