add spec for Semaphore and fix some bugs