timer.c 897 B

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. /*
  2. Timers are callbacks that run when an interrupt happens, from the interrupt context itself.
  3. Therefore they produce more accurate timing than thread scheduling, which is more complex,
  4. but you can't do too much work inside of them.
  5. See also:
  6. - http://stackoverflow.com/questions/10812858/timers-in-linux-device-drivers
  7. - https://gist.github.com/yagihiro/310149
  8. */
  9. #include <linux/jiffies.h>
  10. #include <linux/kernel.h>
  11. #include <linux/module.h>
  12. #include <linux/timer.h>
  13. MODULE_LICENSE("GPL");
  14. static void callback(unsigned long data);
  15. static unsigned long onesec;
  16. DEFINE_TIMER(mytimer, callback, 0, 0);
  17. static void callback(unsigned long data)
  18. {
  19. pr_info("%u\n", (unsigned)jiffies);
  20. mod_timer(&mytimer, jiffies + onesec);
  21. }
  22. int init_module(void)
  23. {
  24. onesec = msecs_to_jiffies(1000);
  25. mod_timer(&mytimer, jiffies + onesec);
  26. return 0;
  27. }
  28. void cleanup_module(void)
  29. {
  30. del_timer(&mytimer);
  31. }