From c63f340d90800895f007de64b7d2d14624263331 Mon Sep 17 00:00:00 2001 From: nthnluu Date: Sun, 28 Jan 2024 21:20:27 -0500 Subject: Created student weenix repository --- kernel/include/proc/kmutex.h | 60 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100644 kernel/include/proc/kmutex.h (limited to 'kernel/include/proc/kmutex.h') 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); -- cgit v1.2.3-70-g09d2