#include <sys/atomic_op.h>
/*-------------------------------------------------------------------*/
/* shared/exclusive access using fetch-and-add */
/*-------------------------------------------------------------------*/
#define EXCL 1
#define SHRD 0x10000
void init_1(n, c)
int *n; /* next available "ticket" */
int *c; /* current "ticket" */
{
*n=0; *c=0;
}
void acquire_exclusive_1(int *n, int *c) {
int w;
w=fetch_and_add(n, EXCL); /* request exclusive access */
while(w!=*(volatile int *)c); /* wait for it */
}
void release_exclusive_1(int *c) {
fetch_and_add(c, EXCL); /* release exclusive access */
}
void acquire_shared_1(int *n, int *c) {
int w;
w=fetch_and_add(n, SHRD); /* request shared access */
while((w%SHRD)!=((*(volatile int *)c)%SHRD)); /* wait for it */
}
void release_shared_1(int *c) {
fetch_and_add(c, SHRD); /* release shared access */
}
/*-------------------------------------------------------------------*/
/* shared/exclusive access using fetch-and-increment */
/*-------------------------------------------------------------------*/
void init_2(n, x, s)
int *n; /* next available "ticket" */
int *x; /* current shared "ticket" */
int *s; /* current exclusive "ticket" */
{
*n=0; *x=0; *s=0; /* initialize */
}
void acquire_exclusive_2(int *n, int *x) {
int w;
w=fetch_and_add(n, 1); /* request (exclusive) access */
while(w!=*(volatile int *)x); /* wait for it */
}
void release_exclusive_2(int *x, int *s) {
fetch_and_add(s, 1); /* unblock next shared */
fetch_and_add(x, 1); /* unblock next exclusive */
}
void acquire_shared_2(int *n, int *s) {
int w;
w=fetch_and_add(n, 1); /* request (shared) access */
while(w!=*(volatile int *)s); /* wait for it */
fetch_and_add(s, 1); /* unblock next shared */
}
void release_shared_2(int *x) {
fetch_and_add(x, 1); /* unblock next exclusive */
}
--
Joe Seigh c1mo...@watson.ibm.com