diff options
author | patacongo <patacongo@42af7a65-404d-4744-a932-0658087f49c3> | 2008-11-17 21:18:03 +0000 |
---|---|---|
committer | patacongo <patacongo@42af7a65-404d-4744-a932-0658087f49c3> | 2008-11-17 21:18:03 +0000 |
commit | 5c3162da749c8c8c1609c1ab1c85ab1961dbabde (patch) | |
tree | 881e6e8ab9eca25b605c21d34ed9ae32b8b8ac1d /nuttx | |
parent | d4919d58ed21910c65c716ccb6a72f8a6d7db272 (diff) | |
download | px4-nuttx-5c3162da749c8c8c1609c1ab1c85ab1961dbabde.tar.gz px4-nuttx-5c3162da749c8c8c1609c1ab1c85ab1961dbabde.tar.bz2 px4-nuttx-5c3162da749c8c8c1609c1ab1c85ab1961dbabde.zip |
Extend test to verify select
git-svn-id: svn://svn.code.sf.net/p/nuttx/code/trunk@1267 42af7a65-404d-4744-a932-0658087f49c3
Diffstat (limited to 'nuttx')
-rw-r--r-- | nuttx/examples/poll/Makefile | 2 | ||||
-rw-r--r-- | nuttx/examples/poll/poll_internal.h | 9 | ||||
-rw-r--r-- | nuttx/examples/poll/poll_listener.c | 18 | ||||
-rw-r--r-- | nuttx/examples/poll/poll_main.c | 60 | ||||
-rw-r--r-- | nuttx/examples/poll/select_listener.c | 193 | ||||
-rw-r--r-- | nuttx/fs/fs_select.c | 6 |
6 files changed, 258 insertions, 30 deletions
diff --git a/nuttx/examples/poll/Makefile b/nuttx/examples/poll/Makefile index e01f010b5..a85a118aa 100644 --- a/nuttx/examples/poll/Makefile +++ b/nuttx/examples/poll/Makefile @@ -38,7 +38,7 @@ ASRCS = AOBJS = $(ASRCS:.S=$(OBJEXT)) -CSRCS = poll_main.c poll_listener.c +CSRCS = poll_main.c poll_listener.c select_listener.c COBJS = $(CSRCS:.c=$(OBJEXT)) SRCS = $(ASRCS) $(CSRCS) diff --git a/nuttx/examples/poll/poll_internal.h b/nuttx/examples/poll/poll_internal.h index 3ac6e50a6..61b22b870 100644 --- a/nuttx/examples/poll/poll_internal.h +++ b/nuttx/examples/poll/poll_internal.h @@ -50,10 +50,12 @@ * Definitions ****************************************************************************/ -#define FIFO_PATH "/dev/fifo0" +#define FIFO_PATH1 "/dev/fifo0" +#define FIFO_PATH2 "/dev/fifo1" -#define LISTENER_DELAY 2000 /* 2 seconds */ -#define WRITER_DELAY 4 /* 4 seconds */ +#define POLL_LISTENER_DELAY 2000 /* 2 seconds */ +#define SELECT_LISTENER_DELAY 4 /* 4 seconds */ +#define WRITER_DELAY 6 /* 6 seconds */ #ifdef CONFIG_DISABLE_POLL # error "The polling API is disabled" @@ -84,5 +86,6 @@ ****************************************************************************/ extern void *poll_listener(pthread_addr_t pvarg); +extern void *select_listener(pthread_addr_t pvarg); #endif /* __EXAMPLES_PIPE_PIPE_H */ diff --git a/nuttx/examples/poll/poll_listener.c b/nuttx/examples/poll/poll_listener.c index 6f3db89bb..6182ca0de 100644 --- a/nuttx/examples/poll/poll_listener.c +++ b/nuttx/examples/poll/poll_listener.c @@ -89,12 +89,12 @@ void *poll_listener(pthread_addr_t pvarg) /* Open the FIFO for non-blocking read */ - message("poll_listener: Opening %s for non-blocking read\n", FIFO_PATH); - fd = open(FIFO_PATH, O_RDONLY|O_NONBLOCK); + message("poll_listener: Opening %s for non-blocking read\n", FIFO_PATH1); + fd = open(FIFO_PATH1, O_RDONLY|O_NONBLOCK); if (fd < 0) { message("poll_listener: ERROR Failed to open FIFO %s: %d\n", - FIFO_PATH, errno); + FIFO_PATH1, errno); (void)close(fd); return (void*)-1; } @@ -113,10 +113,12 @@ void *poll_listener(pthread_addr_t pvarg) timeout = FALSE; pollin = FALSE; - ret = poll(&fds, 1, LISTENER_DELAY); + ret = poll(&fds, 1, POLL_LISTENER_DELAY); + + message("\npoll_listener: poll returned: %d\n", ret); if (ret < 0) { - message("poll_listener: ERROR poll failed: %d\n"); + message("poll_listener: ERROR poll failed: %d\n", errno); } else if (ret == 0) { @@ -157,11 +159,7 @@ void *poll_listener(pthread_addr_t pvarg) { if (nbytes == 0 || errno == EAGAIN) { - if (timeout) - { - message("poll_listener: No read data available\n"); - } - else if (pollin) + if (pollin) { message("poll_listener: ERROR no read data\n"); } diff --git a/nuttx/examples/poll/poll_main.c b/nuttx/examples/poll/poll_main.c index 4e91070b1..1c998a665 100644 --- a/nuttx/examples/poll/poll_main.c +++ b/nuttx/examples/poll/poll_main.c @@ -89,28 +89,46 @@ int user_start(int argc, char *argv[]) { char buffer[64]; ssize_t nbytes; - pthread_t tid; + pthread_t tid1; + pthread_t tid2; int count; - int fd; + int fd1; + int fd2; int ret; - /* Test FIFO logic */ + /* Open FIFOs */ - message("\nuser_start: Creating FIFO %s\n", FIFO_PATH); - ret = mkfifo(FIFO_PATH, 0666); + message("\nuser_start: Creating FIFO %s\n", FIFO_PATH1); + ret = mkfifo(FIFO_PATH1, 0666); if (ret < 0) { message("user_start: mkfifo failed: %d\n", errno); return 1; } - /* Open the FIFO for blocking, write */ + message("\nuser_start: Creating FIFO %s\n", FIFO_PATH2); + ret = mkfifo(FIFO_PATH2, 0666); + if (ret < 0) + { + message("user_start: mkfifo failed: %d\n", errno); + return 2; + } + + /* Open the FIFOs for blocking, write */ + + fd1 = open(FIFO_PATH1, O_WRONLY); + if (fd1 < 0) + { + message("user_start: Failed to open FIFO %s for writing, errno=%d\n", + FIFO_PATH1, errno); + return 2; + } - fd = open(FIFO_PATH, O_WRONLY); - if (fd < 0) + fd2 = open(FIFO_PATH2, O_WRONLY); + if (fd2 < 0) { message("user_start: Failed to open FIFO %s for writing, errno=%d\n", - FIFO_PATH, errno); + FIFO_PATH2, errno); return 2; } @@ -118,13 +136,22 @@ int user_start(int argc, char *argv[]) message("user_start: Starting poll_listener thread\n"); - ret = pthread_create(&tid, NULL, poll_listener, NULL); + ret = pthread_create(&tid1, NULL, poll_listener, NULL); if (ret != 0) { message("user_start: Failed to create poll_listener thread: %d\n", ret); return 3; } + message("user_start: Starting select_listener thread\n"); + + ret = pthread_create(&tid2, NULL, select_listener, NULL); + if (ret != 0) + { + message("user_start: Failed to create select_listener thread: %d\n", ret); + return 3; + } + /* Loop forever */ for (count = 0; ; count++) @@ -134,14 +161,21 @@ int user_start(int argc, char *argv[]) */ sprintf(buffer, "Message %d", count); - nbytes = write(fd, buffer, strlen(buffer)); + nbytes = write(fd1, buffer, strlen(buffer)); + if (nbytes < 0) + { + message("user_start: Write to fd1 failed: %d\n", errno); + return 4; + } + + nbytes = write(fd2, buffer, strlen(buffer)); if (nbytes < 0) { - message("user_start: Write failed: %d\n", errno); + message("user_start: Write fd2 failed: %d\n", errno); return 4; } - message("user_start: Sent '%s' (%d bytes)\n", buffer, nbytes); + message("\nuser_start: Sent '%s' (%d bytes)\n", buffer, nbytes); msgflush(); /* Wait awhile. This delay should be long enough that the diff --git a/nuttx/examples/poll/select_listener.c b/nuttx/examples/poll/select_listener.c new file mode 100644 index 000000000..a9b31465c --- /dev/null +++ b/nuttx/examples/poll/select_listener.c @@ -0,0 +1,193 @@ +/**************************************************************************** + * examples/poll/select_listener.c + * + * Copyright (C) 2008 Gregory Nutt. All rights reserved. + * Author: Gregory Nutt <spudmonkey@racsa.co.cr> + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * 3. Neither the name NuttX nor the names of its contributors may be + * used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS + * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN + * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + * + ****************************************************************************/ + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include <nuttx/config.h> + +#include <sys/types.h> +#include <sys/stat.h> +#include <sys/select.h> + +#include <stdio.h> +#include <unistd.h> +#include <string.h> +#include <pthread.h> +#include <fcntl.h> +#include <errno.h> +#include <debug.h> + +#include "poll_internal.h" + +/**************************************************************************** + * Definitions + ****************************************************************************/ + +/**************************************************************************** + * Private Types + ****************************************************************************/ + +/**************************************************************************** + * Private Data + ****************************************************************************/ + +/**************************************************************************** + * Private Functions + ****************************************************************************/ + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: select_listener + ****************************************************************************/ + +void *select_listener(pthread_addr_t pvarg) +{ + fd_set rfds; + struct timeval tv; + char buffer[64]; + ssize_t nbytes; + boolean timeout; + boolean ready; + int fd; + int ret; + + /* Open the FIFO for non-blocking read */ + + message("select_listener: Opening %s for non-blocking read\n", FIFO_PATH2); + fd = open(FIFO_PATH2, O_RDONLY|O_NONBLOCK); + if (fd < 0) + { + message("select_listener: ERROR Failed to open FIFO %s: %d\n", + FIFO_PATH2, errno); + (void)close(fd); + return (void*)-1; + } + + /* Loop forever */ + + for (;;) + { + message("select_listener: Calling select()\n"); + + FD_ZERO(&rfds); + FD_SET(fd, &rfds); + + tv.tv_sec = SELECT_LISTENER_DELAY; + tv.tv_usec = 0; + + timeout = FALSE; + ready = FALSE; + + ret = select(fd+1, &rfds, NULL, NULL, &tv); + message("\nselect_listener: select returned: %d\n", ret); + + if (ret < 0) + { + message("select_listener: ERROR select failed: %d\n"); + } + else if (ret == 0) + { + message("select_listener: Timeout\n"); + timeout = TRUE; + } + else + { + if (ret != 1) + { + message("select_listener: ERROR poll reported: %d\n"); + } + else + { + ready = TRUE; + } + + if (!FD_ISSET(fd, rfds)) + { + message("select_listener: ERROR fd=%d not in fd_set\n"); + } + } + + /* In any event, read until the pipe is empty */ + + do + { + nbytes = read(fd, buffer, 63); + if (nbytes <= 0) + { + if (nbytes == 0 || errno == EAGAIN) + { + if (ready) + { + message("select_listener: ERROR no read data\n"); + } + } + else if (errno != EINTR) + { + message("select_listener: read failed: %d\n", errno); + } + nbytes = 0; + } + else + { + if (timeout) + { + message("select_listener: ERROR? Poll timeout, but data read\n"); + message(" (might just be a race condition)\n"); + } + + buffer[nbytes] = '\0'; + message("select_listener: Read '%s' (%d bytes)\n", buffer, nbytes); + } + + timeout = FALSE; + ready = FALSE; + } + while (nbytes > 0); + + /* Make sure that everything is displayed */ + + msgflush(); + } + + /* Won't get here */ + + (void)close(fd); + return NULL; +} diff --git a/nuttx/fs/fs_select.c b/nuttx/fs/fs_select.c index ca2ae4027..ad481377a 100644 --- a/nuttx/fs/fs_select.c +++ b/nuttx/fs/fs_select.c @@ -182,7 +182,7 @@ int select(int nfds, FAR fd_set *readfds, FAR fd_set *writefds, { if (readfds) { - if (pollset[fd].revents & POLLIN) + if (pollset[ndx].revents & POLLIN) { FD_SET(pollset[ndx].fd, readfds); ret++; @@ -191,7 +191,7 @@ int select(int nfds, FAR fd_set *readfds, FAR fd_set *writefds, if (writefds) { - if (pollset[fd].revents & POLLOUT) + if (pollset[ndx].revents & POLLOUT) { FD_SET(pollset[ndx].fd, writefds); ret++; @@ -200,7 +200,7 @@ int select(int nfds, FAR fd_set *readfds, FAR fd_set *writefds, if (exceptfds) { - if (pollset[fd].revents & POLLERR) + if (pollset[ndx].revents & POLLERR) { FD_SET(pollset[ndx].fd, exceptfds); ret++; |