#include <stdio.h>
#include <assert.h>
-#define __xg(x) ((volatile long *)(x))
+#define __xg(x) ((volatile INTPTR *)(x))
#define CFENCE asm volatile("":::"memory");
return c;
}
+#ifdef BIT64
+static inline INTPTR LOCKXCHG(volatile INTPTR * ptr, INTPTR val){
+ INTPTR retval;
+ //note: xchgl always implies lock
+ __asm__ __volatile__("xchgq %0,%1"
+ : "=r"(retval)
+ : "m"(*ptr), "0"(val)
+ : "memory");
+ return retval;
+
+}
+#else
static inline int LOCKXCHG(volatile int* ptr, int val){
-
int retval;
//note: xchgl always implies lock
__asm__ __volatile__("xchgl %0,%1"
return retval;
}
+#endif
/*
static inline int write_trylock(volatile int *lock) {
}
*/
-static inline int CAS(volatile int* mem, int cmp, int val){
- int prev;
- asm volatile ("lock; cmpxchgl %1, %2"
- : "=a" (prev)
- : "r" (val), "m" (*(mem)), "0"(cmp)
- : "memory", "cc");
- return prev;
-}
-
-static inline long CAS32(volatile void *ptr, unsigned long old, unsigned long new){
- unsigned long prev;
- __asm__ __volatile__("lock; cmpxchgl %k1,%2"
+#ifdef BIT64
+static inline INTPTR CAS(volatile void *ptr, unsigned INTPTR old, unsigned INTPTR new){
+ unsigned INTPTR prev;
+ __asm__ __volatile__("lock; cmpxchgq %1,%2"
: "=a"(prev)
: "r"(new), "m"(*__xg(ptr)), "0"(old)
: "memory");
return prev;
}
-
-static inline long long CAS64(volatile void *ptr, unsigned long long old, unsigned long long new){
- unsigned long long prev;
- __asm__ __volatile__(LOCK_PREFIX "cmpxchgq %1,%2"
+#else
+static inline long CAS(volatile void *ptr, unsigned long old, unsigned long new){
+ unsigned long prev;
+ __asm__ __volatile__("lock; cmpxchgl %k1,%2"
: "=a"(prev)
: "r"(new), "m"(*__xg(ptr)), "0"(old)
: "memory");
return prev;
}
+#endif
static inline int BARRIER(){
CFENCE;
//at this point, have table
Hashtable* table=(Hashtable*)q->tail;
BinItem * val;
- int key=generateKey((unsigned int)r->dynID);
+ int key=generateKey((unsigned int)(unsigned INTPTR)r->dynID);
do {
val=(BinItem*)0x1;
BinElement* bin=table->array[key];
- val=(BinItem*)LOCKXCHG((unsigned int*)&(bin->head), (unsigned int)val);//note...talk to me about optimizations here.
+ val=(BinItem*)LOCKXCHG((unsigned INTPTR*)&(bin->head), (unsigned INTPTR)val);//note...talk to me about optimizations here.
} while(val==(BinItem*)0x1);
//at this point have locked bin
if (val==NULL) {
r->vector=V;
if (BARRIER() && V->item.status==READY) {
void* flag=NULL;
- flag=(void*)LOCKXCHG((unsigned int*)&(V->array[index]), (unsigned int)flag);
+ flag=(void*)LOCKXCHG((unsigned INTPTR*)&(V->array[index]), (unsigned INTPTR)flag);
if (flag!=NULL) {
if (isParent(r)) { //parent's retire immediately
atomic_dec(&V->item.total);
Q->head=(MemoryQueueItem*)S;
}
void* flag=NULL;
- flag=(void*)LOCKXCHG((unsigned int*)&(S->val), (unsigned int)flag);
+ flag=(void*)LOCKXCHG((unsigned INTPTR*)&(S->val), (unsigned INTPTR)flag);
if (flag!=NULL) {
return READY;
} else {
}
RETIREBIN(Hashtable *T, REntry *r, BinItem *b) {
- int key=generateKey((unsigned int)r->dynID);
+ int key=generateKey((unsigned int)(unsigned INTPTR)r->dynID);
if(isFineRead(r)) {
atomic_dec(&b->total);
}
// CHECK FIRST IF next is nonnull to guarantee that b.total cannot change
BinItem * val;
do {
- val=(BinItem*)1;
- val=(BinItem*)LOCKXCHG((unsigned int*)&(T->array[key]->head), (unsigned int)val);
- } while(val==(BinItem*)1);
+ val=(BinItem*)0x1;
+ val=(BinItem*)LOCKXCHG((unsigned INTPTR*)&(T->array[key]->head), (unsigned INTPTR)val);
+ } while(val==(BinItem*)0x1);
// at this point have locked bin
BinItem *ptr=val;
int haveread=FALSE;
-
- int i;
+ int i;
while (ptr!=NULL) {
- if (isReadBinItem(ptr)) {
+ if (isReadBinItem(ptr)) {
ReadBinItem* rptr=(ReadBinItem*)ptr;
if (rptr->item.status==NOTREADY) {
for (i=0;i<rptr->index;i++) {
break;
}
MemoryQueueItem* nextitem=head->next;
- CAS32((unsigned int*)&(Q->head), (unsigned int)head, (unsigned int)nextitem);
+ CAS((unsigned INTPTR*)&(Q->head), (unsigned INTPTR)head, (unsigned INTPTR)nextitem);
//oldvalue not needed... if we fail we just repeat
}
}
BinItem* val;
do {
val=(BinItem*)1;
- val=(BinItem*)LOCKXCHG((unsigned int*)&(bin->head), (unsigned int)val);
+ val=(BinItem*)LOCKXCHG((unsigned INTPTR*)&(bin->head), (unsigned INTPTR)val);
} while (val==(BinItem*)1);
//at this point have locked bin
int haveread=FALSE;
//enqueue everything
for (i=0;i<NUMITEMS;i++) {
REntry* val=NULL;
- val=(REntry*)LOCKXCHG((unsigned int*)&(tmp->array[i]), (unsigned int)val);
+ val=(REntry*)LOCKXCHG((unsigned INTPTR*)&(tmp->array[i]), (unsigned INTPTR)val);
if (val!=NULL) {
resolveDependencies(val);
if (isParent(val)) {
RESOLVESCC(SCC *S) {
//precondition: SCC's state is READY
void* flag=NULL;
- flag=(void*)LOCKXCHG((unsigned int*)&(S->val), (unsigned int)flag);
+ flag=(void*)LOCKXCHG((unsigned INTPTR*)&(S->val), (unsigned INTPTR)flag);
if (flag!=NULL) {
resolveDependencies(flag);
}
psem_give(&(rentry->parentStallSem));
}
}
+