I have implemented barrier in Linux, with simple
approach.
Struct barrier has num threads, count thread entered
in barrier and thread mutex to protect barrier operation.
When thread enters it first acquires mutex then
decreases count . If count is zero it resets counter ,goes to
wake all threads that wait, then releases mutex.
In other case releases mutex and goes to wait.
This works ok except that if several threads
enter barrier, it can happen that waking thread
wakes before some thread(s) enter syscall.
Then, of course, program hangs.
I have solved this with nanosleep syscall,
so that waking thread sleeps for one microsecond
before issuing futex wake syscall.
Is there simpler solution without sleeping?
Eg futex does not waits if edx variable does not
match variable that rdi points to.
But this I haven't tried as don't know
when to reset variable.
So for now I use sleeping method.
Thanks
struc barrier num{
.count dd num
.num dd num
.mutex dd 0