summaryrefslogtreecommitdiff
path: root/kern
diff options
context:
space:
mode:
authorRichard Braun <rbraun@sceen.net>2017-02-20 20:51:51 +0100
committerRichard Braun <rbraun@sceen.net>2017-02-20 20:51:51 +0100
commit5604e40e64ef079f9815077b1d98621acf212869 (patch)
tree5b6608f2dcb5062359f5397ef9960a7756b8ee7c /kern
parent257df0fbec0849e8eb3c653346c755344babd6c2 (diff)
kern/spinlock: make spinlock_trylock return ERROR_BUSY instead of 1
Diffstat (limited to 'kern')
-rw-r--r--kern/spinlock.h21
-rw-r--r--kern/spinlock_i.h26
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();
}
}