Slightly improve semaphore handling.