Test for poll API
git-svn-id: svn://svn.code.sf.net/p/nuttx/code/trunk@1261 42af7a65-404d-4744-a932-0658087f49c3
This commit is contained in:
parent
8840defcc2
commit
efff6fa8d2
75
examples/poll/Makefile
Normal file
75
examples/poll/Makefile
Normal file
@ -0,0 +1,75 @@
|
||||
############################################################################
|
||||
# examples/poll/Makefile
|
||||
#
|
||||
# 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.
|
||||
#
|
||||
############################################################################
|
||||
|
||||
-include $(TOPDIR)/.config
|
||||
-include $(TOPDIR)/Make.defs
|
||||
|
||||
ASRCS =
|
||||
AOBJS = $(ASRCS:.S=$(OBJEXT))
|
||||
CSRCS = poll_main.c poll_listener.c
|
||||
COBJS = $(CSRCS:.c=$(OBJEXT))
|
||||
|
||||
SRCS = $(ASRCS) $(CSRCS)
|
||||
OBJS = $(AOBJS) $(COBJS)
|
||||
|
||||
BIN = lib$(CONFIG_EXAMPLE)$(LIBEXT)
|
||||
|
||||
all: $(BIN)
|
||||
|
||||
$(AOBJS): %$(OBJEXT): %.S
|
||||
$(call ASSEMBLE, $<, $@)
|
||||
|
||||
$(COBJS): %$(OBJEXT): %.c
|
||||
$(call COMPILE, $<, $@)
|
||||
|
||||
$(BIN): $(OBJS)
|
||||
@( for obj in $(OBJS) ; do \
|
||||
$(call ARCHIVE, $@, $${obj}); \
|
||||
done ; )
|
||||
|
||||
.depend: Makefile $(SRCS)
|
||||
@$(MKDEP) $(CC) -- $(CFLAGS) -- $(SRCS) >Make.dep
|
||||
@touch $@
|
||||
|
||||
depend: .depend
|
||||
|
||||
clean:
|
||||
@rm -f $(BIN) *~ .*.swp
|
||||
$(call CLEAN)
|
||||
|
||||
distclean: clean
|
||||
@rm -f Make.dep .depend
|
||||
|
||||
-include Make.dep
|
88
examples/poll/poll_internal.h
Normal file
88
examples/poll/poll_internal.h
Normal file
@ -0,0 +1,88 @@
|
||||
/****************************************************************************
|
||||
* examples/poll/poll.h
|
||||
*
|
||||
* 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.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
#ifndef __EXAMPLES_PIPE_PIPE_H
|
||||
#define __EXAMPLES_PIPE_PIPE_H
|
||||
|
||||
/****************************************************************************
|
||||
* Compilation Switches
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Included Files
|
||||
****************************************************************************/
|
||||
|
||||
#include <pthread.h>
|
||||
|
||||
/****************************************************************************
|
||||
* Definitions
|
||||
****************************************************************************/
|
||||
|
||||
#define FIFO_PATH "/dev/fifo0"
|
||||
|
||||
#define LISTENER_DELAY 2000 /* 2 seconds */
|
||||
#define WRITER_DELAY 4 /* 4 seconds */
|
||||
|
||||
#ifdef CONFIG_DISABLE_POLL
|
||||
# error "The polling API is disabled"
|
||||
#endif
|
||||
|
||||
/* If debug is enabled, then use lib_rawprintf so that OS debug output and
|
||||
* the test output are synchronized.
|
||||
*/
|
||||
|
||||
#ifdef CONFIG_DEBUG
|
||||
# define message(...) lib_rawprintf(__VA_ARGS__)
|
||||
# define msgflush()
|
||||
#else
|
||||
# define message(...) printf(__VA_ARGS__)
|
||||
# define msgflush() fflush(stdout)
|
||||
#endif
|
||||
|
||||
/****************************************************************************
|
||||
* Public Types
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Public Variables
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Public Function Prototypes
|
||||
****************************************************************************/
|
||||
|
||||
extern void *poll_listener(pthread_addr_t pvarg);
|
||||
|
||||
#endif /* __EXAMPLES_PIPE_PIPE_H */
|
201
examples/poll/poll_listener.c
Normal file
201
examples/poll/poll_listener.c
Normal file
@ -0,0 +1,201 @@
|
||||
/****************************************************************************
|
||||
* examples/poll/poll_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 <stdio.h>
|
||||
#include <unistd.h>
|
||||
#include <string.h>
|
||||
#include <pthread.h>
|
||||
#include <poll.h>
|
||||
#include <fcntl.h>
|
||||
#include <errno.h>
|
||||
#include <debug.h>
|
||||
|
||||
#include "poll_internal.h"
|
||||
|
||||
/****************************************************************************
|
||||
* Definitions
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Private Types
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Private Data
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Private Functions
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Public Functions
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Name: poll_listener
|
||||
****************************************************************************/
|
||||
|
||||
void *poll_listener(pthread_addr_t pvarg)
|
||||
{
|
||||
struct pollfd fds;
|
||||
char buffer[64];
|
||||
ssize_t nbytes;
|
||||
boolean timeout;
|
||||
boolean pollin;
|
||||
int fd;
|
||||
int ret;
|
||||
|
||||
/* 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);
|
||||
if (fd < 0)
|
||||
{
|
||||
message("poll_listener: ERROR Failed to open FIFO %s: %d\n",
|
||||
FIFO_PATH, errno);
|
||||
(void)close(fd);
|
||||
return (void*)-1;
|
||||
}
|
||||
|
||||
/* Loop forever */
|
||||
|
||||
for (;;)
|
||||
{
|
||||
message("poll_listener: Calling poll()\n");
|
||||
|
||||
memset(&fds, 0, sizeof(struct pollfd));
|
||||
fds.fd = fd;
|
||||
fds.events = POLLIN;
|
||||
fds.revents = 0;
|
||||
|
||||
timeout = FALSE;
|
||||
pollin = FALSE;
|
||||
|
||||
ret = poll(&fds, 1, LISTENER_DELAY);
|
||||
if (ret < 0)
|
||||
{
|
||||
message("poll_listener: ERROR poll failed: %d\n");
|
||||
}
|
||||
else if (ret == 0)
|
||||
{
|
||||
message("poll_listener: Timeout, revents=%02x\n", fds.revents);
|
||||
timeout = TRUE;
|
||||
if (fds.revents != 0)
|
||||
{
|
||||
message("poll_listener: ERROR? expected revents=00, received revents=%02x\n",
|
||||
fds.revents);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (ret != 1)
|
||||
{
|
||||
message("poll_listener: ERROR poll reported: %d\n");
|
||||
}
|
||||
else
|
||||
{
|
||||
pollin = TRUE;
|
||||
}
|
||||
|
||||
message("poll_listener: revents=%02x\n", fds.revents);
|
||||
if (fds.revents != POLLIN)
|
||||
{
|
||||
message("poll_listener: ERROR expected revents=%02x, received revents=%02x\n",
|
||||
fds.revents);
|
||||
message(" (might just be a race condition)\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 (timeout)
|
||||
{
|
||||
message("poll_listener: No read data available\n");
|
||||
}
|
||||
else if (pollin)
|
||||
{
|
||||
message("poll_listener: ERROR no read data\n");
|
||||
}
|
||||
}
|
||||
else if (errno != EINTR)
|
||||
{
|
||||
message("poll_listener: read failed: %d\n", errno);
|
||||
}
|
||||
nbytes = 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
if (timeout)
|
||||
{
|
||||
message("poll_listener: ERROR? Poll timeout, but data read\n");
|
||||
message(" (might just be a race condition)\n");
|
||||
}
|
||||
|
||||
buffer[nbytes] = '\0';
|
||||
message("poll_listener: Read '%s' (%d bytes)\n", buffer, nbytes);
|
||||
}
|
||||
|
||||
timeout = FALSE;
|
||||
pollin = FALSE;
|
||||
}
|
||||
while (nbytes > 0);
|
||||
|
||||
/* Make sure that everything is displayed */
|
||||
|
||||
msgflush();
|
||||
}
|
||||
|
||||
/* Won't get here */
|
||||
|
||||
(void)close(fd);
|
||||
return NULL;
|
||||
}
|
156
examples/poll/poll_main.c
Normal file
156
examples/poll/poll_main.c
Normal file
@ -0,0 +1,156 @@
|
||||
/****************************************************************************
|
||||
* examples/poll/poll_main.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 <stdio.h>
|
||||
#include <unistd.h>
|
||||
#include <string.h>
|
||||
#include <pthread.h>
|
||||
#include <fcntl.h>
|
||||
#include <poll.h>
|
||||
#include <errno.h>
|
||||
#include <debug.h>
|
||||
|
||||
#include "poll_internal.h"
|
||||
|
||||
/****************************************************************************
|
||||
* Definitions
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Private Types
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Private Data
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Private Functions
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Public Functions
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Name: user_initialize
|
||||
****************************************************************************/
|
||||
|
||||
void user_initialize(void)
|
||||
{
|
||||
}
|
||||
|
||||
/****************************************************************************
|
||||
* Name: user_start
|
||||
****************************************************************************/
|
||||
|
||||
int user_start(int argc, char *argv[])
|
||||
{
|
||||
char buffer[64];
|
||||
ssize_t nbytes;
|
||||
pthread_t tid;
|
||||
int count;
|
||||
int fd;
|
||||
int ret;
|
||||
|
||||
/* Test FIFO logic */
|
||||
|
||||
message("\nuser_start: Creating FIFO %s\n", FIFO_PATH);
|
||||
ret = mkfifo(FIFO_PATH, 0666);
|
||||
if (ret < 0)
|
||||
{
|
||||
message("user_start: mkfifo failed: %d\n", errno);
|
||||
return 1;
|
||||
}
|
||||
|
||||
/* Open the FIFO for blocking, write */
|
||||
|
||||
fd = open(FIFO_PATH, O_WRONLY);
|
||||
if (fd < 0)
|
||||
{
|
||||
message("user_start: Failed to open FIFO %s for writing, errno=%d\n",
|
||||
FIFO_PATH, errno);
|
||||
return 2;
|
||||
}
|
||||
|
||||
/* Start the listener */
|
||||
|
||||
message("user_start: Starting poll_listener thread\n");
|
||||
|
||||
ret = pthread_create(&tid, NULL, poll_listener, NULL);
|
||||
if (ret != 0)
|
||||
{
|
||||
message("user_start: Failed to create poll_listener thread: %d\n", ret);
|
||||
return 3;
|
||||
}
|
||||
|
||||
/* Loop forever */
|
||||
|
||||
for (count = 0; ; count++)
|
||||
{
|
||||
/* Send a message to the listener... this should wake the listener
|
||||
* from the poll.
|
||||
*/
|
||||
|
||||
sprintf(buffer, "Message %d", count);
|
||||
nbytes = write(fd, buffer, strlen(buffer));
|
||||
if (nbytes < 0)
|
||||
{
|
||||
message("user_start: Write failed: %d\n", errno);
|
||||
return 4;
|
||||
}
|
||||
|
||||
message("user_start: Sent '%s' (%d bytes)\n", buffer, nbytes);
|
||||
msgflush();
|
||||
|
||||
/* Wait awhile. This delay should be long enough that the
|
||||
* listener will timeout.
|
||||
*/
|
||||
|
||||
sleep(WRITER_DELAY);
|
||||
}
|
||||
|
||||
fflush(stdout);
|
||||
return 0;
|
||||
}
|
Loading…
Reference in New Issue
Block a user