Generalized sleep and timeout functions