2021-07-22 11:25:32 +08:00
|
|
|
/*
|
2021-08-08 01:24:59 +08:00
|
|
|
* example_mutex.c
|
2021-07-22 11:25:32 +08:00
|
|
|
*/
|
2021-07-22 06:35:24 +08:00
|
|
|
#include <linux/module.h>
|
|
|
|
#include <linux/mutex.h>
|
2023-02-23 18:41:23 +08:00
|
|
|
#include <linux/printk.h>
|
2021-07-22 06:35:24 +08:00
|
|
|
|
2021-09-04 17:53:29 +08:00
|
|
|
static DEFINE_MUTEX(mymutex);
|
2021-07-22 06:35:24 +08:00
|
|
|
|
2023-07-05 09:44:21 +08:00
|
|
|
static int __init example_mutex_init(void)
|
2021-07-22 06:35:24 +08:00
|
|
|
{
|
|
|
|
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");
|
2021-07-22 06:58:13 +08:00
|
|
|
} else
|
2021-07-22 06:35:24 +08:00
|
|
|
pr_info("Failed to lock\n");
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2023-07-05 09:44:21 +08:00
|
|
|
static void __exit example_mutex_exit(void)
|
2021-07-22 06:35:24 +08:00
|
|
|
{
|
|
|
|
pr_info("example_mutex exit\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
module_init(example_mutex_init);
|
|
|
|
module_exit(example_mutex_exit);
|
|
|
|
|
|
|
|
MODULE_DESCRIPTION("Mutex example");
|
|
|
|
MODULE_LICENSE("GPL");
|