当前位置: 技术问答>linux和unix
关于__mod_timer函数(linux/kernel/timer.c中定义的)的问题
来源: 互联网 发布时间:2016-08-02
本文导语: 关于__mod_timer函数(kernel-2.6.13/timer.c中定义的)的问题 函数代码如下: int __mod_timer(struct timer_list *timer, unsigned long expires) { timer_base_t *base; tvec_base_t *new_base; unsigned long flags; int ret = 0; BUG_ON(!timer->function); che...
关于__mod_timer函数(kernel-2.6.13/timer.c中定义的)的问题
函数代码如下:
int __mod_timer(struct timer_list *timer, unsigned long expires)
{
timer_base_t *base;
tvec_base_t *new_base;
unsigned long flags;
int ret = 0;
BUG_ON(!timer->function);
check_timer(timer);
base = lock_timer_base(timer, &flags);
if (timer_pending(timer)) {
detach_timer(timer, 0);
ret = 1;
}
new_base = &__get_cpu_var(tvec_bases);
if (base != &new_base->t_base) {
/*
* We are trying to schedule the timer on the local CPU.
* However we can't change timer's base while it is running,
* otherwise del_timer_sync() can't detect that the timer's
* handler yet has not finished. This also guarantees that
* the timer is serialized wrt itself.
*/
if (unlikely(base->running_timer == timer)) {
/* The timer remains on a former base */
new_base = container_of(base, tvec_base_t, t_base);
} else {
/* See the comment in lock_timer_base() */
timer->base = NULL;
spin_unlock(&base->lock);
spin_lock(&new_base->t_base.lock);
timer->base = &new_base->t_base;
}
}
timer->expires = expires;
internal_add_timer(new_base, timer);
spin_unlock_irqrestore(&new_base->t_base.lock, flags);
return ret;
}
哪位大侠帮解释一下:“if (unlikely(base->running_timer == timer)) {
/* The timer remains on a former base */
new_base = container_of(base, tvec_base_t, t_base);
} ”这一段是什么意思?为什么要这样做?还有这一句上面的英文注释是什么意思?多谢!!!
函数代码如下:
int __mod_timer(struct timer_list *timer, unsigned long expires)
{
timer_base_t *base;
tvec_base_t *new_base;
unsigned long flags;
int ret = 0;
BUG_ON(!timer->function);
check_timer(timer);
base = lock_timer_base(timer, &flags);
if (timer_pending(timer)) {
detach_timer(timer, 0);
ret = 1;
}
new_base = &__get_cpu_var(tvec_bases);
if (base != &new_base->t_base) {
/*
* We are trying to schedule the timer on the local CPU.
* However we can't change timer's base while it is running,
* otherwise del_timer_sync() can't detect that the timer's
* handler yet has not finished. This also guarantees that
* the timer is serialized wrt itself.
*/
if (unlikely(base->running_timer == timer)) {
/* The timer remains on a former base */
new_base = container_of(base, tvec_base_t, t_base);
} else {
/* See the comment in lock_timer_base() */
timer->base = NULL;
spin_unlock(&base->lock);
spin_lock(&new_base->t_base.lock);
timer->base = &new_base->t_base;
}
}
timer->expires = expires;
internal_add_timer(new_base, timer);
spin_unlock_irqrestore(&new_base->t_base.lock, flags);
return ret;
}
哪位大侠帮解释一下:“if (unlikely(base->running_timer == timer)) {
/* The timer remains on a former base */
new_base = container_of(base, tvec_base_t, t_base);
} ”这一段是什么意思?为什么要这样做?还有这一句上面的英文注释是什么意思?多谢!!!
|
lz,很不好意思,还是我
if (base != &new_base->t_base) {
/*
* We are trying to schedule the timer on the local CPU.
* However we can't change timer's base while it is running,
* otherwise del_timer_sync() can't detect that the timer's
* handler yet has not finished. This also guarantees that
* the timer is serialized wrt itself.
*/
首先if (base != &new_base->t_base)判断要处理的timer是否是属于当前cpu的
因为英文注释大意是,我们尝试将该timer加入到当前cpu中,如果条件不成立,则就不需要进行搬移工作了
接着又说,然而,我们不能在timer正在被执行(timer->function ,因为__mod_timer不仅能添加新的timer,也能修改已经添加进t_base的timer,这里指的是修改已经被添加过一次的timer,可能正在被执行)的时候进行搬移(change timer's base ),此外 del_timer_sync() 也不能察觉到timer的处理还没有完成。因此,以下操作要保证timer自己处理自己的锁(序列化,因为此时在不同的cpu上都在对timer进行操作)
if (unlikely(base->running_timer == timer)) {
/* The timer remains on a former base */
new_base = container_of(base, tvec_base_t, t_base);
} ”
说的是很不幸发生了不太容易发生的事情(unlikely()),我们想修改的timer已经在别的cpu上处于运行状态,我们无法再搬移它到当前cpu,所以new_base还是指向了timer以前所在的cpu
if (base != &new_base->t_base) {
/*
* We are trying to schedule the timer on the local CPU.
* However we can't change timer's base while it is running,
* otherwise del_timer_sync() can't detect that the timer's
* handler yet has not finished. This also guarantees that
* the timer is serialized wrt itself.
*/
首先if (base != &new_base->t_base)判断要处理的timer是否是属于当前cpu的
因为英文注释大意是,我们尝试将该timer加入到当前cpu中,如果条件不成立,则就不需要进行搬移工作了
接着又说,然而,我们不能在timer正在被执行(timer->function ,因为__mod_timer不仅能添加新的timer,也能修改已经添加进t_base的timer,这里指的是修改已经被添加过一次的timer,可能正在被执行)的时候进行搬移(change timer's base ),此外 del_timer_sync() 也不能察觉到timer的处理还没有完成。因此,以下操作要保证timer自己处理自己的锁(序列化,因为此时在不同的cpu上都在对timer进行操作)
if (unlikely(base->running_timer == timer)) {
/* The timer remains on a former base */
new_base = container_of(base, tvec_base_t, t_base);
} ”
说的是很不幸发生了不太容易发生的事情(unlikely()),我们想修改的timer已经在别的cpu上处于运行状态,我们无法再搬移它到当前cpu,所以new_base还是指向了timer以前所在的cpu
|
This also guarantees that the timer is serialized wrt itself
--------------------
wrt是write的缩写,写入
serialize,序列化,我的理解是将动态的对象(不是oop里的对象,仅是一个代词,sth之类的意思)
转换为静态的记录。
就是说timer在正在被执行的时候是动态的,而它在tvec_base_t里作为链表元素存储时是静态的
This also guarantees that the timer is serialized wrt itself
说的是当前要修改到期时间的timer正在被处理的时候,
以下代码将确保这个timer本身由动态转变成静态
--------------------
wrt是write的缩写,写入
serialize,序列化,我的理解是将动态的对象(不是oop里的对象,仅是一个代词,sth之类的意思)
转换为静态的记录。
就是说timer在正在被执行的时候是动态的,而它在tvec_base_t里作为链表元素存储时是静态的
This also guarantees that the timer is serialized wrt itself
说的是当前要修改到期时间的timer正在被处理的时候,
以下代码将确保这个timer本身由动态转变成静态
您可能感兴趣的文章:
本站(WWW.)旨在分享和传播互联网科技相关的资讯和技术,将尽最大努力为读者提供更好的信息聚合和浏览方式。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。