mirror of
https://github.com/sysprog21/lkmpg.git
synced 2024-11-22 15:05:54 +08:00
e62dff0df4
The rule of thumb is to include the headers we are the direct user of. In particular, if we need an atomic API, we include <linux/atomic.h>. On the other hand we should not use headers for no reason. In particular, if we are not doing any IRQ job, why is the <linux/irq.h> included? Signed-off-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
41 lines
760 B
C
41 lines
760 B
C
/*
|
|
* example_mutex.c
|
|
*/
|
|
#include <linux/kernel.h>
|
|
#include <linux/module.h>
|
|
#include <linux/mutex.h>
|
|
|
|
static DEFINE_MUTEX(mymutex);
|
|
|
|
static int example_mutex_init(void)
|
|
{
|
|
int ret;
|
|
|
|
pr_info("example_mutex init\n");
|
|
|
|
ret = mutex_trylock(&mymutex);
|
|
if (ret != 0) {
|
|
pr_info("mutex is locked\n");
|
|
|
|
if (mutex_is_locked(&mymutex) == 0)
|
|
pr_info("The mutex failed to lock!\n");
|
|
|
|
mutex_unlock(&mymutex);
|
|
pr_info("mutex is unlocked\n");
|
|
} else
|
|
pr_info("Failed to lock\n");
|
|
|
|
return 0;
|
|
}
|
|
|
|
static void example_mutex_exit(void)
|
|
{
|
|
pr_info("example_mutex exit\n");
|
|
}
|
|
|
|
module_init(example_mutex_init);
|
|
module_exit(example_mutex_exit);
|
|
|
|
MODULE_DESCRIPTION("Mutex example");
|
|
MODULE_LICENSE("GPL");
|