summaryrefslogtreecommitdiff
path: root/kern/semaphore.c
diff options
context:
space:
mode:
authorRichard Braun <rbraun@sceen.net>2017-03-17 21:16:37 +0100
committerRichard Braun <rbraun@sceen.net>2017-03-17 21:16:37 +0100
commitd28cd672a56342e4dcdbb51fe63a13e61eeefdd4 (patch)
tree54b60096dfe6d96d51a758a9f24a6c9135ab7e59 /kern/semaphore.c
parenta6a4c99fda54e3058387a465ddda76019033f789 (diff)
kern/semaphore: new module
Diffstat (limited to 'kern/semaphore.c')
-rw-r--r--kern/semaphore.c60
1 files changed, 60 insertions, 0 deletions
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 <http://www.gnu.org/licenses/>.
+ */
+
+#include <stdbool.h>
+#include <stddef.h>
+
+#include <kern/semaphore.h>
+#include <kern/semaphore_i.h>
+#include <kern/sleepq.h>
+
+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);
+}