Use dispatch_semaphore on __APPLE__ platforms

Contribution from István Csanády