Skip to content

Instantly share code, notes, and snippets.

@itrobotics
Last active March 16, 2021 18:07
Show Gist options
  • Save itrobotics/b4f5597585b74525e00e to your computer and use it in GitHub Desktop.
Save itrobotics/b4f5597585b74525e00e to your computer and use it in GitHub Desktop.
a simple example of work_queue in Linux kernel
/*******************************************************************************
* Copyright (c) 2015 Song Yang @ ittraining
*
* All rights reserved.
* This program is free to use, but the ban on selling behavior.
* Modify the program must keep all the original text description.
*
* 保留所有權利。
* 本程式可任意使用,但是禁止販售行為。
* 修改程式時必須保留所有原有文字說明。
*
* Email: onionys@ittraining.com.tw
* Blog : http://blog.ittraining.com.tw
*******************************************************************************/
#include <linux/module.h>
#include <linux/init.h>
#include <linux/interrupt.h>
MODULE_LICENSE("Dual BSD/GPL");
static void my_tasklet_handler(unsigned long flag);
DECLARE_TASKLET(my_tasklet, my_tasklet_handler, 0);
static void my_tasklet_handler(unsigned long flag)
{
tasklet_disable(&my_tasklet);
printk("my_tasklet run: do what the tasklet want to do....\n");
tasklet_enable(&my_tasklet);
}
static int hello_tasklet_init(void)
{
printk("module init start. \n");
printk("Hello tasklet!\n");
tasklet_schedule(&my_tasklet);
printk("module init end.\n");
return 0;
}
static void hello_tasklet_exit(void)
{
tasklet_kill(&my_tasklet);
printk("Goodbye, tasklet!\n");
}
module_init(hello_tasklet_init);
module_exit(hello_tasklet_exit);
/*******************************************************************************
* Copyright (c) 2015 Song Yang @ ittraining
*
* All rights reserved.
* This program is free to use, but the ban on selling behavior.
* Modify the program must keep all the original text description.
*
* 保留所有權利。
* 本程式可任意使用,但是禁止販售行為。
* 修改程式時必須保留所有原有文字說明。
*
* Email: onionys@ittraining.com.tw
* Blog : http://blog.ittraining.com.tw
*******************************************************************************/
#include <linux/init.h>
#include <linux/module.h>
#include <linux/interrupt.h>
#include <linux/delay.h>
MODULE_LICENSE("Dual BSD/GPL");
struct work_struct workq;
void my_workqueue_handler(struct work_struct *work)
{
printk("hello work queue: START my_workqueue_handler() \n");
msleep(3000); /* sleep */
printk("hello work queue: END my_workqueue_handler() \n");
}
static int my_init(void)
{
int ret = 0;
printk("hello work queue: START module_init() \n");
INIT_WORK(&workq, my_workqueue_handler);
schedule_work(&workq);
printk("hello work queue: END module_init() \n");
return 0;
}
static void my_exit(void)
{
flush_scheduled_work();
printk("my driver removed.\n");
}
module_init(my_init);
module_exit(my_exit);
@qodroi
Copy link

qodroi commented Mar 16, 2021

hello_tasklet doesn't work at all, freezes my system on load.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment