Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

two readers/writers algorithms using fetch-and-op

6 views
Skip to first unread message

se...@news.delphi.com

unread,
Aug 11, 1993, 8:52:49 PM8/11/93
to
fyi (to put into the public domain) here are so fetch-and-op bakery
style algorithms for shared/exclusive access. They work even in the
case of carry out or overflow of the various fields as long as the
number of processors is less than the max field value.

#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

0 new messages