diff options
author | Andrew Waterman <andrew@sifive.com> | 2016-11-04 13:50:29 -0700 |
---|---|---|
committer | Andrew Waterman <andrew@sifive.com> | 2016-11-04 13:50:29 -0700 |
commit | d5e3e0d86a4ff7034c3dfab172747e8d74063a42 (patch) | |
tree | e53788be24c917578d9c5617f9d9675b7bb70655 | |
parent | c6b55ce3a8e45c8747945ab3bcd85580f0d344dd (diff) | |
download | pk-d5e3e0d86a4ff7034c3dfab172747e8d74063a42.zip pk-d5e3e0d86a4ff7034c3dfab172747e8d74063a42.tar.gz pk-d5e3e0d86a4ff7034c3dfab172747e8d74063a42.tar.bz2 |
Add spinlock_trylock routine; use it to implement spinlock_lock
-rw-r--r-- | machine/atomic.h | 10 |
1 files changed, 8 insertions, 2 deletions
diff --git a/machine/atomic.h b/machine/atomic.h index b23acae..fe81566 100644 --- a/machine/atomic.h +++ b/machine/atomic.h @@ -40,14 +40,20 @@ typedef struct { int lock; } spinlock_t; res; }) #endif +static inline int spinlock_trylock(spinlock_t* lock) +{ + int res = atomic_swap(&lock->lock, -1); + mb(); + return res; +} + static inline void spinlock_lock(spinlock_t* lock) { do { while (atomic_read(&lock->lock)) ; - } while (atomic_swap(&lock->lock, -1)); - mb(); + } while (spinlock_trylock(lock)); } static inline void spinlock_unlock(spinlock_t* lock) |