Add select()

git-svn-id: svn://svn.code.sf.net/p/nuttx/code/trunk@1263 42af7a65-404d-4744-a932-0658087f49c3
This commit is contained in:
patacongo 2008-11-17 20:25:28 +00:00
parent 68ec2ba098
commit 73c4fa5757
5 changed files with 349 additions and 8 deletions

View File

@ -564,4 +564,7 @@
types.
0.3.19 2008-xx-xx Gregory Nutt <spudmonkey@racsa.co.cr>
* Add the basic infrastructure for support the poll() API
* Add poll() and select() APIs (in the initial check-in, these work only with character devices)
* Add poll() methods to /dev/null, /dev/zero, pipes, and fifos.
* Add examples/poll for testing poll() and select()

View File

@ -8,7 +8,7 @@
<tr align="center" bgcolor="#e4e4e4">
<td>
<h1><big><font color="#3c34ec"><i>NuttX RTOS</i></font></big></h1>
<p>Last Updated: November 16, 2008</p>
<p>Last Updated: November 17, 2008</p>
</td>
</tr>
</table>
@ -1200,7 +1200,9 @@ buildroot-0.1.2 2007-11-06 &lt;spudmonkey@racsa.co.cr&gt
<pre><ul>
nuttx-0.3.19 2008-xx-xx Gregory Nutt &lt;spudmonkey@racsa.co.cr&gt;
* Add the basic infrastructure for support the poll() API
* Add poll() and select() APIs (in the initial check-in, these work only with character devices)
* Add poll() methods to /dev/null, /dev/zero, pipes, and fifos.
* Add examples/poll for testing poll() and select()
pascal-0.1.3 2008-xx-xx Gregory Nutt &lt;spudmonkey@racsa.co.cr&gt;

View File

@ -41,14 +41,14 @@ AOBJS = $(ASRCS:.S=$(OBJEXT))
CSRCS =
ifeq ($(CONFIG_NFILE_DESCRIPTORS),0)
ifneq ($(CONFIG_NSOCKET_DESCRIPTORS),0)
CSRCS += fs_close.c fs_write.c fs_ioctl.c fs_poll.c
CSRCS += fs_close.c fs_write.c fs_ioctl.c fs_poll.c fs_select.c
endif
else
CSRCS += fs_open.c fs_close.c fs_read.c fs_write.c fs_ioctl.c fs_poll.c \
fs_lseek.c fs_dup.c fs_mmap.c fs_opendir.c fs_closedir.c fs_stat.c \
fs_readdir.c fs_readdirr.c fs_seekdir.c fs_telldir.c fs_rewinddir.c \
fs_files.c fs_inode.c fs_inodefind.c fs_inodereserve.c fs_statfs.c \
fs_inoderemove.c fs_registerdriver.c fs_unregisterdriver.c \
fs_select.c fs_lseek.c fs_dup.c fs_mmap.c fs_opendir.c fs_closedir.c \
fs_stat.c fs_readdir.c fs_readdirr.c fs_seekdir.c fs_telldir.c \
fs_rewinddir.c fs_files.c fs_inode.c fs_inodefind.c fs_inodereserve.c \
fs_statfs.c fs_inoderemove.c fs_registerdriver.c fs_unregisterdriver.c \
fs_inodeaddref.c fs_inoderelease.c
CSRCS += fs_registerblockdriver.c fs_unregisterblockdriver.c \
fs_findblockdriver.c fs_openblockdriver.c fs_closeblockdriver.c

217
fs/fs_select.c Normal file
View File

