bug-hurd
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[PATCH 1/2] Implement basic sleeping locks for gnumach.


From: Agustina Arzille
Subject: [PATCH 1/2] Implement basic sleeping locks for gnumach.
Date: Thu, 19 Jan 2017 10:00:32 -0500

---
 Makefrag.am       |  3 +++
 kern/atomic.h     | 40 ++++++++++++++++++++++++++++
 kern/kmutex.c     | 79 +++++++++++++++++++++++++++++++++++++++++++++++++++++++
 kern/kmutex.h     | 50 +++++++++++++++++++++++++++++++++++
 kern/sched_prim.c |  5 +++-
 kern/sched_prim.h |  2 +-
 6 files changed, 177 insertions(+), 2 deletions(-)
 create mode 100644 kern/atomic.h
 create mode 100644 kern/kmutex.c
 create mode 100644 kern/kmutex.h

diff --git a/Makefrag.am b/Makefrag.am
index c16f1c72..4625b487 100644
--- a/Makefrag.am
+++ b/Makefrag.am
@@ -132,6 +132,7 @@ libkernel_a_SOURCES += \
        kern/assert.h \
        kern/ast.c \
        kern/ast.h \
+       kern/atomic.h \
        kern/boot_script.h \
        kern/bootstrap.c \
        kern/bootstrap.h \
@@ -160,6 +161,8 @@ libkernel_a_SOURCES += \
        kern/ipc_tt.h \
        kern/kalloc.h \
        kern/kern_types.h \
+       kern/kmutex.c \
+       kern/kmutex.h \
        kern/list.h \
        kern/lock.c \
        kern/lock.h \
diff --git a/kern/atomic.h b/kern/atomic.h
new file mode 100644
index 00000000..12427e89
--- /dev/null
+++ b/kern/atomic.h
@@ -0,0 +1,40 @@
+/* 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(ptr, exp, nval)   \
+  ({   \
+     typeof(exp) __e = (exp);   \
+     __atomic_compare_exchange_n ((ptr), &__e, (nval), 0,   \
+       __ATOMIC_SEQ_CST, __ATOMIC_RELAXED);   \
+   })
+
+/* Atomically exchange the value of *PTR with VAL, evaluating to
+ * its previous value. */
+#define atomic_swap(ptr, val)   \
+  __atomic_exchange_n ((ptr), (val), __ATOMIC_SEQ_CST)
+
+/* Atomically store VAL in *PTR. */
+#define atomic_store(ptr, val)   \
+  __atomic_store_n ((ptr), (val), __ATOMIC_SEQ_CST)
+
+#endif
diff --git a/kern/kmutex.c b/kern/kmutex.c
new file mode 100644
index 00000000..9635a6ef
--- /dev/null
+++ b/kern/kmutex.c
@@ -0,0 +1,79 @@
+/* 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);
+}
+
+#define locked_swap(ptr, val)   \
+  ({   \
+     typeof (*ptr) __tmp = *(ptr);   \
+     *(ptr) = (val);   \
+     __tmp;   \
+   })
+
+int kmutex_lock (struct kmutex *mtxp, int interruptible)
+{
+  if (atomic_cas (&mtxp->state, KMUTEX_AVAIL, KMUTEX_LOCKED))
+    /* Unowned mutex - We're done. */
+    return (0);
+
+  /* The mutex is locked. We may have to sleep. */
+  simple_lock (&mtxp->lock);
+  if (locked_swap (&mtxp->state, KMUTEX_CONTENDED) == KMUTEX_AVAIL)
+    {
+      /* The mutex was released in-between. */
+      simple_unlock (&mtxp->lock);
+      return (0);
+    }
+
+  /* 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 ? 0 : -1);
+}
+
+int kmutex_trylock (struct kmutex *mtxp)
+{
+  return (atomic_cas (&mtxp->state, KMUTEX_AVAIL, KMUTEX_LOCKED) ? 0 : -1);
+}
+
+void kmutex_unlock (struct kmutex *mtxp)
+{
+  if (atomic_cas (&mtxp->state, KMUTEX_LOCKED, KMUTEX_AVAIL))
+    /* No waiters - We're done. */
+    return;
+
+  simple_lock (&mtxp->lock);
+
+  if (!thread_wakeup_one ((event_t)mtxp))
+    /* The waiter was 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..b774492d
--- /dev/null
+++ b/kern/kmutex.h
@@ -0,0 +1,50 @@
+/* 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>
+
+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 -1. Otherwise,
+ * it returns zero, with the mutex marked as owned. */
+extern int kmutex_lock (struct kmutex *mtxp, boolean_t interruptible);
+
+/* Try to acquire the lock MTXP without sleeping.
+ * Returns 0 if successful, -1 otherwise. */
+extern int 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);
-- 
2.11.0




reply via email to

[Prev in Thread] Current Thread [Next in Thread]