rewrite mutex and semaphore