From 25f22702f153d62b55cb276152ba5ea935023e6e Mon Sep 17 00:00:00 2001 From: Agustina Arzille Date: Wed, 1 Feb 2017 12:32:52 -0300 Subject: Implement basic sleeping locks for gnumach * kern/atomic.h: New file. * kern/kmutex.h: New file. * kern/kmutex.c: New file. * Makefrag.am (libkernel_a_SOURCES): Add atomic.h, kmutex.h, kmutex.c. * kern/sched_prim.h (thread_wakeup_prim): Make it return boolean_t. * kern/sched_prim.c (thread_wakeup_prim): Return TRUE if we woke a thread, and FALSE otherwise. --- kern/kmutex.c | 76 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 76 insertions(+) create mode 100644 kern/kmutex.c (limited to 'kern/kmutex.c') diff --git a/kern/kmutex.c b/kern/kmutex.c new file mode 100644 index 00000000..5926d1d9 --- /dev/null +++ b/kern/kmutex.c @@ -0,0 +1,76 @@ +/* Copyright (C) 2017 Free Software Foundation, Inc. + Contributed by Agustina Arzille , 2017. + + 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 2 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 + +void kmutex_init (struct kmutex *mtxp) +{ + mtxp->state = KMUTEX_AVAIL; + simple_lock_init (&mtxp->lock); +} + +kern_return_t kmutex_lock (struct kmutex *mtxp, boolean_t interruptible) +{ + check_simple_locks (); + + if (atomic_cas_acq (&mtxp->state, KMUTEX_AVAIL, KMUTEX_LOCKED)) + /* Unowned mutex - We're done. */ + return (KERN_SUCCESS); + + /* The mutex is locked. We may have to sleep. */ + simple_lock (&mtxp->lock); + if (atomic_swap_acq (&mtxp->state, KMUTEX_CONTENDED) == KMUTEX_AVAIL) + { + /* The mutex was released in-between. */ + simple_unlock (&mtxp->lock); + return (KERN_SUCCESS); + } + + /* Sleep and check the result value of the waiting, in order to + * inform our caller if we were interrupted or not. Note that + * we don't need to set again the mutex state. The owner will + * handle that in every case. */ + thread_sleep ((event_t)mtxp, (simple_lock_t)&mtxp->lock, interruptible); + return (current_thread()->wait_result == THREAD_AWAKENED ? + KERN_SUCCESS : KERN_INTERRUPTED); +} + +kern_return_t kmutex_trylock (struct kmutex *mtxp) +{ + return (atomic_cas_acq (&mtxp->state, KMUTEX_AVAIL, KMUTEX_LOCKED) ? + KERN_SUCCESS : KERN_FAILURE); +} + +void kmutex_unlock (struct kmutex *mtxp) +{ + if (atomic_cas_rel (&mtxp->state, KMUTEX_LOCKED, KMUTEX_AVAIL)) + /* No waiters - We're done. */ + return; + + simple_lock (&mtxp->lock); + + if (!thread_wakeup_one ((event_t)mtxp)) + /* Any threads that were waiting on this mutex were + * interrupted and left - Reset the mutex state. */ + mtxp->state = KMUTEX_AVAIL; + + simple_unlock (&mtxp->lock); +} -- cgit v1.2.3