虽然 iOS 内核是闭源的,但可以合理地假设它的大部分与 OSX 内核相同,即开源 https://github.com/opensource-apple/xnu.
Within osfmk/kern/clock.c
有这样的功能:
/*
* clock_get_boottime_nanotime:
*
* Return the boottime, used by sysctl.
*/
void
clock_get_boottime_nanotime(
clock_sec_t *secs,
clock_nsec_t *nanosecs)
{
spl_t s;
s = splclock();
clock_lock();
*secs = (clock_sec_t)clock_boottime;
*nanosecs = 0;
clock_unlock();
splx(s);
}
and clock_boottime
声明为:
static uint64_t clock_boottime; /* Seconds boottime epoch */
最后,对该函数的注释表明它确实可以更改:
/*
* clock_set_calendar_microtime:
*
* Sets the current calendar value by
* recalculating the epoch and offset
* from the system clock.
*
* Also adjusts the boottime to keep the
* value consistent, writes the new
* calendar value to the platform clock,
* and sends calendar change notifications.
*/
void
clock_set_calendar_microtime(
clock_sec_t secs,
clock_usec_t microsecs)
{
...
更新以回答 OP 的查询
我不确定多久一次clock_set_calendar_microtime()
被称为,因为我不熟悉内核的内部工作原理;然而它调整了clock_boottime
值和clock_bootime
值被初始化为clock_initialize_calendar()
,所以我想说它可以被多次调用。我无法使用以下方法找到对它的任何调用:
$ find . -type f -exec grep -l clock_set_calendar_microtime {} \;