您的位置:首页 > 其它

内核定时机制API之add_timer_on

2018-02-24 08:14 681 查看
add_timer_on 用于在形参指定的cpu上开始一个定时器.
其源码分析如下:
从这个函数可以知道每个cpu上都有timer的list.如果调用add_timer的时候没有指定cpu
其实就是运行在当前的cpu上。
void add_timer_on(struct timer_list *timer, int cpu)
{
struct timer_base *new_base, *base;
unsigned long flags;
#如果这个timer已经pending或者timer到期后的回调函数为null,则通过BUG_ON 打印当前的callstack
BUG_ON(timer_pending(timer) || !timer->function);
#得到在形参指定cpu上的timer_base 指针
new_base = get_timer_cpu_base(timer->flags, cpu);

/*
* If @timer was on a different CPU, it should be migrated with the
* old base locked to prevent other operations proceeding with the
* wrong base locked.  See lock_timer_base().
*/
#在对timer_base 操作期间需要锁定
base = lock_timer_base(timer, &flags);
if (base != new_base) {
timer->flags |= TIMER_MIGRATING;

raw_spin_unlock(&base->lock);
base = new_base;
raw_spin_lock(&base->lock);
WRITE_ONCE(timer->flags,
(timer->flags & ~TIMER_BASEMASK) | cpu);
}
#由于NOHZ的影响如果cpu处于idle,或者刚从idle 出来,base的时间可能不准,因此这里会检查是否需要调整
forward_timer_base(base);

debug_activate(timer, timer->expires);
#在形参指定cpu的timer_base上添加这个time
internal_add_timer(base, timer);
#解锁对形参cpu的base->lock的解锁
raw_spin_unlock_irqrestore(&base->lock, flags);
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: