summaryrefslogtreecommitdiff
path: root/kern
diff options
context:
space:
mode:
authorAgustina Arzille <avarzille@riseup.net>2017-02-01 12:32:52 -0300
committerSamuel Thibault <samuel.thibault@ens-lyon.org>2017-03-04 23:11:20 +0100
commit25f22702f153d62b55cb276152ba5ea935023e6e (patch)
treed93d342c3e7f124df94215a855ca4b99d93012fe /kern
parentdc5486ba8801625069fc6172b03e897612f42a90 (diff)
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.
Diffstat (limited to 'kern')
-rw-r--r--kern/atomic.h54
-rw-r--r--kern/kmutex.c76
-rw-r--r--kern/kmutex.h52
-rw-r--r--kern/sched_prim.c5
-rw-r--r--kern/sched_prim.h2
5 files changed, 187 insertions, 2 deletions
diff --git a/kern/atomic.h b/kern/atomic.h
new file mode 100644
index 00000000..00da1645
--- /dev/null
+++ b/kern/atomic.h
@@ -0,0 +1,54 @@
+/* Copyright (C) 2017 Free Software Foundation, Inc.
+ Contributed by Agustina Arzille <avarzille@riseup.net>, 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
+ <http://www.gnu.org/licenses/>.
+*/
+
+#ifndef _KERN_ATOMIC_H_
+#define _KERN_ATOMIC_H_ 1
+
+/* Atomically compare *PTR with EXP and set it to NVAL if they're equal.
+ * Evaluates to a boolean, indicating whether the comparison was successful.*/
+#define __atomic_cas_helper(ptr, exp, nval, mo) \
+ ({ \
+ typeof(exp) __e = (exp); \
+ __atomic_compare_exchange_n ((ptr), &__e, (nval), 0, \
+ __ATOMIC_##mo, __ATOMIC_RELAXED); \
+ })
+
+#define atomic_cas_acq(ptr, exp, nval) \
+ __atomic_cas_helper (ptr, exp, nval, ACQUIRE)
+
+#define atomic_cas_rel(ptr, exp, nval) \
+ __atomic_cas_helper (ptr, exp, nval, RELEASE)
+
+#define atomic_cas_seq(ptr, exp, nval) \
+ __atomic_cas_helper (ptr, exp, nval, SEQ_CST)
+
+/* Atomically exchange the value of *PTR with VAL, evaluating to
+ * its previous value. */
+#define __atomic_swap_helper(ptr, val, mo) \
+ __atomic_exchange_n ((ptr), (val), __ATOMIC_##mo)
+
+#define atomic_swap_acq(ptr, val) \
+ __atomic_swap_helper (ptr, val, ACQUIRE)
+
+#define atomic_swap_rel(ptr, val) \
+ __atomic_swap_helper (ptr, val, RELEASE)
+
+#define atomic_swap_seq(ptr, val) \
+ __atomic_swap_helper (ptr, val, SEQ_CST)
+
+#endif
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 <avarzille@riseup.net>, 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
+ <http://www.gnu.org/licenses/>.
+*/
+
+#include <kern/kmutex.h>
+#include <kern/atomic.h>
+#include <kern/sched_prim.h>
+#include <kern/thread.h>
+
+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);
+}
diff --git a/kern/kmutex.h b/kern/kmutex.h
new file mode 100644
index 00000000..29815156
--- /dev/null
+++ b/kern/kmutex.h
@@ -0,0 +1,52 @@
+/* Copyright (C) 2017 Free Software Foundation, Inc.
+ Contributed by Agustina Arzille <avarzille@riseup.net>, 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
+ <http://www.gnu.org/licenses/>.
+*/
+
+#ifndef _KERN_KMUTEX_H_
+#define _KERN_KMUTEX_H_ 1
+
+#include <kern/lock.h>
+#include <mach/kern_return.h>
+
+struct kmutex
+{
+ unsigned int state;
+ decl_simple_lock_data (, lock)
+};
+
+/* Possible values for the mutex state. */
+#define KMUTEX_AVAIL 0
+#define KMUTEX_LOCKED 1
+#define KMUTEX_CONTENDED 2
+
+/* Initialize mutex in *MTXP. */
+extern void kmutex_init (struct kmutex *mtxp);
+
+/* Acquire lock MTXP. If INTERRUPTIBLE is true, the sleep may be
+ * prematurely terminated, in which case the function returns
+ * KERN_INTERRUPTED. Otherwise, KERN_SUCCESS is returned. */
+extern kern_return_t kmutex_lock (struct kmutex *mtxp,
+ boolean_t interruptible);
+
+/* Try to acquire the lock MTXP without sleeping.
+ * Returns KERN_SUCCESS if successful, KERN_FAILURE otherwise. */
+extern kern_return_t kmutex_trylock (struct kmutex *mtxp);
+
+/* Unlock the mutex MTXP. */
+extern void kmutex_unlock (struct kmutex *mtxp);
+
+#endif
diff --git a/kern/sched_prim.c b/kern/sched_prim.c
index bb767352..173f0687 100644
--- a/kern/sched_prim.c
+++ b/kern/sched_prim.c
@@ -376,13 +376,14 @@ void clear_wait(
* and thread_wakeup_one.
*
*/
-void thread_wakeup_prim(
+boolean_t thread_wakeup_prim(
event_t event,
boolean_t one_thread,
int result)
{
queue_t q;
int index;
+ boolean_t woke = FALSE;
thread_t thread, next_th;
decl_simple_lock_data( , *lock);
spl_t s;
@@ -435,6 +436,7 @@ void thread_wakeup_prim(
break;
}
thread_unlock(thread);
+ woke = TRUE;
if (one_thread)
break;
}
@@ -442,6 +444,7 @@ void thread_wakeup_prim(
}
simple_unlock(lock);
splx(s);
+ return (woke);
}
/*
diff --git a/kern/sched_prim.h b/kern/sched_prim.h
index dfb2f54b..405e5456 100644
--- a/kern/sched_prim.h
+++ b/kern/sched_prim.h
@@ -72,7 +72,7 @@ extern void thread_sleep(
simple_lock_t lock,
boolean_t interruptible);
extern void thread_wakeup(void); /* for function pointers */
-extern void thread_wakeup_prim(
+extern boolean_t thread_wakeup_prim(
event_t event,
boolean_t one_thread,
int result);