-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdefer_workqueue.c
70 lines (60 loc) · 2.36 KB
/
defer_workqueue.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
/* Copyright (C) 2018, Savio Machado <[email protected]>
* All Rights Reserved
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
#include <linux/init.h>
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/workqueue.h>
/**
* if you need your bottom-half to sleep you need workqueues. workqueues
* have all the benefits of the process context besides being schedulable.
* it's usually used for example when your defered work needs to allocate
* a lot of memory or perform blocking i/o. kernel threads would indeed be
* an alternative to workqueues but the latter do have a much better api.
* if you don't need a kernel thread or, if your defered work doesn't need
* to sleep, use tasklets instead. see defer_tasklet.c.
*
* roughly speeking the workqueues subsystem actually provides an an
* interface to create kernel threads tohandle work queued previously
* from another part of the code. these kernel threads are called worker
* threads. the default worker threads are called event/n where n is the
* cpu number. there is one worker thread per cpu.
*/
void somework(struct work_struct *);
DECLARE_WORK(somework_, somework);
void somework(struct work_struct *data)
{
pr_info("somework()\n");
}
static int __init defer_workqueue_init(void)
{
pr_debug("loaded at 0x%p\n", defer_workqueue_init);
schedule_work(&somework_);
return 0;
}
static void __exit defer_workqueue_exit(void)
{
flush_scheduled_work();
pr_debug("unloaded from 0x%p\n", defer_workqueue_exit);
}
module_init(defer_workqueue_init);
module_exit(defer_workqueue_exit);
MODULE_LICENSE("GPL v2");
MODULE_VERSION("0.0.1");
MODULE_AUTHOR("sav <[email protected]>");
MODULE_DESCRIPTION(__FILE__);