/* Demonstrate atomic exchange */ #include /* Code to atomic exchange */ static void exchange(lock, val, out) /* Atomic exchange value->memory */ unsigned int *lock; /* Points to location to swap */ unsigned int val; /* New value to store in swap location */ unsigned int *out; /* Points to where to store old value */ { asm("swap [%i0], %i1"); asm ("st %i1, [%i2]"); } int Lock = 0; int main() { int OldLock; exchange(&Lock, 1, &OldLock); printf("Old Lock value %d\n", OldLock); exchange(&Lock, 1, &OldLock); printf("Old Lock value %d\n", OldLock); return(0); } #ifdef HERES_THE_OUTPUT Old Lock value 0 Old Lock value 1 #endif