Inline Assembly: Difference between revisions

[unchecked revision][unchecked revision]
Line 183:
 
<source lang="c">
// Works for both 32 and 64 bit
#define cmpxchg( ptr, _old, _new, fail_label ) { \
volatile u32 *__ptr = (volatile u32 *)(ptr); \
u32asm __retgoto( "lock; cmpxchg %1,%0 \t\n" \
asm volatile goto("jnz %l["lock; cmpxchgl#fail_label %2,%1"] \t\n" \
"jnz: /* empty */ %l[fail_label] \t\n" \
: "=am" (__ret*__ptr), "+mr" (*__ptr_new), "a" (_old) \
: "rmemory" (_new), "0cc" (_old) \
: "memory" fail_label ); \
: fail_label ); \
); \
__ret; \
}
</source>
Line 199 ⟶ 197:
This new macro could then be used as follows:
<source lang="c">
typedef unsigned int u32;
 
volatilestruct Item *head;{
volatile struct Item* next;
};
 
voidvolatile addItem(struct Item *i ) { head;
 
void addItem( struct Item *i ) {
volatile struct Item *oldHead;
again:
Item *oldHead = head;
i->next = oldHead;
cmpxchg( &tailhead, oldHead, i, again );
}
 
Anonymous user