63 lines
1.4 KiB
C
63 lines
1.4 KiB
C
/*
|
|
* example_spinlock.c
|
|
*/
|
|
#include <linux/init.h>
|
|
#include <linux/module.h>
|
|
#include <linux/printk.h>
|
|
#include <linux/spinlock.h>
|
|
|
|
static DEFINE_SPINLOCK(sl_static);
|
|
static spinlock_t sl_dynamic;
|
|
|
|
static void example_spinlock_static(void)
|
|
{
|
|
unsigned long flags;
|
|
|
|
spin_lock_irqsave(&sl_static, flags);
|
|
pr_info("已锁定静态自旋锁\n");
|
|
|
|
/* 执行一些安全的操作。由于这会占用 100% 的 CPU 时间,
|
|
* 这段代码应该运行时间不超过几毫秒。
|
|
*/
|
|
|
|
spin_unlock_irqrestore(&sl_static, flags);
|
|
pr_info("已解锁静态自旋锁\n");
|
|
}
|
|
|
|
static void example_spinlock_dynamic(void)
|
|
{
|
|
unsigned long flags;
|
|
|
|
spin_lock_init(&sl_dynamic);
|
|
spin_lock_irqsave(&sl_dynamic, flags);
|
|
pr_info("已锁定动态自旋锁\n");
|
|
|
|
/* 执行一些安全的操作。由于这会占用 100% 的 CPU 时间,
|
|
* 这段代码应该运行时间不超过几毫秒。
|
|
*/
|
|
|
|
spin_unlock_irqrestore(&sl_dynamic, flags);
|
|
pr_info("已解锁动态自旋锁\n");
|
|
}
|
|
|
|
static int __init example_spinlock_init(void)
|
|
{
|
|
pr_info("自旋锁示例启动\n");
|
|
|
|
example_spinlock_static();
|
|
example_spinlock_dynamic();
|
|
|
|
return 0;
|
|
}
|
|
|
|
static void __exit example_spinlock_exit(void)
|
|
{
|
|
pr_info("自旋锁示例退出\n");
|
|
}
|
|
|
|
module_init(example_spinlock_init);
|
|
module_exit(example_spinlock_exit);
|
|
|
|
MODULE_DESCRIPTION("自旋锁示例");
|
|
MODULE_LICENSE("GPL");
|