Use waitqueue instead of completion. master
authorThadeu Lima de Souza Cascardo <cascardo@holoscopio.com>
Fri, 25 Sep 2009 11:06:24 +0000 (08:06 -0300)
committerThadeu Lima de Souza Cascardo <cascardo@holoscopio.com>
Fri, 25 Sep 2009 11:06:24 +0000 (08:06 -0300)
Makefile
test_waitqueue.c [new file with mode: 0644]

index 6801ea2..3a4252f 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -1 +1,2 @@
 obj-m += test_completion.o
+obj-m += test_waitqueue.o
diff --git a/test_waitqueue.c b/test_waitqueue.c
new file mode 100644 (file)
index 0000000..7bff210
--- /dev/null
@@ -0,0 +1,96 @@
+/*
+ *  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/sched.h>
+#include <linux/wait.h>
+#include <linux/mutex.h>
+#include <linux/proc_fs.h>
+#include <linux/uaccess.h>
+
+MODULE_LICENSE("GPL");
+
+DECLARE_MUTEX(test_mutex);
+DECLARE_WAIT_QUEUE_HEAD(test_wait);
+
+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;
+       if (wait_event_interruptible(test_wait, test_len > 0))
+               return -ERESTARTSYS;
+       if (down_interruptible(&test_mutex))
+               return -ERESTARTSYS;
+       if (s > test_len)
+               s = test_len;
+       test_len = 0;
+       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;
+       wake_up(&test_wait);
+       *o += s;
+       return s;
+}
+
+static const struct file_operations test_fops =
+{
+       .open = test_open,
+       .read = test_read,
+       .write = test_write,
+};
+
+static int test_wait_init(void)
+{
+       proc_create("test_wait", 0666, NULL, &test_fops);
+       return 0;
+}
+
+static void test_wait_exit(void)
+{
+       remove_proc_entry("test_wait", NULL);
+}
+
+module_init(test_wait_init);
+module_exit(test_wait_exit);