Implement a semaphore primitive