lab3.1: locks
This commit is contained in:
@@ -156,6 +156,24 @@ lock_create(const char *name)
|
||||
|
||||
// add stuff here as needed
|
||||
|
||||
#if OPT_SEMLOCK
|
||||
lock->sem = sem_create(name, 1);
|
||||
if (lock->sem == NULL) {
|
||||
kfree(lock->lk_name);
|
||||
kfree(lock);
|
||||
return NULL;
|
||||
}
|
||||
#elif OPT_WCHANLOCK
|
||||
lock->wchan = wchan_create(lock->lk_name);
|
||||
if (lock->wchan == NULL) {
|
||||
kfree(lock->lk_name);
|
||||
kfree(lock);
|
||||
return NULL;
|
||||
}
|
||||
spinlock_init(&lock->slk);
|
||||
#endif
|
||||
|
||||
lock->owner = NULL;
|
||||
return lock;
|
||||
}
|
||||
|
||||
@@ -167,6 +185,12 @@ lock_destroy(struct lock *lock)
|
||||
// add stuff here as needed
|
||||
|
||||
kfree(lock->lk_name);
|
||||
#if OPT_SEMLOCK
|
||||
sem_destroy(lock->sem);
|
||||
#elif OPT_WCHANLOCK
|
||||
wchan_destroy(lock->wchan);
|
||||
spinlock_cleanup(&lock->slk);
|
||||
#endif
|
||||
kfree(lock);
|
||||
}
|
||||
|
||||
@@ -175,7 +199,19 @@ lock_acquire(struct lock *lock)
|
||||
{
|
||||
// Write this
|
||||
|
||||
(void)lock; // suppress warning until code gets written
|
||||
#if OPT_SEMLOCK
|
||||
P(lock->sem);
|
||||
lock->owner = curthread;
|
||||
#elif OPT_WCHANLOCK
|
||||
spinlock_acquire(&lock->slk);
|
||||
while (lock->owner != NULL) {
|
||||
wchan_sleep(lock->wchan, &lock->slk);
|
||||
}
|
||||
lock->owner = curthread;
|
||||
spinlock_release(&lock->slk);
|
||||
#else
|
||||
lock->owner = curthread;
|
||||
#endif
|
||||
}
|
||||
|
||||
void
|
||||
@@ -183,17 +219,36 @@ lock_release(struct lock *lock)
|
||||
{
|
||||
// Write this
|
||||
|
||||
(void)lock; // suppress warning until code gets written
|
||||
KASSERT(lock != NULL);
|
||||
KASSERT(lock_do_i_hold(lock));
|
||||
#if OPT_SEMLOCK
|
||||
lock->owner = NULL;
|
||||
V(lock->sem);
|
||||
#elif OPT_WCHANLOCK
|
||||
spinlock_acquire(&lock->slk);
|
||||
lock->owner = NULL;
|
||||
wchan_wakeall(lock->wchan, &lock->slk);
|
||||
spinlock_release(&lock->slk);
|
||||
#else
|
||||
lock->owner = NULL;
|
||||
#endif
|
||||
}
|
||||
|
||||
bool
|
||||
lock_do_i_hold(struct lock *lock)
|
||||
{
|
||||
// Write this
|
||||
KASSERT(lock != NULL);
|
||||
#if OPT_WCHANLOCK
|
||||
bool ret = true;
|
||||
spinlock_acquire(&lock->slk);
|
||||
ret = lock->owner == curthread;
|
||||
spinlock_release(&lock->slk);
|
||||
return ret; // dummy until code gets written
|
||||
#else
|
||||
return lock->owner == curthread;
|
||||
#endif
|
||||
|
||||
(void)lock; // suppress warning until code gets written
|
||||
|
||||
return true; // dummy until code gets written
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////
|
||||
|
||||
Reference in New Issue
Block a user