@ -0,0 +1,217 @@
/****************************************************************************
* fs/fs_select.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/select.h>
#include <stdlib.h>
#include <string.h>
#include <poll.h>
#include <time.h>
#include <errno.h>
#include <assert.h>
#include <debug.h>
#include <nuttx/fs.h>
#include "fs_internal.h"
#ifndef CONFIG_DISABLE_POLL
/****************************************************************************
* Definitions
****************************************************************************/
/****************************************************************************
* Private Functions
****************************************************************************/
/****************************************************************************
* Public Functions
****************************************************************************/
/****************************************************************************
* Name: select
*
* Description:
* select() allows a program to monitor multiple file descriptors, waiting
* until one or more of the file descriptors become "ready" for some class
* of I/O operation (e.g., input possible). A file descriptor is
* considered ready if it is possible to perform the corresponding I/O
* operation (e.g., read(2)) without blocking.
*
* NOTE: poll() is the fundamental API for performing such monitoring
* operation under NuttX. select() is provided for compatibility and
* is simply a layer of added logic on top of poll(). As such, select()
* is more wasteful of resources and poll() is the recommend API to be
* used.
*
* Input parameters:
* nfds - the maximum fd number (+1) of any descriptor in any of the
* three sets.
* readfds - the set of descriptions to monitor for read-ready events
* writefds - the set of descriptions to monitor for write-ready events
* exceptfds - the set of descriptions to monitor for error events
* timeout - Return at this time if none of these events of interest
* occur.
*
* Return:
* 0: Timer expired
* >0: The number of bits set in the three sets of descriptors
* -1: An error occurred (errno will be set approrpiately)
*
****************************************************************************/
int select(int nfds, FAR fd_set *readfds, FAR fd_set *writefds,
FAR fd_set *exceptfds, FAR struct timeval *timeout)
{
struct pollfd *pollset;
int fd;
int npfds;
int msec;
int ndx;
int ret;
/* Allocate the descriptor list for poll() */
pollset = (struct pollfd *)zalloc(nfds * sizeof(struct pollfd));
if (!pollset)
{
errno = ENOMEM;
return ERROR;
}
/* Initialize the descriptor list for poll() */
for (fd = 0, npfds = 0; fd < nfds; fd++)
{
int incr = 0;
if (readfds && FD_ISSET(fd, readfds))
{
pollset[npfds].fd = fd;
pollset[npfds].events |= POLLIN;
incr = 1;
}
if (writefds && FD_ISSET(fd, writefds))
{
pollset[npfds].fd = fd;
pollset[npfds].events |= POLLOUT;
incr = 1;
}
if (exceptfds && FD_ISSET(fd, exceptfds))
{
pollset[npfds].fd = fd;
incr = 1;
}
npfds += incr;
}
/* Convert the timeout to milliseconds */
msec = timeout->tv_sec * 1000 + timeout->tv_usec / 1000;
/* Then let poll do all of the real work. */
ret = poll(pollset, npfds, msec);
/* Now set up the return values */
if (readfds)
{
memset(readfds, 0, sizeof(fd_set));
}
if (writefds)
{
memset(writefds, 0, sizeof(fd_set));
}
if (exceptfds)
{
memset(exceptfds, 0, sizeof(fd_set));
}
/* Convert the poll descriptor list back into selects 3 bitsets */
if (ret > 0)
{
ret = 0;
for (ndx = 0; ndx < npfds; ndx++)
{
if (readfds)
{
if (pollset[fd].revents & POLLIN)
{
FD_SET(pollset[ndx].fd, readfds);
ret++;
}
}
if (writefds)
{
if (pollset[fd].revents & POLLOUT)
{
FD_SET(pollset[ndx].fd, writefds);
ret++;
}
}
if (exceptfds)
{
if (pollset[fd].revents & POLLERR)
{
FD_SET(pollset[ndx].fd, exceptfds);
ret++;
}
}
}
}
free(pollset);
return ret;
}
#endif /* CONFIG_DISABLE_POLL */

119
include/sys/select.h Normal file
View File

@ -0,0 +1,119 @@
/****************************************************************************
* include/sys/select.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 __INCLUDE_SYS_SELECT_H
#define __INCLUDE_SYS_SELECT_H
/****************************************************************************
* Included Files
****************************************************************************/
#include <nuttx/config.h>
#include <time.h>
#if CONFIG_NFILE_DESCRIPTORS > 0 || CONFIG_NSOCKET_DESCRIPTORS > 0
/****************************************************************************
* Pre-Processor Definitions
****************************************************************************/
/* Get the total number of descriptors that we will have to support */
#define __SELECT_NDESCRIPTORS (CONFIG_NFILE_DESCRIPTORS + CONFIG_NSOCKET_DESCRIPTORS)
/* We will use a 32-bit bitsets to represent the set of descriptors. How
* many uint32's do we need to span all descriptors?
*/
#if __SELECT_NDESCRIPTORS <= 32
# define __SELECT_NUINT32 1
#elif __SELECT_NDESCRIPTORS <= 64
# define __SELECT_NUINT32 2
#elif __SELECT_NDESCRIPTORS <= 96
# define __SELECT_NUINT32 3
#elif __SELECT_NDESCRIPTORS <= 128
# define __SELECT_NUINT32 4
#elif __SELECT_NDESCRIPTORS <= 160
# define __SELECT_NUINT32 5
#elif __SELECT_NDESCRIPTORS <= 192
# define __SELECT_NUINT32 6
#elif __SELECT_NDESCRIPTORS <= 224
# define __SELECT_NUINT32 7
#elif __SELECT_NDESCRIPTORS <= 256
# define __SELECT_NUINT32 8
#else
# warning "Large fd_set needed"
#endif
/* These macros map a file descripto to an index and bit number */
#define _FD_NDX(fd) ((fd) >> 5)
#define _FD_BIT(fd) ((fd) & 0x1f)
/* Standard helper macros */
#define FD_CLR(fd,set) (((uint32*)(set))[_FD_NDX(fd)] &= ~(1 << _FD_BIT(fd)))
#define FD_SET(fd,set) (((uint32*)(set))[_FD_NDX(fd)] |= (1 << _FD_BIT(fd)))
#define FD_ISSET(fd,set) ((((uint32*)(set))[_FD_NDX(fd)] & (1 << _FD_BIT(fd))) != 0)
#define FD_ZERO(set) memset(set, 0, sizeof(fd_set))
/****************************************************************************
* Type Definitions
****************************************************************************/
typedef uint32 fd_set[__SELECT_NUINT32];
/****************************************************************************
* Public Function Prototypes
****************************************************************************/
#undef EXTERN
#if defined(__cplusplus)
#define EXTERN extern "C"
extern "C" {
#else
#define EXTERN extern
#endif
EXTERN int select(int nfds, FAR fd_set *readfds, FAR fd_set *writefds,
FAR fd_set *exceptfds, FAR struct timeval *timeout);
#undef EXTERN
#if defined(__cplusplus)
}
#endif
#endif /* CONFIG_NFILE_DESCRIPTORS || CONFIG_NSOCKET_DESCRIPTORS */
#endif /* __INCLUDE_SYS_SELECT_H */