lkmpg/examples/example_mutex.c
linD026 9289bfe59c
Fix the warnings raised by Sparse (#92)
Sparse[1] is a semantic parser, capable of finding out the potential
problems of Linux kernel code. This patch fixed the warnings.

[1] https://www.kernel.org/doc/html/latest/dev-tools/sparse.html
2021-09-04 17:53:29 +08:00

42 lines
784 B
C

/*
* example_mutex.c
*/
#include <linux/init.h>
#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");