mtx: remove the stupid

This commit is contained in:
anna 2021-10-16 04:23:05 +02:00
parent ad422894f2
commit 721ba69276
Signed by: fef
GPG key ID: EC22E476DC2D3D84
2 changed files with 27 additions and 24 deletions

View file

@ -21,8 +21,8 @@ int spin_trylock(spin_t *spin);
void spin_unlock(spin_t *spin); void spin_unlock(spin_t *spin);
struct lock_waiter { struct lock_waiter {
struct task *task;
struct clist clink; struct clist clink;
struct task *task;
}; };
struct mtx { struct mtx {

View file

@ -40,7 +40,6 @@ void mtx_init(struct mtx *mtx)
void mtx_lock(struct mtx *mtx) void mtx_lock(struct mtx *mtx)
{ {
while (1) {
/* /*
* acquire a lock on the wait queue before trying to claim the * acquire a lock on the wait queue before trying to claim the
* mutex itself to make sure the mutex isn't released while we * mutex itself to make sure the mutex isn't released while we
@ -49,24 +48,28 @@ void mtx_lock(struct mtx *mtx)
spin_lock(&mtx->wait_queue_lock); spin_lock(&mtx->wait_queue_lock);
if (atom_write(&mtx->lock, 1) == 0) { if (atom_write(&mtx->lock, 1) == 0) {
spin_unlock(&mtx->wait_queue_lock); spin_unlock(&mtx->wait_queue_lock);
break;
} else { } else {
struct task *cur = current; struct task *task = current;
/* /*
* It might not be the smartest idea to allocate this thing on * It might not be the smartest idea to allocate this thing on
* the stack because it's gonna blow up if the task somehow dies * the stack because it's gonna blow up if the task somehow dies
* before returning here. Let's see how this turns out. * before returning here. Let's see how this turns out.
*/ */
struct lock_waiter waiter = { struct lock_waiter waiter = {
.task = cur, .task = task,
}; };
clist_add(&mtx->wait_queue, &waiter.clink); clist_add(&mtx->wait_queue, &waiter.clink);
spin_unlock(&mtx->wait_queue_lock); spin_unlock(&mtx->wait_queue_lock);
cur->state = TASK_BLOCKED; task->state = TASK_BLOCKED;
/*
* This is only gonna return when the task currently owning the
* lock releases it. In that case, it doesn't unlock the mutex
* but merely switches back to us directly and thereby implicitly
* transfers the ownership of the mutex to us (see mtx_unlock()).
*/
schedule(); schedule();
} }
}
} }
int mtx_trylock(struct mtx *mtx) int mtx_trylock(struct mtx *mtx)