From: Thadeu Lima de Souza Cascardo Date: Fri, 25 Sep 2009 10:39:08 +0000 (-0300) Subject: First test of use of completion. X-Git-Url: http://git.cascardo.eti.br/?p=cascardo%2Fkernel%2Fsamples%2Fwaitqueue%2F.git;a=commitdiff_plain;h=873353a2dc0163a01a42ac4560aa0cb5ebdff825 First test of use of completion. --- 873353a2dc0163a01a42ac4560aa0cb5ebdff825 diff --git a/Makefile b/Makefile new file mode 100644 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 index 0000000..4e0fa70 --- /dev/null +++ b/test_completion.c @@ -0,0 +1,94 @@ +/* + * 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 + +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);