samir82show

worker seqLK

Sep 26th, 2015
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.42 KB | None | 0 0
  1. #include <linux/module.h>
  2. #include <linux/semaphore.h>
  3. #include <linux/delay.h>
  4. #include <linux/completion.h>
  5. #include <linux/kernel.h>
  6. #include <linux/init.h>
  7. #include <linux/string.h>
  8. #include <linux/kthread.h>
  9. #include <linux/mutex.h>
  10. #include <linux/wait.h>
  11. MODULE_LICENSE("GPL");
  12. MODULE_AUTHOR("Sanfoundry-Student");
  13. MODULE_DESCRIPTION("Kernel Producer-Consumer Thread assignment");
  14. struct arg {
  15. char name[20];
  16. } arg[4] = {{"prod1"}, {"cons1"}, {"prod2"}, {"cons2"}};
  17.  
  18. static struct task_struct *threadtest0;
  19. static struct task_struct *threadtest1;
  20. static struct task_struct *threadtest2;
  21. static struct task_struct *threadtest3;
  22. int i;
  23. DECLARE_COMPLETION (compLK1);
  24. DECLARE_COMPLETION (compLK2);
  25. DEFINE_SPINLOCK (workLK);
  26. DEFINE_SEQLOCK(seqLK1);
  27. DEFINE_SEQLOCK(seqLK2);
  28. static DECLARE_MUTEX (sem);
  29. static int sanfd_producer (void *arg) {
  30. char *c = (char *)arg;
  31. while (!kthread_should_stop ()) {
  32. if ((strcmp (c, "prod1")) == 0) {
  33. write_seqlock (&seqLK1);
  34. i++;
  35. printk (KERN_INFO "%s written, %d\n", c, i);
  36. write_sequnlock (&seqLK1);
  37. schedule ();
  38. } else {
  39. write_seqlock (&seqLK2);
  40. i++;
  41. printk (KERN_INFO "%s written, %d\n", c, i);
  42. write_sequnlock (&seqLK2);
  43. schedule ();
  44. }
  45. }
  46. return 0;
  47. }
  48. static int sanfd_consumer (void *arg) {
  49. unsigned seq;
  50. char *c = (char *)arg;
  51. while (!kthread_should_stop ()) {
  52. if ((strcmp (c, "cons1")) == 0) {
  53. do {
  54. seq = read_seqbegin(&seqLK1);
  55. printk (KERN_INFO "%s read, %d\n", c, i);
  56. } while (read_seqretry(&seqLK1, seq));
  57. schedule ();
  58. } else {
  59. do {
  60. seq = read_seqbegin(&seqLK2);
  61. printk (KERN_INFO "%s read, %d\n", c, i);
  62. } while (read_seqretry(&seqLK2, seq));
  63. schedule ();
  64. }
  65. }
  66. return 0;
  67. }
  68.  
  69.  
  70. static int __init sanfd_kthread_init(void)
  71. {
  72. printk(KERN_INFO "Loaded kthreadwork module\n");
  73. threadtest0 = kthread_run (sanfd_producer, &arg[0], "%s%d\n", arg[0].name, i);
  74. threadtest1 = kthread_run (sanfd_consumer, &arg[1], "%s%d\n", arg[1].name, i);
  75. threadtest2 = kthread_run (sanfd_producer, &arg[2], "%s%d\n", arg[2].name, i);
  76. threadtest3 = kthread_run (sanfd_consumer, &arg[3], "%s%d\n", arg[3].name, i);
  77. if (!threadtest0 || !threadtest1 || !threadtest2 || !threadtest3) {
  78. printk (KERN_ERR "unable to start kernel thread\n");
  79. return 1;
  80. }
  81. printk (KERN_INFO "threads started\n");
  82. return 0; // Non-zero return means that the module couldn't be loaded.
  83. }
  84.  
  85. static void __exit sanfd_kthread_cleanup(void)
  86. {
  87. printk(KERN_INFO "Cleaning up kthreadwork module.\n");
  88. kthread_stop (threadtest0);
  89. kthread_stop (threadtest1);
  90. kthread_stop (threadtest2);
  91. kthread_stop (threadtest3);
  92. }
  93.  
  94. module_init(sanfd_kthread_init);
  95. module_exit(sanfd_kthread_cleanup);
Advertisement
Add Comment
Please, Sign In to add comment