2021-07-22 11:25:32 +08:00
|
|
|
/*
|
2021-08-08 01:24:59 +08:00
|
|
|
* example_rwlock.c
|
2021-07-22 11:25:32 +08:00
|
|
|
*/
|
2021-07-22 06:35:24 +08:00
|
|
|
#include <linux/module.h>
|
2023-02-23 18:41:23 +08:00
|
|
|
#include <linux/printk.h>
|
2023-02-22 23:43:27 +08:00
|
|
|
#include <linux/rwlock.h>
|
2021-07-22 06:35:24 +08:00
|
|
|
|
2021-09-04 17:53:29 +08:00
|
|
|
static DEFINE_RWLOCK(myrwlock);
|
2021-07-22 06:35:24 +08:00
|
|
|
|
|
|
|
static void example_read_lock(void)
|
|
|
|
{
|
|
|
|
unsigned long flags;
|
|
|
|
|
|
|
|
read_lock_irqsave(&myrwlock, flags);
|
|
|
|
pr_info("Read Locked\n");
|
|
|
|
|
|
|
|
/* Read from something */
|
|
|
|
|
|
|
|
read_unlock_irqrestore(&myrwlock, flags);
|
|
|
|
pr_info("Read Unlocked\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
static void example_write_lock(void)
|
|
|
|
{
|
|
|
|
unsigned long flags;
|
|
|
|
|
|
|
|
write_lock_irqsave(&myrwlock, flags);
|
|
|
|
pr_info("Write Locked\n");
|
|
|
|
|
|
|
|
/* Write to something */
|
|
|
|
|
|
|
|
write_unlock_irqrestore(&myrwlock, flags);
|
|
|
|
pr_info("Write Unlocked\n");
|
|
|
|
}
|
|
|
|
|
2023-07-05 09:44:21 +08:00
|
|
|
static int __init example_rwlock_init(void)
|
2021-07-22 06:35:24 +08:00
|
|
|
{
|
|
|
|
pr_info("example_rwlock started\n");
|
|
|
|
|
|
|
|
example_read_lock();
|
|
|
|
example_write_lock();
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2023-07-05 09:44:21 +08:00
|
|
|
static void __exit example_rwlock_exit(void)
|
2021-07-22 06:35:24 +08:00
|
|
|
{
|
|
|
|
pr_info("example_rwlock exit\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
module_init(example_rwlock_init);
|
|
|
|
module_exit(example_rwlock_exit);
|
|
|
|
|
|
|
|
MODULE_DESCRIPTION("Read/Write locks example");
|
|
|
|
MODULE_LICENSE("GPL");
|