struct bar_type {
int counter;
struct lock_type lock;
int flag = 0;
} bar_name;
BARINIT (bar_name) {
LOCKINIT(bar_name.lock);
bar_name.counter = 0;
}
BARRIER (bar_name, P) {
int my_count;
LOCK (bar_name.lock);
if (!bar_name.counter) {
bar_name.flag = 0; /* first one */
}
my_count = ++bar_name.counter;
UNLOCK (bar_name.lock);
if (my_count == P) {
bar_name.counter = 0;
bar_name.flag = 1; /* last one */
}
else {
while (!bar_name.flag);
}
}