mach_wait_until() 会被信号打断吗?

Can mach_wait_until() get interrupted by a signal?

sleep() 可能会比指定时间更早工作,因为它可以被信号唤醒。

手册页指出:

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.

mach_wait_until() 没有手册页。它也可以被信号打断吗?在一个旧的 Apple 邮件列表 post 上,有人声称它不会,但我 运行 进入了一种只有在它存在时才有意义的情况。是否在任何地方记录或是否有人对此主题有更多见解?

看来可以。

当然没有文档,这不是Apple写文档的方式。

还好我们可以用apple opensource xnu kernel来查看: https://opensource.apple.com/source/xnu/xnu-7195.81.3/

我想说我们对文件 xnu-7195.50.7.100.1\osfmk\kern\clock.c 很感兴趣 有一个通过这样的调用实现 mach_wait_until 的陷阱:

wresult = assert_wait_deadline_with_leeway((event_t)mach_wait_until_trap, THREAD_ABORTSAFE,
    TIMEOUT_URGENCY_USER_NORMAL, deadline, 0);

这里的THREAD_ABORTSAFE在xnu-7195.50.7.100.1\osfmk\kern\kern_types.h中声明了一些有用的注释:

* 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.