nim-taskpools/taskpools/primitives/barriers.md

2.1 KiB

Synchronization Barriers

OSX does not implement pthread_barrier as its an optional part of the POSIX standard and they probably want to drive people to libdispatch/Grand Central Dispatch.

So we need to roll our own with a POSIX compatible API.

Glibc barriers, design bug and implementation

Note: due to GPL licensing, do not lift the code. Not that we can as it is heavily dependent on futexes which are not available on OSX

We need to make sure that we don't hit the same bug as glibc: https://sourceware.org/bugzilla/show_bug.cgi?id=13065 which seems to be an issue in some of the barrier implementations in the wild.

The design of Glibc barriers is here: https://sourceware.org/git/?p=glibc.git;a=blob;f=nptl/DESIGN-barrier.txt;h=23463c6b7e77231697db3e13933b36ce295365b1;hb=HEAD

And implementation:

Synchronization barrier techniques

This article goes over the techniques of "pool barrier" and "ticket barrier" https://locklessinc.com/articles/barriers/ to reach 2x to 20x the speed of pthreads barrier

This course https://cs.anu.edu.au/courses/comp8320/lectures/aux/comp422-Lecture21-Barriers.pdf goes over

It however requires lightweight mutexes like Linux futexes that OSX lacks.

This post goes over lightweight mutexes like Benaphores (from BeOS) https://preshing.com/20120226/roll-your-own-lightweight-mutex/

This gives a few barrier implementations http://gallium.inria.fr/~maranget/MPRI/02.pdf and refers to Cubible paper for formally verifying synchronization barriers http://cubicle.lri.fr/papers/jfla2014.pdf (in French)