example_rwlock.c 1009 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. #include <linux/kernel.h>
  2. #include <linux/module.h>
  3. #include <linux/interrupt.h>
  4. DEFINE_RWLOCK(myrwlock);
  5. static void example_read_lock(void)
  6. {
  7. unsigned long flags;
  8. read_lock_irqsave(&myrwlock, flags);
  9. pr_info("Read Locked\n");
  10. /* Read from something */
  11. read_unlock_irqrestore(&myrwlock, flags);
  12. pr_info("Read Unlocked\n");
  13. }
  14. static void example_write_lock(void)
  15. {
  16. unsigned long flags;
  17. write_lock_irqsave(&myrwlock, flags);
  18. pr_info("Write Locked\n");
  19. /* Write to something */
  20. write_unlock_irqrestore(&myrwlock, flags);
  21. pr_info("Write Unlocked\n");
  22. }
  23. static int example_rwlock_init(void)
  24. {
  25. pr_info("example_rwlock started\n");
  26. example_read_lock();
  27. example_write_lock();
  28. return 0;
  29. }
  30. static void example_rwlock_exit(void)
  31. {
  32. pr_info("example_rwlock exit\n");
  33. }
  34. module_init(example_rwlock_init);
  35. module_exit(example_rwlock_exit);
  36. MODULE_AUTHOR("Bob Mottram");
  37. MODULE_DESCRIPTION("Read/Write locks example");
  38. MODULE_LICENSE("GPL");