Use single threaded work queue.
[cascardo/kernel/samples/workqueue/.git] / block_wq.c
1 /*
2  *  Copyright (C) 2010  Thadeu Lima de Souza Cascardo <cascardo@holoscopio.com>
3  *
4  *  This program is free software; you can redistribute it and/or modify
5  *  it under the terms of the GNU General Public License as published by
6  *  the Free Software Foundation; either version 2 of the License, or
7  *  (at your option) any later version.
8  *
9  *  This program is distributed in the hope that it will be useful,
10  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  *  GNU General Public License for more details.
13  *
14  *  You should have received a copy of the GNU General Public License along
15  *  with this program; if not, write to the Free Software Foundation, Inc.,
16  *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17  */
18
19 /*
20  * This code demonstrates why a work should not block for too long.
21  * This is worse in the case that the work is scheduled in keventd.
22  */
23
24 #include <linux/module.h>
25 #include <linux/workqueue.h>
26
27 MODULE_LICENSE("GPL");
28 MODULE_AUTHOR("Thadeu Lima de Souza Cascardo");
29
30 static void work_block(struct work_struct *work)
31 {
32         printk(KERN_INFO "Blocking other works in the queue\n");
33         printk(KERN_INFO "Executing task in CPU %d\n", smp_processor_id());
34         msleep(5);
35 }
36
37 static void work_print(struct work_struct *work)
38 {
39         printk(KERN_INFO "Phew! That was waiting!\n");
40         printk(KERN_INFO "Task done in CPU %d\n", smp_processor_id());
41 }
42
43 static struct workqueue_struct *block_wq;
44 static DECLARE_WORK(block_work, work_block);
45 static DECLARE_WORK(print_work, work_print);
46
47 static int block_wq_init(void)
48 {
49         block_wq = create_singlethread_workqueue("block_wq");
50         if (!block_wq)
51                 return -ENOMEM;
52         printk(KERN_INFO "Queueing task in CPU %d\n", get_cpu());
53         put_cpu();
54         queue_work(block_wq, &block_work);
55         queue_work(block_wq, &print_work);
56         return 0;
57 }
58
59 static void block_wq_exit(void)
60 {
61         destroy_workqueue(block_wq);
62 }
63
64 module_init(block_wq_init);
65 module_exit(block_wq_exit);