summaryrefslogtreecommitdiff
path: root/kern/mutex.h
diff options
context:
space:
mode:
authorRichard Braun <rbraun@sceen.net>2013-04-08 21:40:25 +0200
committerRichard Braun <rbraun@sceen.net>2013-04-08 21:40:25 +0200
commitc0fb9d9c300480a865158be6dc78e9225e0b310e (patch)
tree76d029e5c8c69d9015436be6d1e92523d85de24e /kern/mutex.h
parente2cfad45825f974e544c2b5788392381e2f1b87b (diff)
kern/mutex: new module
As the name implies, this module provides sleepable mutual exclusion locks.
Diffstat (limited to 'kern/mutex.h')
-rw-r--r--kern/mutex.h53
1 files changed, 53 insertions, 0 deletions
diff --git a/kern/mutex.h b/kern/mutex.h
new file mode 100644
index 00000000..2f46ca8c
--- /dev/null
+++ b/kern/mutex.h
@@ -0,0 +1,53 @@
+/*
+ * Copyright (c) 2013 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/>.
+ *
+ *
+ * Mutual exclusion locks.
+ *
+ * Unlike spin locks, acquiring a mutex may make the calling thread sleep.
+ */
+
+#ifndef _KERN_MUTEX_H
+#define _KERN_MUTEX_H
+
+#include <kern/list.h>
+#include <kern/spinlock.h>
+
+#define MUTEX_UNLOCKED 0
+#define MUTEX_LOCKED 1
+#define MUTEX_CONTENDED 2
+
+struct mutex {
+ unsigned long state;
+ struct spinlock lock;
+ struct list waiters;
+};
+
+#define MUTEX_INITIALIZER(mutex) \
+ { MUTEX_UNLOCKED, SPINLOCK_INITIALIZER, LIST_INITIALIZER((mutex).waiters) }
+
+void mutex_init(struct mutex *mutex);
+
+/*
+ * Return 0 on success, 1 if busy.
+ */
+int mutex_trylock(struct mutex *mutex);
+
+void mutex_lock(struct mutex *mutex);
+
+void mutex_unlock(struct mutex *mutex);
+
+#endif /* _KERN_MUTEX_H */