replace sleep with two condition variables

Change-Id: I7611b5be7d462646a19ebfd86b8d6612dccafc71