First test of use of completion.
authorThadeu Lima de Souza Cascardo <cascardo@holoscopio.com>
Fri, 25 Sep 2009 10:39:08 +0000 (07:39 -0300)
committerThadeu Lima de Souza Cascardo <cascardo@holoscopio.com>
Fri, 25 Sep 2009 10:39:08 +0000 (07:39 -0300)
Makefile [new file with mode: 0644]
test_completion.c [new file with mode: 0644]

diff --git a/Makefile b/Makefile
new file mode 100644 (file)
index 0000000..6801ea2
--- /dev/null
+++ b/Makefile
@@ -0,0 +1 @@
+obj-m += test_completion.o
diff --git a/test_completion.c b/test_completion.c
new file mode 100644 (file)
index 0000000..4e0fa70
--- /dev/null
@@ -0,0 +1,94 @@
+/*
+ *  Copyright (C) 2009  Thadeu Lima de Souza Cascardo <cascardo@holoscopio.com>
+ *
+ *  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.
+ */
+
+
+#include <linux/module.h>
+#include <linux/completion.h>
+#include <linux/mutex.h>
+#include <linux/proc_fs.h>
+#include <linux/uaccess.h>
+
+MODULE_LICENSE("GPL");
+
+DECLARE_MUTEX(test_mutex);
+static struct completion test_completion;
+
+static char test_buffer[16];
+static size_t test_len;
+
+static int test_open(struct inode* i, struct file *f)
+{
+       return 0;
+}
+
+static ssize_t test_read(struct file *f, char * __user buf,
+                        size_t s, loff_t *o)
+{
+       int r;
+       wait_for_completion(&test_completion);
+       if (down_interruptible(&test_mutex))
+               return -ERESTARTSYS;
+       if (s > test_len)
+               s = test_len;
+       r = copy_to_user(buf, test_buffer, s);
+       up(&test_mutex);
+       if (r)
+               return -EFAULT;
+       *o += s;
+       return s;
+}
+
+static ssize_t test_write(struct file *f, const char * __user buf,
+                         size_t s, loff_t *o)
+{
+       int r;
+       if (s > sizeof(test_buffer))
+               s = sizeof(test_buffer);
+       if (down_interruptible(&test_mutex))
+               return -ERESTARTSYS;
+       r = copy_from_user(test_buffer, buf, s);
+       test_len = s;
+       up(&test_mutex);
+       if (r)
+               return -EFAULT;
+       complete(&test_completion);
+       *o += s;
+       return s;
+}
+
+static const struct file_operations test_fops =
+{
+       .open = test_open,
+       .read = test_read,
+       .write = test_write,
+};
+
+static int test_completion_init(void)
+{
+       init_completion(&test_completion);
+       proc_create("test_completion", 0666, NULL, &test_fops);
+       return 0;
+}
+
+static void test_completion_exit(void)
+{
+       remove_proc_entry("test_completion", NULL);
+}
+
+module_init(test_completion_init);
+module_exit(test_completion_exit);