对于cascade()函数它是确保之前定时器建立时internal_add_timer()定时器队列以及队列租得迁移更新工作,为什么要迁移,因为,系统在处理定时器时,比较的只是tv1组而已,也就是说,原来的tv1执行完之后,那么剩下的tv2,tv3,tv4,tv5将会先后迁移到tv1组:tv5 -> tv4 -> tv3 -> tv2-> tv1,这样定时器超时事件服务程序并不需要对每组的tv的超时事件进行检测,相比而言,也就提高了CPU的处理效率。那么这样一来timer 链表将发生变化,所以需要重新计算,重新实现internal_add_timer(),所以cascade ()函数代码如下:
- tatic int cascade(struct tvec_base *base,struct tvec *tv, int index)
- {
- /*cascade all the timers from tv up one level */
- structtimer_list *timer, *tmp;
- structlist_head tv_list;
- list_replace_init(tv->vec+ index, &tv_list); //
- list_for_each_entry_safe(timer,tmp, &tv_list, entry) {
- BUG_ON(tbase_get_base(timer->base)!= base); //确保本地cpu 的tvec_base没
- internal_add_timer(base,timer); //有发生改变
- }
- return index;
- }
通过以上分析,我们对Linux中系统定时器TIMER实现过程有所了解了。
