fastlock.h
eca7f442
 /*
  * fast arhitecture specific locking
  *
  * $Id$
  *
  * 
  */
 
 
 
 #ifndef fastlock_h
 #define fastlock_h
 
 
 #include <sched.h>
 
 
 
 
2781889b
 typedef  volatile int fl_lock_t;
eca7f442
 
 
 
 #define init_lock( l ) (l)=0
 
 
 
 /*test and set lock, ret 1 if lock held by someone else, 0 otherwise*/
2781889b
 inline static int tsl(fl_lock_t* lock)
eca7f442
 {
43cfbe04
 	volatile int val;
c4217e61
 #ifdef __i386
eca7f442
 	
 	val=1;
 	asm volatile( 
 		" xchg %b0, %1" : "=q" (val), "=m" (*lock) : "0" (val) : "memory"
 	);
43cfbe04
 #elif defined __sparc
c4217e61
 	asm volatile(
 			"ldstub [%1], %0 \n\t"
 			"membar #StoreStore | #StoreLoad \n\t"
 			: "=r"(val) : "r"(lock):"memory"
 	);
43cfbe04
 #else
 #error "unknown arhitecture"
c4217e61
 #endif
43cfbe04
 	return val;
eca7f442
 }
 
 
 
2781889b
 inline static void get_lock(fl_lock_t* lock)
eca7f442
 {
 	
 	while(tsl(lock)){
 		sched_yield();
 	}
 }
 
 
 
2781889b
 inline static void release_lock(fl_lock_t* lock)
eca7f442
 {
c4217e61
 #ifdef __i386
43cfbe04
 	char val;
eca7f442
 	val=0;
 	asm volatile(
da87d067
 		" movb $0, (%0)" : /*no output*/ : "r"(lock): "memory"
 		/*" xchg %b0, %1" : "=q" (val), "=m" (*lock) : "0" (val) : "memory"*/
c4217e61
 	); /* hmm, maybe lock; movb $0, [%1] would be faster ???*/
43cfbe04
 #elif defined __sparc
c4217e61
 	asm volatile(
 			"membar #LoadStore | #StoreStore \n\t" /*is this really needed?*/
 			"stb %%g0, [%0] \n\t"
 			: /*no output*/
 			: "r" (lock)
 			: "memory"
eca7f442
 	);
43cfbe04
 #else
 #error "unknown arhitecture"
c4217e61
 #endif
eca7f442
 }
 
 
 
 #endif