diff options
-rw-r--r-- | kern/spinlock.h | 21 | ||||
-rw-r--r-- | kern/spinlock_i.h | 26 |
2 files changed, 29 insertions, 18 deletions
diff --git a/kern/spinlock.h b/kern/spinlock.h index a9e82bc7..b165e759 100644 --- a/kern/spinlock.h +++ b/kern/spinlock.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2012-2014 Richard Braun. + * Copyright (c) 2012-2017 Richard Braun. * * 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 @@ -40,22 +40,19 @@ spinlock_init(struct spinlock *lock) #define spinlock_assert_locked(lock) assert((lock)->locked) -/* - * Return 0 on success, 1 if busy. - */ static inline int spinlock_trylock(struct spinlock *lock) { - int busy; + int error; thread_preempt_disable(); - busy = spinlock_tryacquire(lock); + error = spinlock_tryacquire(lock); - if (busy) { + if (error) { thread_preempt_enable(); } - return busy; + return error; } static inline void @@ -80,18 +77,18 @@ spinlock_unlock(struct spinlock *lock) static inline int spinlock_trylock_intr_save(struct spinlock *lock, unsigned long *flags) { - int busy; + int error; thread_preempt_disable(); cpu_intr_save(flags); - busy = spinlock_tryacquire(lock); + error = spinlock_tryacquire(lock); - if (busy) { + if (error) { cpu_intr_restore(*flags); thread_preempt_enable(); } - return busy; + return error; } static inline void diff --git a/kern/spinlock_i.h b/kern/spinlock_i.h index 6f7f4a6c..88cb89e4 100644 --- a/kern/spinlock_i.h +++ b/kern/spinlock_i.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2012-2014 Richard Braun. + * Copyright (c) 2012-2017 Richard Braun. * * 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 @@ -19,23 +19,37 @@ #define _KERN_SPINLOCK_I_H #include <kern/assert.h> +#include <kern/error.h> #include <kern/spinlock_types.h> #include <machine/atomic.h> #include <machine/cpu.h> -/* - * Return 0 on success, 1 if busy. - */ static inline int spinlock_tryacquire(struct spinlock *lock) { - return atomic_swap_uint(&lock->locked, 1); + unsigned int state; + + state = atomic_swap_uint(&lock->locked, 1); + + if (state == 0) { + return 0; + } + + return ERROR_BUSY; } static inline void spinlock_acquire(struct spinlock *lock) { - while (spinlock_tryacquire(lock)) { + int error; + + for (;;) { + error = spinlock_tryacquire(lock); + + if (!error) { + break; + } + cpu_pause(); } } |