summaryrefslogtreecommitdiff
path: root/spinlock.c
diff options
context:
space:
mode:
Diffstat (limited to 'spinlock.c')
-rw-r--r--spinlock.c21
1 files changed, 15 insertions, 6 deletions
diff --git a/spinlock.c b/spinlock.c
index 2d64044..8d40258 100644
--- a/spinlock.c
+++ b/spinlock.c
@@ -4,31 +4,39 @@
#include "mmu.h"
#define LOCK_FREE -1
+#define DEBUG 0
uint32_t kernel_lock = LOCK_FREE;
+int getcallerpc(void *v) {
+ return ((int*)v)[-1];
+}
+
// lock = LOCK_FREE if free, else = cpu_id of owner CPU
-void
+void
acquire_spinlock(uint32_t* lock)
{
int cpu_id = cpu();
+ // on a real machine there would be a memory barrier here
+ if(DEBUG) cprintf("cpu%d: acquiring at %x\n", cpu_id, getcallerpc(&lock));
+ write_eflags(read_eflags() & ~FL_IF);
if (*lock == cpu_id)
- return;
+ panic("recursive lock");
- write_eflags(read_eflags() & ~FL_IF);
while ( cmpxchg(LOCK_FREE, cpu_id, lock) != cpu_id ) { ; }
- // cprintf ("acquired: %d\n", cpu_id);
+ if(DEBUG) cprintf("cpu%d: acquired at %x\n", cpu_id, getcallerpc(&lock));
}
void
release_spinlock(uint32_t* lock)
{
int cpu_id = cpu();
- // cprintf ("release: %d\n", cpu_id);
+ if(DEBUG) cprintf ("cpu%d: releasing at %x\n", cpu_id, getcallerpc(&lock));
if (*lock != cpu_id)
panic("release_spinlock: releasing a lock that i don't own\n");
*lock = LOCK_FREE;
+ // on a real machine there would be a memory barrier here
write_eflags(read_eflags() | FL_IF);
}
@@ -36,8 +44,9 @@ void
release_grant_spinlock(uint32_t* lock, int c)
{
int cpu_id = cpu();
- // cprintf ("release_grant: %d -> %d\n", cpu_id, c);
+ if(DEBUG) cprintf ("cpu%d: release_grant to %d at %x\n", cpu_id, c, getcallerpc(&lock));
if (*lock != cpu_id)
panic("release_spinlock: releasing a lock that i don't own\n");
*lock = c;
}
+