Fix race condition in sleep code

Signed-off-by: Paul Brook <paul@nowt.org>
Add example code

Signed-off-by: Paul Brook <paul@nowt.org>