typedef struct{
volatile int flags[80];
volatile int queueLast;
volatile int owner;
}my_spinlock_t;
int my_spin_init(my_spinlock_t * s, void *);
void my_spin_lock(my_spinlock_t * s);
void my_spin_unlock(my_spinlock_t * s);
#define HAS_LOCK 0
#define MUST_WAIT 1
int g_atomic_int_exchange_and_add (int *atomic, int val)
{
int result;
__asm__ __volatile__ ("lock; xaddl %0,%1"
: "=r" (result), "=m" (*atomic)
: "0" (val), "m" (*atomic));
return result;
}
int my_gettid()
{
int * p = (int*)pthread_self();
return p[18];
}
int my_spin_init(my_spinlock_t * s, void *p)
{
int i;
s->flags[0] = HAS_LOCK;
for(i=1;i<8;i++)
s->flags[i*10] = MUST_WAIT;
s->queueLast = 0;
return 0;
}
void my_spin_lock(my_spinlock_t * s)
{
int myPlace, t;
volatile int * p;
myPlace = g_atomic_int_exchange_and_add(&s->queueLast, 1);
//myPlace = my_gettid();
t = myPlace % 8;
p = &s->flags[t*10];
while(*p == MUST_WAIT);
s->flags[t*10] = MUST_WAIT;
s->owner = myPlace;
}
void my_spin_unlock(my_spinlock_t * s)
{
int myPlace = s->owner;
s->flags[((myPlace+1) % 8)*10] = HAS_LOCK;
}
Reference
Thomas E. Anderson, "The Performance of Spin Lock Alternatives for Shared-Memory Multiprocessors", IEEE Transactions on Parallel and Distributed Systems, 1990
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment