diff options
author | neal <neal> | 2008-01-22 21:54:52 +0000 |
---|---|---|
committer | neal <neal> | 2008-01-22 21:54:52 +0000 |
commit | e0e1046a2a54fab8626b7721483899b30fa34929 (patch) | |
tree | bec8b4cc6c12dac470a984a2257813d8128776ad | |
parent | 00fee5105dca3b1545ccc4bc16a8efa6f8d0207b (diff) |
2008-01-22 Neal H. Walfield <neal@gnu.org>
* mutex.h: New file, copy ../hurd/mutex.h.
* ager.c: Include "mutex.h", not <hurd/mutex.h>.
* object.h: Likewise.
* t-environment.h: Likewise.
* viengoos.c: Likewise.
-rw-r--r-- | viengoos/ChangeLog | 9 | ||||
-rw-r--r-- | viengoos/ager.c | 2 | ||||
-rw-r--r-- | viengoos/mutex.h | 116 | ||||
-rw-r--r-- | viengoos/object.h | 2 | ||||
-rw-r--r-- | viengoos/t-environment.h | 4 | ||||
-rw-r--r-- | viengoos/viengoos.c | 2 |
6 files changed, 130 insertions, 5 deletions
diff --git a/viengoos/ChangeLog b/viengoos/ChangeLog index 5e6c2a0..ae6b7a4 100644 --- a/viengoos/ChangeLog +++ b/viengoos/ChangeLog @@ -1,3 +1,12 @@ +2008-01-22 Neal H. Walfield <neal@gnu.org> + + * mutex.h: New file, copy ../hurd/mutex.h. + + * ager.c: Include "mutex.h", not <hurd/mutex.h>. + * object.h: Likewise. + * t-environment.h: Likewise. + * viengoos.c: Likewise. + 2008-01-17 Neal H. Walfield <neal@gnu.org> * object.c (folio_object_alloc): If a thread is waiting for an diff --git a/viengoos/ager.c b/viengoos/ager.c index a2fe2c4..1151ba7 100644 --- a/viengoos/ager.c +++ b/viengoos/ager.c @@ -20,7 +20,7 @@ #include <l4/space.h> #include <l4/schedule.h> -#include <hurd/mutex.h> +#include "mutex.h" #include <assert.h> diff --git a/viengoos/mutex.h b/viengoos/mutex.h new file mode 100644 index 0000000..595658f --- /dev/null +++ b/viengoos/mutex.h @@ -0,0 +1,116 @@ +/* mutex.c - Small, simple LIFO mutex implementation. + Copyright (C) 2007 Free Software Foundation, Inc. + Written by Neal H. Walfield <neal@gnu.org>. + + This file is part of the GNU Hurd. + + GNU Hurd is free software: you can redistribute it and/or modify it + under the terms of the GNU Lesser General Public License as + published by the Free Software Foundation, either version 3 of the + License, or (at your option) any later version. + + GNU Hurd 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 + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with GNU Hurd. If not, see + <http://www.gnu.org/licenses/>. */ + +#ifndef _MUTEX_H +#define _MUTEX_H + +#include <l4/thread.h> +#include <atomic.h> +#include <assert.h> +#include <hurd/lock.h> + +typedef l4_thread_id_t ss_mutex_t; + +static inline void +ss_mutex_lock (__const char *caller, int line, ss_mutex_t *lock) +{ + l4_thread_id_t owner; + + for (;;) + { + owner = atomic_exchange_acq (lock, l4_myself ()); + if (owner == l4_nilthread) + { + ss_mutex_trace_add (SS_MUTEX_LOCK, caller, line, lock); + return; + } + + ss_mutex_trace_add (SS_MUTEX_LOCK_WAIT, caller, line, lock); + + if (owner == l4_myself ()) + ss_lock_trace_dump (lock); + assert (owner != l4_myself ()); + + __ss_lock_wait (owner); + } +} + +#define ss_mutex_lock(__sml_lockp) \ + do \ + { \ + debug (5, "ss_mutex_lock (%p)", __sml_lockp); \ + ss_mutex_lock (__func__, __LINE__, __sml_lockp); \ + } \ + while (0) + +static inline void +ss_mutex_unlock (__const char *caller, int line, ss_mutex_t *lock) +{ + l4_thread_id_t waiter; + + waiter = atomic_exchange_acq (lock, l4_nilthread); + ss_mutex_trace_add (SS_MUTEX_UNLOCK, caller, line, lock); + if (waiter == l4_myself ()) + /* No waiter. */ + return; + + if (waiter == l4_nilthread) + ss_lock_trace_dump (lock); + assert (waiter != l4_nilthread); + + /* Signal the waiter. */ + __ss_lock_wakeup (waiter); +} + +#define ss_mutex_unlock(__smu_lockp) \ + do \ + { \ + debug (5, "ss_mutex_unlock (%p)", __smu_lockp); \ + ss_mutex_unlock (__func__, __LINE__, __smu_lockp); \ + } \ + while (0) + +static inline bool +ss_mutex_trylock (__const char *caller, int line, ss_mutex_t *lock) +{ + l4_thread_id_t owner; + + owner = atomic_compare_and_exchange_val_acq (lock, l4_myself (), + l4_nilthread); + if (owner == l4_nilthread) + { + ss_mutex_trace_add (SS_MUTEX_TRYLOCK, caller, line, lock); + return true; + } + + // ss_mutex_trace_add (SS_MUTEX_TRYLOCK_BLOCKED, caller, line, lock); + + return false; +} + +#define ss_mutex_trylock(__sml_lockp) \ + ({ \ + bool __sml_r = ss_mutex_trylock (__func__, __LINE__, __sml_lockp); \ + debug (5, "ss_mutex_trylock (%p) -> %s", \ + __sml_lockp, __sml_r ? "t" : "f"); \ + __sml_r; \ + }) + +#endif diff --git a/viengoos/object.h b/viengoos/object.h index a5b75a6..377a778 100644 --- a/viengoos/object.h +++ b/viengoos/object.h @@ -27,7 +27,7 @@ #include <assert.h> #include <hurd/cap.h> #include <hurd/folio.h> -#include <hurd/mutex.h> +#include "mutex.h" #include <hurd/btree.h> #include <stdint.h> diff --git a/viengoos/t-environment.h b/viengoos/t-environment.h index f49e374..b496b9a 100644 --- a/viengoos/t-environment.h +++ b/viengoos/t-environment.h @@ -13,7 +13,7 @@ implementation to use. The latter section consists of start up code and convenience code. - Whatever file is the mail file should first define the + Whatever file is the main file should first define the _L4_TEST_MAIN macro and then include this file. The program then need only define a test function. */ @@ -178,7 +178,7 @@ static int opt_keep_going; /* True if a check failed. */ static int failed; -#include <hurd/mutex.h> +#include "mutex.h" struct ss_lock_trace ss_lock_trace[SS_LOCK_TRACE_COUNT]; int ss_lock_trace_count; diff --git a/viengoos/viengoos.c b/viengoos/viengoos.c index e362287..14b2a73 100644 --- a/viengoos/viengoos.c +++ b/viengoos/viengoos.c @@ -22,7 +22,7 @@ #include <config.h> #endif -#include <hurd/mutex.h> +#include "mutex.h" #ifndef NDEBUG struct ss_lock_trace ss_lock_trace[SS_LOCK_TRACE_COUNT]; |