From 2339b594a5b5a4dd86bd62228239ac7178866e48 Mon Sep 17 00:00:00 2001 From: Thadeu Lima de Souza Cascardo Date: Fri, 25 Sep 2009 08:06:24 -0300 Subject: [PATCH] Use waitqueue instead of completion. --- Makefile | 1 + test_waitqueue.c | 96 ++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 97 insertions(+) create mode 100644 test_waitqueue.c diff --git a/Makefile b/Makefile index 6801ea2..3a4252f 100644 --- 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 index 0000000..7bff210 --- /dev/null +++ b/test_waitqueue.c @@ -0,0 +1,96 @@ +/* + * Copyright (C) 2009 Thadeu Lima de Souza Cascardo + * + * 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 +#include +#include +#include +#include +#include + +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); -- 2.20.1