From f51ccd30817357ad23bc8c13a66db9b89569b1c5 Mon Sep 17 00:00:00 2001 From: sbosse Date: Mon, 14 Oct 2024 23:06:59 +0200 Subject: [PATCH] Mon 14 Oct 23:06:38 CEST 2024 --- kernel/mutex.c | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 kernel/mutex.c diff --git a/kernel/mutex.c b/kernel/mutex.c new file mode 100644 index 0000000..87cc73d --- /dev/null +++ b/kernel/mutex.c @@ -0,0 +1,28 @@ +/* +Copyright (C) 2015-2019 The University of Notre Dame +This software is distributed under the GNU General Public License. +See the file LICENSE for details. +*/ + +#include "mutex.h" +#include "interrupt.h" +#include "process.h" + +void mutex_lock(struct mutex *m) +{ + interrupt_block(); + while(m->locked) { + process_wait(&m->waitqueue); + interrupt_block(); + } + m->locked = 1; + interrupt_unblock(); +} + +void mutex_unlock(struct mutex *m) +{ + interrupt_block(); + m->locked = 0; + process_wakeup(&m->waitqueue); + interrupt_unblock(); +}