summaryrefslogtreecommitdiff
path: root/kern/mutex.h
diff options
context:
space:
mode:
authorRichard Braun <rbraun@sceen.net>2017-07-21 00:54:31 +0200
committerRichard Braun <rbraun@sceen.net>2017-07-21 00:54:54 +0200
commit4c0dcbeb7d363918c9a4a75faf3f1912f580f9c1 (patch)
tree6fdb4a1b80d4aa51bbafda6f930f9f222adc4368 /kern/mutex.h
parentadcae3076edee5ed24cb06f4328f88cfa5e8998a (diff)
parent5c2cf8fff7a1d6dc6b88615df5433ddccbbcf51f (diff)
Merge branch 'adaptive_spinning'
Diffstat (limited to 'kern/mutex.h')
-rw-r--r--kern/mutex.h105
1 files changed, 20 insertions, 85 deletions
diff --git a/kern/mutex.h b/kern/mutex.h
index 103af02a..f192a70a 100644
--- a/kern/mutex.h
+++ b/kern/mutex.h
@@ -18,77 +18,38 @@
* Mutual exclusion sleep locks.
*
* Unlike spin locks, acquiring a mutex may make the calling thread sleep.
- *
- * TODO Adaptive spinning.
*/
#ifndef _KERN_MUTEX_H
#define _KERN_MUTEX_H
-#include <kern/init.h>
-#include <kern/mutex_types.h>
-
-#ifdef X15_MUTEX_PI
-
-#include <kern/rtmutex.h>
-
-struct mutex;
-
-#define mutex_assert_locked(mutex) rtmutex_assert_locked(&(mutex)->rtmutex)
-
-static inline void
-mutex_init(struct mutex *mutex)
-{
- rtmutex_init(&mutex->rtmutex);
-}
-
-static inline int
-mutex_trylock(struct mutex *mutex)
-{
- return rtmutex_trylock(&mutex->rtmutex);
-}
-
-static inline void
-mutex_lock(struct mutex *mutex)
-{
- rtmutex_lock(&mutex->rtmutex);
-}
-
-static inline void
-mutex_unlock(struct mutex *mutex)
-{
- rtmutex_unlock(&mutex->rtmutex);
-
- /*
- * If this mutex was used along with a condition variable, wake up
- * a potential pending waiter. This must be done after the mutex is
- * unlocked so that a higher priority thread can directly acquire it.
- */
- thread_wakeup_last_cond();
-}
+#if defined(X15_MUTEX_PI) && defined(X15_MUTEX_ADAPTIVE)
+#error "only one of X15_MUTEX_PI and X15_MUTEX_ADAPTIVE may be defined"
+#endif
-#else /* X15_MUTEX_PI */
+#if defined(X15_MUTEX_PI)
+#include <kern/mutex/mutex_pi_i.h>
+#elif defined(X15_MUTEX_ADAPTIVE)
+#include <kern/mutex/mutex_adaptive_i.h>
+#else
+#include <kern/mutex/mutex_plain_i.h>
+#endif
-#include <assert.h>
-
-#include <kern/error.h>
-#include <kern/macros.h>
-#include <kern/mutex_i.h>
+#include <kern/init.h>
+#include <kern/mutex_types.h>
#include <kern/thread.h>
-struct mutex;
-
-#define mutex_assert_locked(mutex) assert((mutex)->state != MUTEX_UNLOCKED)
-
/*
* Initialize a mutex.
*/
static inline void
mutex_init(struct mutex *mutex)
{
- mutex->state = MUTEX_UNLOCKED;
+ mutex_impl_init(mutex);
}
+#define mutex_assert_locked(mutex) mutex_impl_assert_locked(mutex)
+
/*
* Attempt to lock the given mutex.
*
@@ -99,37 +60,20 @@ mutex_init(struct mutex *mutex)
static inline int
mutex_trylock(struct mutex *mutex)
{
- unsigned int state;
-
- state = mutex_lock_fast(mutex);
-
- if (unlikely(state != MUTEX_UNLOCKED)) {
- assert((state == MUTEX_LOCKED) || (state == MUTEX_CONTENDED));
- return ERROR_BUSY;
- }
-
- return 0;
+ return mutex_impl_trylock(mutex);
}
/*
* Lock a mutex.
*
- * If the mutex is already locked, the calling thread sleeps until the
- * mutex is unlocked.
+ * On return, the mutex is locked. A mutex can only be locked once.
*
- * A mutex can only be locked once.
+ * This function may sleep.
*/
static inline void
mutex_lock(struct mutex *mutex)
{
- unsigned int state;
-
- state = mutex_lock_fast(mutex);
-
- if (unlikely(state != MUTEX_UNLOCKED)) {
- assert((state == MUTEX_LOCKED) || (state == MUTEX_CONTENDED));
- mutex_lock_slow(mutex);
- }
+ mutex_impl_lock(mutex);
}
/*
@@ -141,14 +85,7 @@ mutex_lock(struct mutex *mutex)
static inline void
mutex_unlock(struct mutex *mutex)
{
- unsigned int state;
-
- state = mutex_unlock_fast(mutex);
-
- if (unlikely(state != MUTEX_LOCKED)) {
- assert(state == MUTEX_CONTENDED);
- mutex_unlock_slow(mutex);
- }
+ mutex_impl_unlock(mutex);
/*
* If this mutex was used along with a condition variable, wake up
@@ -157,8 +94,6 @@ mutex_unlock(struct mutex *mutex)
thread_wakeup_last_cond();
}
-#endif /* X15_MUTEX_PI */
-
/*
* This init operation provides :
* - uncontended mutex locking