semaphore and other sync refactoring