All pastes #730992 Raw Edit

Stuff

public text v1 · immutable
·published 2007-10-09 18:11 UTC
rendered paste body
int cmpxchg(int oldval, int newval, lock_t * l)
{
	int result;
	asm volatile("cmpxchgl %2, %0" :
			"+m" (*l), "=a" (result) :
 			"r"(newval), "1"(oldval) :
 			"cc");
	return result;

}

int test_and_set(lock_t * l, int newval)
{
	return cmpxchg(l->status, newval, &l->status);

}


void lock_acquire(lock_t * l)
{
	int count;

	for(;;) {
		for(count = 1024; count > 0; count--) {
	   		if(test_and_set(l, 1) == 0) {
			// Got it
	       			return;
	     		}
		}
		
		// didn't get it, go to sleep
       		yield();
   	}
}