Skip to content
Snippets Groups Projects
spinlock.c 867 B
Newer Older
#include "types.h"
#include "defs.h"
#include "x86.h"
#include "mmu.h"
rtm's avatar
rtm committed
#include "param.h"
#include "proc.h"
#include "spinlock.h"
rsc's avatar
 
rsc committed
// Can't call cprintf from inside these routines,
// because cprintf uses them itself.
#define cprintf dont_use_cprintf
rtm's avatar
rtm committed
extern int use_console_lock;
rsc's avatar
 
rsc committed
int
getcallerpc(void *v)
{
	return ((int*)v)[-1];
rtm's avatar
rtm committed
acquire1(struct spinlock * lock, struct proc *cp)
rsc's avatar
 
rsc committed
	if(cpus[cpu()].nlock++ == 0)
		cli();
	while(cmpxchg(0, 1, &lock->locked) == 1)
		;
	cpuid(0, 0, 0, 0, 0);	// memory barrier
	lock->locker_pc = getcallerpc(&lock);
rtm's avatar
rtm committed
release1(struct spinlock * lock, struct proc *cp)
rsc's avatar
 
rsc committed
	cpuid(0, 0, 0, 0, 0);	// memory barrier
	lock->locked = 0;
	if(--cpus[cpu()].nlock == 0)
		sti();
rtm's avatar
rtm committed
}

void
acquire(struct spinlock *lock)
{
rsc's avatar
 
rsc committed
	acquire1(lock, curproc[cpu()]);
rtm's avatar
rtm committed
}

void
release(struct spinlock *lock)
{
rsc's avatar
 
rsc committed
	release1(lock, curproc[cpu()]);
rtm's avatar
rtm committed
}
rsc's avatar
 
rsc committed