|
|
|
@ -94,13 +94,79 @@ void mtx_unlock(struct mtx *mtx)
|
|
|
|
|
clist_del_first_entry(&mtx->wait_queue, typeof(*waiter), clink);
|
|
|
|
|
spin_unlock(&mtx->wait_queue_lock);
|
|
|
|
|
waiter->task->state = TASK_READY;
|
|
|
|
|
switch_to(waiter->task, current);
|
|
|
|
|
} else {
|
|
|
|
|
atom_write(&mtx->lock, 0);
|
|
|
|
|
spin_unlock(&mtx->wait_queue_lock);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void sem_init(struct sem *sem, int initial_count)
|
|
|
|
|
{
|
|
|
|
|
atom_write(&sem->count, initial_count);
|
|
|
|
|
spin_init(&sem->wait_queue_lock);
|
|
|
|
|
clist_init(&sem->wait_queue);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int sem_down(struct sem *sem)
|
|
|
|
|
{
|
|
|
|
|
# ifdef DEBUG
|
|
|
|
|
if (in_irq()) {
|
|
|
|
|
kprintf("sem_down() called from IRQ context!\n");
|
|
|
|
|
spin_loop {
|
|
|
|
|
int old = atom_sub(&sem->count, 1);
|
|
|
|
|
if (old >= 0)
|
|
|
|
|
return old;
|
|
|
|
|
atom_inc(&sem->count);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
# endif
|
|
|
|
|
|
|
|
|
|
int ret = atom_sub(&sem->count, 1);
|
|
|
|
|
|
|
|
|
|
if (ret < 0) {
|
|
|
|
|
struct task *task = current;
|
|
|
|
|
struct lock_waiter waiter = {
|
|
|
|
|
.task = task,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
spin_lock(&sem->wait_queue_lock);
|
|
|
|
|
clist_add(&sem->wait_queue, &waiter.clink);
|
|
|
|
|
spin_unlock(&sem->wait_queue_lock);
|
|
|
|
|
|
|
|
|
|
task->state = TASK_BLOCKED;
|
|
|
|
|
schedule();
|
|
|
|
|
ret = 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int sem_up(struct sem *sem)
|
|
|
|
|
{
|
|
|
|
|
int ret = atom_add(&sem->count, 1);
|
|
|
|
|
|
|
|
|
|
if (ret <= 0) {
|
|
|
|
|
spin_lock(&sem->wait_queue_lock);
|
|
|
|
|
struct lock_waiter *waiter =
|
|
|
|
|
clist_del_first_entry(&sem->wait_queue, typeof(*waiter), clink);
|
|
|
|
|
spin_unlock(&sem->wait_queue_lock);
|
|
|
|
|
|
|
|
|
|
waiter->task->state = TASK_READY;
|
|
|
|
|
ret = 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int sem_trydown(struct sem *sem)
|
|
|
|
|
{
|
|
|
|
|
int ret = atom_sub(&sem->count, 1);
|
|
|
|
|
|
|
|
|
|
if (ret < 0) {
|
|
|
|
|
atom_inc(&sem->count);
|
|
|
|
|
ret = -EAGAIN;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* This file is part of GayBSD.
|
|
|
|
|
* Copyright (c) 2021 fef <owo@fef.moe>.
|
|
|
|
|