Last active
August 29, 2022 23:18
-
-
Save yagihiro/309746 to your computer and use it in GitHub Desktop.
workqueue sample on linux kernel
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <linux/module.h> | |
#include <linux/kernel.h> | |
#include <linux/workqueue.h> | |
static void mykmod_work_handler(struct work_struct *w); | |
static struct workqueue_struct *wq = 0; | |
static DECLARE_DELAYED_WORK(mykmod_work, mykmod_work_handler); | |
static unsigned long onesec; | |
static void | |
mykmod_work_handler(struct work_struct *w) | |
{ | |
pr_info("mykmod work %u jiffies\n", (unsigned)onesec); | |
} | |
static int __devinit mykmod_init(void) | |
{ | |
onesec = msecs_to_jiffies(1000); | |
pr_info("mykmod loaded %u jiffies\n", (unsigned)onesec); | |
if (!wq) | |
wq = create_singlethread_workqueue("mykmod"); | |
if (wq) | |
queue_delayed_work(wq, &mykmod_work, onesec); | |
return 0; | |
} | |
static void __devexit mykmod_exit(void) | |
{ | |
if (wq) | |
destroy_workqueue(wq); | |
pr_info("mykmod exit\n"); | |
} | |
module_init(mykmod_init); | |
module_exit(mykmod_exit); | |
MODULE_DESCRIPTION("mykmod"); | |
MODULE_LICENSE("GPL"); |
You are a lifesaver, arxchruncher, I had spent an hour trying to figure out why my kernel crashed.
Here is a minimal working example using cancel_work_sync https://github.com/cirosantilli/linux-kernel-module-cheat/blob/ad077d3943f79c0f6481dab929970613c33c31a7/kernel_module/workqueue_cheat.c
Great work! @cirosantilli
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Don't forget to cancel the possible work in the queue (cancel_delayed_work_sync) when you close the workqueue, otherwise your kernel will crash.