[NFS] Set CONFIG_KEYS when CONFIG_NFS_USE_KERNEL_DNS is set
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / samples / kfifo / inttype-example.c
blobd6c5b7d9df64ada598c500bc2e1293200fffe006
1 /*
2 * Sample kfifo int type implementation
4 * Copyright (C) 2010 Stefani Seibold <stefani@seibold.net>
6 * Released under the GPL version 2 only.
8 */
10 #include <linux/init.h>
11 #include <linux/module.h>
12 #include <linux/proc_fs.h>
13 #include <linux/mutex.h>
14 #include <linux/kfifo.h>
17 * This module shows how to create a int type fifo.
20 /* fifo size in elements (ints) */
21 #define FIFO_SIZE 32
23 /* name of the proc entry */
24 #define PROC_FIFO "int-fifo"
26 /* lock for procfs read access */
27 static DEFINE_MUTEX(read_lock);
29 /* lock for procfs write access */
30 static DEFINE_MUTEX(write_lock);
33 * define DYNAMIC in this example for a dynamically allocated fifo.
35 * Otherwise the fifo storage will be a part of the fifo structure.
37 #if 0
38 #define DYNAMIC
39 #endif
41 #ifdef DYNAMIC
42 static DECLARE_KFIFO_PTR(test, int);
43 #else
44 static DEFINE_KFIFO(test, int, FIFO_SIZE);
45 #endif
47 static int __init testfunc(void)
49 int buf[6];
50 int i;
51 unsigned int ret;
53 printk(KERN_INFO "int fifo test start\n");
55 /* put values into the fifo */
56 for (i = 0; i != 10; i++)
57 kfifo_put(&test, &i);
59 /* show the number of used elements */
60 printk(KERN_INFO "fifo len: %u\n", kfifo_len(&test));
62 /* get max of 2 elements from the fifo */
63 ret = kfifo_out(&test, buf, 2);
64 printk(KERN_INFO "ret: %d\n", ret);
65 /* and put it back to the end of the fifo */
66 ret = kfifo_in(&test, buf, ret);
67 printk(KERN_INFO "ret: %d\n", ret);
69 for (i = 20; i != 30; i++)
70 kfifo_put(&test, &i);
72 printk(KERN_INFO "queue len: %u\n", kfifo_len(&test));
74 /* show the first value without removing from the fifo */
75 if (kfifo_peek(&test, &i))
76 printk(KERN_INFO "%d\n", i);
78 /* print out all values in the fifo */
79 while (kfifo_get(&test, &i))
80 printk("%d ", i);
81 printk("\n");
83 return 0;
86 static ssize_t fifo_write(struct file *file, const char __user *buf,
87 size_t count, loff_t *ppos)
89 int ret;
90 unsigned int copied;
92 if (mutex_lock_interruptible(&write_lock))
93 return -ERESTARTSYS;
95 ret = kfifo_from_user(&test, buf, count, &copied);
97 mutex_unlock(&write_lock);
99 return ret ? ret : copied;
102 static ssize_t fifo_read(struct file *file, char __user *buf,
103 size_t count, loff_t *ppos)
105 int ret;
106 unsigned int copied;
108 if (mutex_lock_interruptible(&read_lock))
109 return -ERESTARTSYS;
111 ret = kfifo_to_user(&test, buf, count, &copied);
113 mutex_unlock(&read_lock);
115 return ret ? ret : copied;
118 static const struct file_operations fifo_fops = {
119 .owner = THIS_MODULE,
120 .read = fifo_read,
121 .write = fifo_write,
124 static int __init example_init(void)
126 #ifdef DYNAMIC
127 int ret;
129 ret = kfifo_alloc(&test, FIFO_SIZE, GFP_KERNEL);
130 if (ret) {
131 printk(KERN_ERR "error kfifo_alloc\n");
132 return ret;
134 #endif
135 testfunc();
137 if (proc_create(PROC_FIFO, 0, NULL, &fifo_fops) == NULL) {
138 #ifdef DYNAMIC
139 kfifo_free(&test);
140 #endif
141 return -ENOMEM;
143 return 0;
146 static void __exit example_exit(void)
148 remove_proc_entry(PROC_FIFO, NULL);
149 #ifdef DYNAMIC
150 kfifo_free(&test);
151 #endif
154 module_init(example_init);
155 module_exit(example_exit);
156 MODULE_LICENSE("GPL");
157 MODULE_AUTHOR("Stefani Seibold <stefani@seibold.net>");