mirror of
https://github.com/sysprog21/lkmpg.git
synced 2024-11-22 14:18:35 +08:00
10c7a9433a
This patch makes source listing shorter and more compact, that helps when browsing.
64 lines
1.4 KiB
C
64 lines
1.4 KiB
C
/*
|
|
* example_spinlock.c
|
|
*/
|
|
#include <linux/init.h>
|
|
#include <linux/interrupt.h>
|
|
#include <linux/kernel.h>
|
|
#include <linux/module.h>
|
|
#include <linux/spinlock.h>
|
|
|
|
DEFINE_SPINLOCK(sl_static);
|
|
spinlock_t sl_dynamic;
|
|
|
|
static void example_spinlock_static(void)
|
|
{
|
|
unsigned long flags;
|
|
|
|
spin_lock_irqsave(&sl_static, flags);
|
|
pr_info("Locked static spinlock\n");
|
|
|
|
/* Do something or other safely. Because this uses 100% CPU time, this
|
|
* code should take no more than a few milliseconds to run.
|
|
*/
|
|
|
|
spin_unlock_irqrestore(&sl_static, flags);
|
|
pr_info("Unlocked static spinlock\n");
|
|
}
|
|
|
|
static void example_spinlock_dynamic(void)
|
|
{
|
|
unsigned long flags;
|
|
|
|
spin_lock_init(&sl_dynamic);
|
|
spin_lock_irqsave(&sl_dynamic, flags);
|
|
pr_info("Locked dynamic spinlock\n");
|
|
|
|
/* Do something or other safely. Because this uses 100% CPU time, this
|
|
* code should take no more than a few milliseconds to run.
|
|
*/
|
|
|
|
spin_unlock_irqrestore(&sl_dynamic, flags);
|
|
pr_info("Unlocked dynamic spinlock\n");
|
|
}
|
|
|
|
static int example_spinlock_init(void)
|
|
{
|
|
pr_info("example spinlock started\n");
|
|
|
|
example_spinlock_static();
|
|
example_spinlock_dynamic();
|
|
|
|
return 0;
|
|
}
|
|
|
|
static void example_spinlock_exit(void)
|
|
{
|
|
pr_info("example spinlock exit\n");
|
|
}
|
|
|
|
module_init(example_spinlock_init);
|
|
module_exit(example_spinlock_exit);
|
|
|
|
MODULE_DESCRIPTION("Spinlock example");
|
|
MODULE_LICENSE("GPL");
|