#define EXCL 1
#define SHRD 0x100
volatile int n; /* next available ticket */
volatile int c; /* current request */
volatile int x; /* exclusive */
volatile int s; /* shared */
int w; /* wait ticket, local */
/* shared/exclusive access using fetch-and-add, fa() */
n=0; c=0; /* initialize */
w=fa(&n, EXCL); /* request exclusive access */
while(w!=c); /* wait for it */
/* critical region */
fa(&c, EXCL); /* release exclusive access */
w=fa(&n, SHRD); /* request shared access */
while((w%SHRD)!=(c%SHRD)); /* wait for it */
/* critical region */
fa(&c, SHRD); /* release shared access */
/* shared/exclusive access using fetch-and-increment, fi() */
n=0; x=0; s=0; /* initialize */
w=fi(&n); /* request (exclusive) access */
while(w!=x); /* wait for it */
/* critical region */
fi(&s); /* unblock subsequent shared requests */
fi(&x); /* unblock subsequent exclusive requests */
w=fi(&n); /* request (shared) access */
while(w!=s); /* wait for it */
fi(&s); /* unblock subsequent shared requests */
/* critical region */
fi(&x); /* unblock subsequent exclusive requests */
Joe Seigh