completions.c 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. /*
  2. * completions.c
  3. */
  4. #include <linux/completion.h>
  5. #include <linux/err.h> /* for IS_ERR() */
  6. #include <linux/init.h>
  7. #include <linux/kthread.h>
  8. #include <linux/module.h>
  9. #include <linux/printk.h>
  10. static struct {
  11. struct completion crank_comp;
  12. struct completion flywheel_comp;
  13. } machine;
  14. static int machine_crank_thread(void *arg)
  15. {
  16. pr_info("Turn the crank\n");
  17. complete_all(&machine.crank_comp);
  18. complete_and_exit(&machine.crank_comp, 0);
  19. }
  20. static int machine_flywheel_spinup_thread(void *arg)
  21. {
  22. wait_for_completion(&machine.crank_comp);
  23. pr_info("Flywheel spins up\n");
  24. complete_all(&machine.flywheel_comp);
  25. complete_and_exit(&machine.flywheel_comp, 0);
  26. }
  27. static int completions_init(void)
  28. {
  29. struct task_struct *crank_thread;
  30. struct task_struct *flywheel_thread;
  31. pr_info("completions example\n");
  32. init_completion(&machine.crank_comp);
  33. init_completion(&machine.flywheel_comp);
  34. crank_thread = kthread_create(machine_crank_thread, NULL, "KThread Crank");
  35. if (IS_ERR(crank_thread))
  36. goto ERROR_THREAD_1;
  37. flywheel_thread = kthread_create(machine_flywheel_spinup_thread, NULL,
  38. "KThread Flywheel");
  39. if (IS_ERR(flywheel_thread))
  40. goto ERROR_THREAD_2;
  41. wake_up_process(flywheel_thread);
  42. wake_up_process(crank_thread);
  43. return 0;
  44. ERROR_THREAD_2:
  45. kthread_stop(crank_thread);
  46. ERROR_THREAD_1:
  47. return -1;
  48. }
  49. static void completions_exit(void)
  50. {
  51. wait_for_completion(&machine.crank_comp);
  52. wait_for_completion(&machine.flywheel_comp);
  53. pr_info("completions exit\n");
  54. }
  55. module_init(completions_init);
  56. module_exit(completions_exit);
  57. MODULE_DESCRIPTION("Completions example");
  58. MODULE_LICENSE("GPL");