inline struct timespec current_kernel_time(void) 此函数用于返回当前内核时间。该时间是距离1970开始的秒和纳秒.
static inline struct timespec current_kernel_time(void)
{
#首先得到时间并存在timespec64 类型的now中
struct timespec64 now = current_kernel_time64();
#在通过将timespec64 转成timespec然后返回给用户
return timespec64_to_timespec(now);
}
其中current_kernel_time64 是通过timekeep得到时间
struct timespec64 current_kernel_time64(void)
{
#指向timekeep的指针
struct timekeeper *tk = &tk_core.timekeeper;
struct timespec64 now;
unsigned long seq;
do {
#顺序锁保护,防止在读取时间的同时时间已经跳变
seq = read_seqcount_begin(&tk_core.seq);
#通过timekeep中得到时间
now = tk_xtime(tk);
} while (read_seqcount_retry(&tk_core.seq, seq));
#返回给用户时间,这个时间是保存在timespec64 这个结构体中。
return now;
}
其中timespec64_to_timespec的实现比较简单,简单的成员变量之间赋值
static inline struct timespec timespec64_to_timespec(const struct timespec64 ts64)
{
struct timespec ret;
ret.tv_sec = (time_t)ts64.tv_sec;
ret.tv_nsec = ts64.tv_nsec;
return ret;
}