You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Did you know that pthread_mutex_init can _fail_?
On macOS, the value of PTHREAD_MUTEX_INITIALIZER is not just zeroes -
there's optional signature bits in there. POSIX says that you're allowed
to check for those signature bits in the API and issue EINVAL if they
don't match. By default, pthread_mutex_t() in swift will zero-fill
through those signature bits, which results in an invalid mutex
variable - even WRT pthread_mutex_init. Once that fails, all
subsequent calls to lock and unlock will fail too and leave all of
your critical sections completely unguarded.
Thank you POSIX
On Linuxes this is (often) not the case, and they tend to just use zeroes
here, don't check the signature, or both. This allows compilers to allocate
lock variables in .bss, which is kinda neat.
So, on macOS, we need to install those signature bits, BUT Swift cannot
import PTHREAD_MUTEX_INITIALIZER since it's a non-trivial macro that uses
brace initialization. Really what we care about is just the signature bits,
so we'll install those by hand.
0 commit comments