关于__mod_timer函数(linux/kernel/timer.c中定义的)的问题

langwangrenzhengfei 2010-01-28 05:56:51
关于__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);
} ”这一段是什么意思?为什么要这样做?还有这一句上面的英文注释是什么意思?多谢!!!
...全文
2203 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 deep_pro 的回复:]
动态的timer被再次写入链表就成了静态的timer,具体的实现是靠internal_add_timer(new_base, timer);

[/Quote]

兄弟太谢谢你了,你真牛啊!咱俩认识下得了呗,我也在读内核,读的也很详细的
deep_pro 2010-01-29
  • 打赏
  • 举报
回复
动态的timer被再次写入链表就成了静态的timer,具体的实现是靠internal_add_timer(new_base, timer);
deep_pro 2010-01-29
  • 打赏
  • 举报
回复
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本身由动态转变成静态
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 deep_pro 的回复:]
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
[/Quote]


兄弟 你真是及时雨啊!讲得很好!“This also guarantees that the timer is serialized wrt itself” 请问这一句中的"wrt"是什么意思?能把这一句再详细得帮我解释一下吗?多谢!!!
deep_pro 2010-01-28
  • 打赏
  • 举报
回复
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

4,441

社区成员

发帖
与我相关
我的任务
社区描述
Linux/Unix社区 内核源代码研究区
社区管理员
  • 内核源代码研究区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