work_from_work.c 780 B

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. /* https://cirosantilli.com/linux-kernel-module-cheat#workqueue-from-workqueue */
  2. #include <linux/kernel.h>
  3. #include <linux/module.h>
  4. #include <linux/workqueue.h>
  5. static int i;
  6. static struct workqueue_struct *queue;
  7. static void work_func(struct work_struct *work);
  8. DECLARE_DELAYED_WORK(next_work, work_func);
  9. DECLARE_WORK(work, work_func);
  10. static void work_func(struct work_struct *work)
  11. {
  12. pr_info("%d\n", i);
  13. i++;
  14. if (i == 10)
  15. i = 0;
  16. queue_delayed_work(queue, &next_work, HZ);
  17. }
  18. static int myinit(void)
  19. {
  20. queue = create_workqueue("myworkqueue");
  21. queue_work(queue, &work);
  22. return 0;
  23. }
  24. static void myexit(void)
  25. {
  26. cancel_delayed_work(&next_work);
  27. flush_workqueue(queue);
  28. destroy_workqueue(queue);
  29. }
  30. module_init(myinit)
  31. module_exit(myexit)
  32. MODULE_LICENSE("GPL");