From 4278f99adcbcfbd52904c0d8809184afe091c958 Mon Sep 17 00:00:00 2001 From: Agustina Arzille Date: Fri, 21 Jul 2017 00:49:39 +0200 Subject: Rework mutex implementation selection --- kern/mutex/mutex_plain.c | 65 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) create mode 100644 kern/mutex/mutex_plain.c (limited to 'kern/mutex/mutex_plain.c') diff --git a/kern/mutex/mutex_plain.c b/kern/mutex/mutex_plain.c new file mode 100644 index 00000000..a925a5a2 --- /dev/null +++ b/kern/mutex/mutex_plain.c @@ -0,0 +1,65 @@ +/* + * 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 . + */ + +#include +#include + +#include +#include +#include +#include + +void +mutex_plain_lock_slow(struct mutex *mutex) +{ + unsigned int state; + struct sleepq *sleepq; + unsigned long flags; + + sleepq = sleepq_lend(mutex, false, &flags); + + for (;;) { + state = atomic_swap_release(&mutex->state, MUTEX_CONTENDED); + + if (state == MUTEX_UNLOCKED) { + break; + } + + sleepq_wait(sleepq, "mutex"); + } + + if (sleepq_empty(sleepq)) { + /* TODO Review memory order */ + atomic_store(&mutex->state, MUTEX_LOCKED, ATOMIC_RELEASE); + } + + sleepq_return(sleepq, flags); +} + +void +mutex_plain_unlock_slow(struct mutex *mutex) +{ + struct sleepq *sleepq; + unsigned long flags; + + sleepq = sleepq_acquire(mutex, false, &flags); + + if (sleepq != NULL) { + sleepq_signal(sleepq); + sleepq_release(sleepq, flags); + } +} -- cgit v1.2.3