aboutsummaryrefslogtreecommitdiff
path: root/kernel/include/proc/kmutex.h
diff options
context:
space:
mode:
authornthnluu <nate1299@me.com>2024-01-28 21:20:27 -0500
committernthnluu <nate1299@me.com>2024-01-28 21:20:27 -0500
commitc63f340d90800895f007de64b7d2d14624263331 (patch)
tree2c0849fa597dd6da831c8707b6f2603403778d7b /kernel/include/proc/kmutex.h
Created student weenix repository
Diffstat (limited to 'kernel/include/proc/kmutex.h')
-rw-r--r--kernel/include/proc/kmutex.h60
1 files changed, 60 insertions, 0 deletions
diff --git a/kernel/include/proc/kmutex.h b/kernel/include/proc/kmutex.h
new file mode 100644
index 0000000..37d8ece
--- /dev/null
+++ b/kernel/include/proc/kmutex.h
@@ -0,0 +1,60 @@
+#pragma once
+
+#include "proc/sched.h"
+#include "proc/spinlock.h"
+
+/*===========
+ * Structures
+ *==========*/
+
+typedef struct kmutex
+{
+ ktqueue_t km_waitq; /* wait queue */
+ struct kthread *km_holder; /* current holder */
+ list_link_t km_link;
+} kmutex_t;
+
+#define KMUTEX_INITIALIZER(mtx) \
+ { \
+ .km_waitq = KTQUEUE_INITIALIZER((mtx).km_waitq), .km_holder = NULL, \
+ .km_link = LIST_LINK_INITIALIZER((mtx).km_link), \
+ }
+
+/*==========
+ * Functions
+ *=========*/
+
+/**
+ * Initializes a mutex.
+ *
+ * @param mtx the mutex
+ */
+void kmutex_init(kmutex_t *mtx);
+
+/**
+ * Locks the specified mutex.
+ *
+ * Note: This function may block.
+ *
+ * Note: These locks are not re-entrant
+ *
+ * @param mtx the mutex to lock
+ */
+void kmutex_lock(kmutex_t *mtx);
+
+/**
+ * Unlocks the specified mutex.
+ *
+ * @param mtx the mutex to unlock
+ */
+void kmutex_unlock(kmutex_t *mtx);
+
+/**
+ * Indicates if a mutex has waiters.
+ */
+long kmutex_has_waiters(kmutex_t *mtx);
+
+/**
+ * Indicates if curthr owns a mutex.
+ */
+long kmutex_owns_mutex(kmutex_t *mtx);