ae6d8abf8b38a7b2aa13eb98f58f9a363535971d
[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         unsigned long tmp = jiffies + 5 * HZ;
33         printk(KERN_INFO "Blocking other works in the queue\n");
34         printk(KERN_INFO "Executing task in CPU %d\n", smp_processor_id());
35         /* We also kick the CPU high */
36         while (!time_after(jiffies, tmp))
37                 cpu_relax();
38 }
39
40 static void work_print(struct work_struct *work)
41 {
42         printk(KERN_INFO "Phew! That was waiting!\n");
43         printk(KERN_INFO "Task done in CPU %d\n", smp_processor_id());
44 }
45
46 static struct workqueue_struct *block_wq;
47 static DECLARE_WORK(block_work, work_block);
48 static DECLARE_WORK(print_work, work_print);
49
50 static int block_wq_init(void)
51 {
52         block_wq = create_workqueue("block_wq");
53         if (!block_wq)
54                 return -ENOMEM;
55         printk(KERN_INFO "Queueing task in CPU %d\n", get_cpu());
56         put_cpu();
57         queue_work(block_wq, &block_work);
58         queue_work(block_wq, &print_work);
59         return 0;
60 }
61
62 static void block_wq_exit(void)
63 {
64         destroy_workqueue(block_wq);
65 }
66
67 module_init(block_wq_init);
68 module_exit(block_wq_exit);