workqueue_cheat.c 788 B

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. /* https://github.com/cirosantilli/linux-kernel-module-cheat#workqueues */
  2. #include <linux/delay.h> /* usleep_range */
  3. #include <linux/kernel.h>
  4. #include <linux/module.h>
  5. #include <linux/types.h> /* atomic_t */
  6. #include <linux/workqueue.h>
  7. static struct workqueue_struct *queue;
  8. static atomic_t run = ATOMIC_INIT(1);
  9. static void work_func(struct work_struct *work)
  10. {
  11. int i = 0;
  12. while (atomic_read(&run)) {
  13. pr_info("%d\n", i);
  14. usleep_range(1000000, 1000001);
  15. i++;
  16. if (i == 10)
  17. i = 0;
  18. }
  19. }
  20. DECLARE_WORK(work, work_func);
  21. static int myinit(void)
  22. {
  23. queue = create_workqueue("myworkqueue");
  24. queue_work(queue, &work);
  25. return 0;
  26. }
  27. static void myexit(void)
  28. {
  29. atomic_set(&run, 0);
  30. destroy_workqueue(queue);
  31. }
  32. module_init(myinit)
  33. module_exit(myexit)
  34. MODULE_LICENSE("GPL");