sleep()
may work up earlier than specified because it can be woken up by a signal.
man page states:
The sleep() function suspends execution of the calling thread until either seconds seconds have elapsed or a signal is delivered to the thread and its action is to invoke a signal-catching function or to terminate the thread or process.
There is no man page for mach_wait_until()
. Can it also be interrupted by a signal? On an old Apple mailing list post someone claimed it won't but I ran into a situation that would only make sense if it does. Is that documented anywhere or does anyone have more insights on this topic?
It looks like it can.
Of course there is no documentation, it's not the Apple way to write a documentation.
But fortunatelly, we can check it with apple opensource xnu kernel: https://opensource.apple.com/source/xnu/xnu-7195.81.3/
I would say we are interesting in the file xnu-7195.50.7.100.1\osfmk\kern\clock.c There is a trap that implements mach_wait_until with such call:
wresult = assert_wait_deadline_with_leeway((event_t)mach_wait_until_trap, THREAD_ABORTSAFE,
TIMEOUT_URGENCY_USER_NORMAL, deadline, 0);
The THREAD_ABORTSAFE here is declared in xnu-7195.50.7.100.1\osfmk\kern\kern_types.h with some useful comments:
* THREAD_ABORTSAFE:
* Wait will end if someone explicitly wakes up the thread, the wait timeout
* expires, the current thread is being terminated, if any signal arrives for
* the task, or thread_abort_safely() is called on the thread.
*
* Using this value means that you are willing to be interrupted in the face
* of any user signal, and safely rewind the thread back to the user/kernel
* boundary. Many syscalls will try to restart the operation they were performing
* after the signal has been handled.
*
* You must provide this value for any unbounded wait - otherwise you will
* pend user signals forever.