Extend test to verify select
git-svn-id: https://nuttx.svn.sourceforge.net/svnroot/nuttx/trunk@1267 7fd9a85b-ad96-42d3-883c-3090e2eb8679
This commit is contained in:
parent
1590d31704
commit
8b3c715439
|
@ -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)
|
||||
|
|
|
@ -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 */
|
||||
|
|
|
@ -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");
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
||||
fd = open(FIFO_PATH, O_WRONLY);
|
||||
if (fd < 0)
|
||||
/* 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_PATH, errno);
|
||||
FIFO_PATH1, errno);
|
||||
return 2;
|
||||
}
|
||||
|
||||
fd2 = open(FIFO_PATH2, O_WRONLY);
|
||||
if (fd2 < 0)
|
||||
{
|
||||
message("user_start: Failed to open FIFO %s for writing, errno=%d\n",
|
||||
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 failed: %d\n", errno);
|
||||
message("user_start: Write to fd1 failed: %d\n", errno);
|
||||
return 4;
|
||||
}
|
||||
|
||||
message("user_start: Sent '%s' (%d bytes)\n", buffer, nbytes);
|
||||
nbytes = write(fd2, buffer, strlen(buffer));
|
||||
if (nbytes < 0)
|
||||
{
|
||||
message("user_start: Write fd2 failed: %d\n", errno);
|
||||
return 4;
|
||||
}
|
||||
|
||||
message("\nuser_start: Sent '%s' (%d bytes)\n", buffer, nbytes);
|
||||
msgflush();
|
||||
|
||||
/* Wait awhile. This delay should be long enough that the
|
||||
|
|
|
@ -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;
|
||||
}
|
|
@ -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++;
|
||||
|
|
Loading…
Reference in New Issue