example_rwlock.c 1013 B

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