diff options
author | Richard Braun <rbraun@sceen.net> | 2013-04-14 18:29:12 +0200 |
---|---|---|
committer | Richard Braun <rbraun@sceen.net> | 2013-04-14 18:29:12 +0200 |
commit | 04e1818f60ad8f90ea502f2f6c6ea61e6e61644c (patch) | |
tree | 51979cf0fdea679b868b044e386e92473295bbc2 /kern/mutex.h | |
parent | 909c423347085774a3fc7f8021ce765465cc92c8 (diff) |
kern/{condition,mutex}: refactor common code
The condition module intrusively uses mutexes. Augment the interface of
the mutex module so that mutexes and conditions share common code. As a
side effect, the implementation should have gained in clarity.
Diffstat (limited to 'kern/mutex.h')
-rw-r--r-- | kern/mutex.h | 68 |
1 files changed, 49 insertions, 19 deletions
diff --git a/kern/mutex.h b/kern/mutex.h index dcbf98eb..08d3d304 100644 --- a/kern/mutex.h +++ b/kern/mutex.h @@ -25,38 +25,68 @@ #include <kern/assert.h> #include <kern/list.h> +#include <kern/mutex_i.h> #include <kern/spinlock.h> -#include <kern/thread.h> -#define MUTEX_UNLOCKED 0 -#define MUTEX_LOCKED 1 -#define MUTEX_CONTENDED 2 - -struct mutex_waiter { - struct list node; - struct thread *thread; -}; - -struct mutex { - unsigned long state; - struct spinlock lock; - struct list waiters; -}; +struct mutex; #define MUTEX_INITIALIZER(mutex) \ { MUTEX_UNLOCKED, SPINLOCK_INITIALIZER, LIST_INITIALIZER((mutex).waiters) } -void mutex_init(struct mutex *mutex); +static inline void +mutex_init(struct mutex *mutex) +{ + mutex->state = MUTEX_UNLOCKED; + spinlock_init(&mutex->lock); + list_init(&mutex->waiters); +} #define mutex_assert_locked(mutex) assert((mutex)->state != MUTEX_UNLOCKED) /* * Return 0 on success, 1 if busy. */ -int mutex_trylock(struct mutex *mutex); +static inline int +mutex_trylock(struct mutex *mutex) +{ + unsigned long state; + + state = mutex_tryacquire(mutex); + + if (state == MUTEX_UNLOCKED) + return 0; + + return 1; +} + +static inline void +mutex_lock(struct mutex *mutex) +{ + unsigned long state; + + state = mutex_tryacquire(mutex); + + if (state == MUTEX_UNLOCKED) + return; + + assert((state == MUTEX_LOCKED) || (state == MUTEX_CONTENDED)); + + mutex_lock_slow(mutex); +} + +static inline void +mutex_unlock(struct mutex *mutex) +{ + unsigned long state; + + state = mutex_release(mutex); + + if (state == MUTEX_LOCKED) + return; -void mutex_lock(struct mutex *mutex); + assert(state == MUTEX_CONTENDED); -void mutex_unlock(struct mutex *mutex); + mutex_unlock_slow(mutex); +} #endif /* _KERN_MUTEX_H */ |