Generic semaphore implementation