Do you need to call init_timer() again after a del_timer()

痴心易碎 提交于 2019-12-25 05:24:12

问题


I have a Linux module which creates timers, some of which may add themselves again during their handler function.

In some other cases, the timer is removed (perhaps before it's gone off) using del_timer_sync().

In that case, do I need to do the init_timer() call again on the struct before I next add_timer() or is that just a waste of (precious) interrupt latency?


回答1:


To answer my own question, I believe I do need to init_timer() my struct after any del_timer() or del_timer_sync() if I ever intend to access the struct again - for example, when doing a timer_pending() or something during module cleanup.

I think in the case of writing a kernel module which potentially re-uses a timer, the best thing to do is:

static struct timer_list my_timer;

...

static void remove_my_timer(void)
{
  if (timer_pending(&my_timer))
  {
    del_timer_sync(&my_timer);
    init_timer(&my_timer);
  }
}

static void arm_my_timer(...)
{
  remove_my_timer();
  my_timer.expires  = ...;
  my_timer.data     = ...;
  my_timer.function = ...;
  add_timer(&my_timer);
}

...

int __init init_my_device(void)
{
  ...
  init_timer(&my_timer);
  ...
}

void __exit cleanup_my_device(void)
{
  ...
  remove_my_timer();
  ...
}

Hope that helps someone else in future.



来源:https://stackoverflow.com/questions/19979276/do-you-need-to-call-init-timer-again-after-a-del-timer

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!