From d28cd672a56342e4dcdbb51fe63a13e61eeefdd4 Mon Sep 17 00:00:00 2001 From: Richard Braun Date: Fri, 17 Mar 2017 21:16:37 +0100 Subject: kern/semaphore: new module --- kern/semaphore.c | 60 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100644 kern/semaphore.c (limited to 'kern/semaphore.c') diff --git a/kern/semaphore.c b/kern/semaphore.c new file mode 100644 index 00000000..e41e2c2f --- /dev/null +++ b/kern/semaphore.c @@ -0,0 +1,60 @@ +/* + * Copyright (c) 2017 Richard Braun. + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#include +#include + +#include +#include +#include + +void +semaphore_wait_slow(struct semaphore *semaphore) +{ + struct sleepq *sleepq; + unsigned int prev; + + sleepq = sleepq_lend(semaphore, false); + + for (;;) { + prev = semaphore_dec(semaphore); + + if (prev != 0) { + break; + } + + sleepq_wait(sleepq, "sem"); + } + + sleepq_return(sleepq); +} + +void +semaphore_post_slow(struct semaphore *semaphore) +{ + struct sleepq *sleepq; + + sleepq = sleepq_acquire(semaphore, false); + + if (sleepq == NULL) { + return; + } + + sleepq_signal(sleepq); + + sleepq_release(sleepq); +} -- cgit v1.2.3