mirror of
https://github.com/54shady/kernel_drivers_examples.git
synced 2025-08-15 02:39:36 +00:00
WorkQueue : delay work queue
This commit is contained in:
@ -16,6 +16,7 @@ endif
|
||||
|
||||
obj-m := workq_test.o
|
||||
obj-m += workq_test2.o
|
||||
obj-m += workq_test3.o
|
||||
KERNEL_DIR ?= /lib/modules/$(shell uname -r)/build
|
||||
KERNEL_BUID_OUTPUT ?=$(KERNEL_DIR)
|
||||
CC = $(CROSS_COMPILE)gcc
|
||||
|
53
debug/workqueue/workq_test3.c
Normal file
53
debug/workqueue/workq_test3.c
Normal file
@ -0,0 +1,53 @@
|
||||
#include <linux/kernel.h>
|
||||
#include <linux/module.h>
|
||||
#include <linux/workqueue.h>
|
||||
#include <linux/slab.h>
|
||||
|
||||
struct self_define_struct *g_sds; /* just for free sds memory */
|
||||
struct self_define_struct {
|
||||
struct delayed_work mywork;
|
||||
};
|
||||
static int g_cnt = 0;
|
||||
|
||||
static void mywork_handler(struct delayed_work *dwork)
|
||||
{
|
||||
struct self_define_struct *sds = container_of(dwork, struct self_define_struct, mywork);
|
||||
|
||||
printk("%s, %d[%d]\n", __FUNCTION__, __LINE__, g_cnt++);
|
||||
|
||||
/* do work again */
|
||||
schedule_delayed_work(&sds->mywork, msecs_to_jiffies(500 + g_cnt * 100));
|
||||
}
|
||||
|
||||
static int workq_test_init(void)
|
||||
{
|
||||
struct self_define_struct *sds;
|
||||
|
||||
sds = kzalloc(sizeof(struct self_define_struct), GFP_KERNEL);
|
||||
if (sds == NULL)
|
||||
return -ENOMEM;
|
||||
|
||||
/* for free the memory */
|
||||
g_sds = sds;
|
||||
|
||||
printk("%s, %d\n", __FUNCTION__, __LINE__);
|
||||
|
||||
/* init mywork */
|
||||
INIT_DELAYED_WORK(&sds->mywork, mywork_handler);
|
||||
|
||||
/* do my work */
|
||||
schedule_delayed_work(&sds->mywork, msecs_to_jiffies(500));
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void workq_test_exit(void)
|
||||
{
|
||||
printk("%s, %d\n", __FUNCTION__, __LINE__);
|
||||
cancel_delayed_work_sync(&g_sds->mywork);
|
||||
kfree(g_sds);
|
||||
}
|
||||
|
||||
module_init(workq_test_init)
|
||||
module_exit(workq_test_exit)
|
||||
MODULE_LICENSE("GPL");
|
Reference in New Issue
Block a user