|
@@ -87,7 +87,7 @@ sem_destroy(struct semaphore *sem)
|
|
|
kfree(sem);
|
|
|
}
|
|
|
|
|
|
-void
|
|
|
+void
|
|
|
P(struct semaphore *sem)
|
|
|
{
|
|
|
KASSERT(sem != NULL);
|
|
@@ -147,8 +147,7 @@ V(struct semaphore *sem)
|
|
|
//
|
|
|
// Lock.
|
|
|
|
|
|
-struct lock *
|
|
|
-lock_create(const char * name)
|
|
|
+struct lock * lock_create(const char * name)
|
|
|
{
|
|
|
struct lock * lock = kmalloc(sizeof(struct lock));
|
|
|
|
|
@@ -165,45 +164,61 @@ lock_create(const char * name)
|
|
|
{
|
|
|
kfree(lock);
|
|
|
kfree(wc);
|
|
|
+ }
|
|
|
+
|
|
|
+ lock->owner = NULL;
|
|
|
+ spinlock_init(&lock->spin);
|
|
|
|
|
|
return lock;
|
|
|
}
|
|
|
|
|
|
-void
|
|
|
-lock_destroy(struct lock *lock)
|
|
|
+void lock_destroy(struct lock * lock)
|
|
|
{
|
|
|
- KASSERT(lock != NULL);
|
|
|
-
|
|
|
- // add stuff here as needed
|
|
|
-
|
|
|
- kfree(lock->lk_name);
|
|
|
- kfree(lock);
|
|
|
+ KASSERT(lock);
|
|
|
+
|
|
|
+ spinlock_cleanup(lock->spin);
|
|
|
+ wchan_destroy(lock->wc);
|
|
|
+ kfree(lock->lk_name);
|
|
|
+ kfree(lock->owner);
|
|
|
+ kfree(lock);
|
|
|
}
|
|
|
|
|
|
-void
|
|
|
-lock_acquire(struct lock *lock)
|
|
|
+void lock_acquire(struct lock * lock)
|
|
|
{
|
|
|
- // Write this
|
|
|
-
|
|
|
- (void)lock; // suppress warning until code gets written
|
|
|
+ KASSERT(lock);
|
|
|
+
|
|
|
+ spinlock_acquire(&(lock->spin));
|
|
|
+
|
|
|
+ if (lock->owner)
|
|
|
+ {
|
|
|
+ wchan_lock(lock->wc);
|
|
|
+ spinlock_release(&(lock->spin));
|
|
|
+ wchan_sleep(lock->wc);
|
|
|
+ spinlock_acquire(lock->wc);
|
|
|
+ }
|
|
|
+
|
|
|
+ lock->owner = curthread;
|
|
|
+ spinlock_release(lock->wc);
|
|
|
}
|
|
|
|
|
|
-void
|
|
|
-lock_release(struct lock *lock)
|
|
|
+void lock_release(struct lock * lock)
|
|
|
{
|
|
|
- // Write this
|
|
|
-
|
|
|
- (void)lock; // suppress warning until code gets written
|
|
|
+ KASSERT(lock);
|
|
|
+
|
|
|
+ spinlock_acquire(&(lock->spin));
|
|
|
+ KASSERT(curthread == lock->owner);
|
|
|
+ lock->owner = NULL;
|
|
|
+ wchan_wakeone(lock->wc);
|
|
|
+ spinlock_release(lock->spin);
|
|
|
}
|
|
|
|
|
|
-bool
|
|
|
-lock_do_i_hold(struct lock *lock)
|
|
|
+bool lock_do_i_hold(struct lock * lock)
|
|
|
{
|
|
|
- // Write this
|
|
|
-
|
|
|
- (void)lock; // suppress warning until code gets written
|
|
|
-
|
|
|
- return true; // dummy until code gets written
|
|
|
+ KASSERT(lock);
|
|
|
+
|
|
|
+ if (lock->owner == curthread) return 1;
|
|
|
+
|
|
|
+ return 0;
|
|
|
}
|
|
|
|
|
|
////////////////////////////////////////////////////////////
|