poll.cpp revision 625fbb1328649ec29bf92374a6a980cdcd163726
/* $Id$ */
/** @file
* IPRT - Polling I/O Handles, Windows+Posix Implementation.
*/
/*
* Copyright (C) 2010-2013 Oracle Corporation
*
* This file is part of VirtualBox Open Source Edition (OSE), as
* available from http://www.virtualbox.org. This file is free software;
* General Public License (GPL) as published by the Free Software
* Foundation, in version 2 as it comes in the "COPYING" file of the
* VirtualBox OSE distribution. VirtualBox OSE is distributed in the
* hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
*
* The contents of this file may alternatively be used under the terms
* of the Common Development and Distribution License Version 1.0
* (CDDL) only, as it comes in the "COPYING.CDDL" file of the
* VirtualBox OSE distribution, in which case the provisions of the
* CDDL are applicable instead of those of the GPL.
*
* You may elect to license modified versions of this file under the
* terms and conditions of either the GPL or the CDDL or both.
*/
/*******************************************************************************
* Header Files *
*******************************************************************************/
#ifdef RT_OS_WINDOWS
# include <Windows.h>
# define INCL_BASE
# include <os2.h>
# include <limits.h>
#else
# include <limits.h>
# include <errno.h>
#endif
/*******************************************************************************
* Defined Constants And Macros *
*******************************************************************************/
/** The maximum poll set size.
* @remarks To help portability, we set this to the Windows limit. We can lift
* this restriction later if it becomes necessary. */
#define RTPOLL_SET_MAX 64
/*******************************************************************************
* Structures and Typedefs *
*******************************************************************************/
/**
* Handle entry in a poll set.
*/
typedef struct RTPOLLSETHNDENT
{
/** The handle type. */
/** The handle ID. */
/** The events we're waiting for here. */
/** Set if this is the final entry for this handle.
* If the handle is entered more than once, this will be clear for all but
* the last entry. */
bool fFinalEntry;
/** The handle union. */
/** Pointer to a handle entry. */
typedef RTPOLLSETHNDENT *PRTPOLLSETHNDENT;
/**
* Poll set data.
*/
typedef struct RTPOLLSETINTERNAL
{
/** The magic value (RTPOLLSET_MAGIC). */
/** Set when someone is polling or making changes. */
bool volatile fBusy;
/** The number of allocated handles. */
/** The number of valid handles in the set. */
#ifdef RT_OS_WINDOWS
/** Pointer to an array of native handles. */
/** The semaphore records. */
/** The multiple wait semaphore used for non-socket waits. */
/** os2_select template. */
int *pafdSelect;
/** The number of sockets to monitor for read. */
/** The number of sockets to monitor for write. */
/** The number of sockets to monitor for exceptions. */
/** The number of pipes. */
/** Pointer to an array of native handles. */
#else
/** Pointer to an array of pollfd structures. */
#endif
/** Pointer to an array of handles and IDs. */
/**
* Common worker for RTPoll and RTPollNoResume
*/
{
int rc;
return VERR_DEADLOCK;
/*
* Check for special case, RTThreadSleep...
*/
if (cHandles == 0)
{
if (RT_SUCCESS(rc))
rc = VERR_TIMEOUT;
return rc;
}
#if defined(RT_OS_WINDOWS) || defined(RT_OS_OS2)
/*
* Check + prepare the handles before waiting.
*/
uint32_t i;
for (i = 0; i < cHandles; i++)
{
{
case RTHANDLETYPE_PIPE:
break;
case RTHANDLETYPE_SOCKET:
break;
default:
AssertFailed();
break;
}
if (fEvents)
break;
}
if ( fEvents
|| fNoWait)
{
if (pid)
if (pfEvents)
: fEvents != UINT32_MAX
/* clean up */
if (!fNoWait)
while (i-- > 0)
{
{
case RTHANDLETYPE_PIPE:
break;
case RTHANDLETYPE_SOCKET:
break;
default:
AssertFailed();
break;
}
}
return rc;
}
/*
* Wait.
*/
# ifdef RT_OS_WINDOWS
FALSE /*fWaitAll */,
TRUE /*fAlertable*/);
if ( dwRc >= WAIT_OBJECT_0
else if (dwRc == WAIT_TIMEOUT)
rc = VERR_TIMEOUT;
else if (dwRc == WAIT_IO_COMPLETION)
else if (dwRc == WAIT_FAILED)
else
{
}
# else /* RT_OS_OS2 */
if (cSockets == 0)
{
/* Only pipes. */
&ulUser);
}
else
{
{
/* Only sockets. */
if (rc > 0)
rc = VINF_SUCCESS;
else if (rc == 0)
rc = VERR_TIMEOUT;
else
}
else
{
/* Mix of both - taking the easy way out, not optimal, but whatever... */
do
{
{
break;
}
if (rc != 0)
{
if (rc > 0)
rc = VINF_SUCCESS;
else
break;
}
}
}
# endif /* RT_OS_OS2 */
/*
* Get event (if pending) and do wait cleanup.
*/
bool fHarvestEvents = true;
for (i = 0; i < cHandles; i++)
{
fEvents = 0;
{
case RTHANDLETYPE_PIPE:
break;
case RTHANDLETYPE_SOCKET:
break;
default:
AssertFailed();
break;
}
if ( fEvents
&& fHarvestEvents)
{
fHarvestEvents = false;
if (pfEvents)
if (pid)
rc = VINF_SUCCESS;
}
}
#else /* POSIX */
/* clear the revents. */
while (i-- > 0)
? -1
: (int)cMillies);
if (rc == 0)
return VERR_TIMEOUT;
if (rc < 0)
return RTErrConvertFromErrno(errno);
{
if (pfEvents)
{
*pfEvents = 0;
# ifdef POLLRDNORM
| POLLRDNORM /* just in case */
# endif
# ifdef POLLRDBAND
| POLLRDBAND /* ditto */
# endif
# ifdef POLLPRI
| POLLPRI /* ditto */
# endif
# ifdef POLLMSG
| POLLMSG /* ditto */
# endif
# ifdef POLLWRITE
| POLLWRITE /* ditto */
# endif
# ifdef POLLEXTEND
| POLLEXTEND /* ditto */
# endif
)
)
*pfEvents |= RTPOLL_EVT_READ;
# ifdef POLLWRNORM
| POLLWRNORM /* just in case */
# endif
# ifdef POLLWRBAND
| POLLWRBAND /* ditto */
# endif
)
)
*pfEvents |= RTPOLL_EVT_WRITE;
# ifdef POLLRDHUP
# endif
)
)
*pfEvents |= RTPOLL_EVT_ERROR;
}
if (pid)
return VINF_SUCCESS;
}
AssertFailed();
#endif /* POSIX */
return rc;
}
{
/*
* Set the busy flag and do the job.
*/
int rc;
{
while (rc == VERR_INTERRUPTED);
}
else
{
{
{
rc = VERR_TIMEOUT;
break;
}
}
}
return rc;
}
RTDECL(int) RTPollNoResume(RTPOLLSET hPollSet, RTMSINTERVAL cMillies, uint32_t *pfEvents, uint32_t *pid)
{
/*
* Set the busy flag and do the job.
*/
int rc;
else
return rc;
}
{
if (!pThis)
return VERR_NO_MEMORY;
pThis->cHandlesAllocated = 0;
#ifdef RT_OS_WINDOWS
{
return RTErrConvertFromOS2(orc);
}
pThis->cReadSockets = 0;
pThis->cWriteSockets = 0;
pThis->cXcptSockets = 0;
#else
#endif
return VINF_SUCCESS;
}
{
if (pThis == NIL_RTPOLLSET)
return VINF_SUCCESS;
#ifdef RT_OS_WINDOWS
#else
#endif
return VINF_SUCCESS;
}
#ifdef RT_OS_OS2
/**
* Checks if @a fd is in the specific socket subset.
*
* @returns true / false.
* @param pThis The poll set instance.
* @param iStart The index to start at.
* @param cFds The number of sockets to check.
* @param fd The socket to look for.
*/
static bool rtPollSetOs2IsSocketInSet(RTPOLLSETINTERNAL *pThis, uint16_t iStart, uint16_t cFds, int fd)
{
while (cFds-- > 0)
{
return true;
pfd++;
}
return false;
}
/**
* Removes a socket from a select template subset.
*
* @param pThis The poll set instance.
* @param iStart The index to start at.
* @param pcSubSet The subset counter to decrement.
* @param fd The socket to remove.
*/
static void rtPollSetOs2RemoveSocket(RTPOLLSETINTERNAL *pThis, uint16_t iStart, uint16_t *pcFds, int fd)
{
while (cFds-- > 0)
{
break;
iStart++;
}
/* Note! We keep a -1 entry at the end of the set, thus the + 1. */
*pcFds -= 1;
}
/**
* Adds a socket to a select template subset.
*
* @param pThis The poll set instance.
* @param iInsert The insertion point.
* ASSUMED to be at the end of the subset.
* @param pcSubSet The subset counter to increment.
* @param fd The socket to add.
*/
static void rtPollSetOs2AddSocket(RTPOLLSETINTERNAL *pThis, uint16_t iInsert, uint16_t *pcFds, int fd)
{
/* Note! We keep a -1 entry at the end of the set, thus the + 1. */
*pcFds += 1;
}
/**
* OS/2 specific RTPollSetAdd worker.
*
* @returns IPRT status code.
* @param pThis The poll set instance.
* @param i The index of the new handle (not committed).
* @param fEvents The events to poll for.
*/
{
{
if ( (fEvents & RTPOLL_EVT_READ)
if ( (fEvents & RTPOLL_EVT_WRITE)
rtPollSetOs2AddSocket(pThis, pThis->cReadSockets + pThis->cWriteSockets, &pThis->cWriteSockets, fdSocket);
if ( (fEvents & RTPOLL_EVT_ERROR)
&& rtPollSetOs2IsSocketInSet(pThis, pThis->cReadSockets + pThis->cWriteSockets, pThis->cXcptSockets, fdSocket))
}
{
return RTErrConvertFromOS2(orc);
}
else
return VINF_SUCCESS;
}
#endif /* RT_OS_OS2 */
/**
* Grows the poll set.
*
* @returns VINF_SUCCESS or VERR_NO_MEMORY.
* @param pThis The poll set instance.
* @param cHandlesNew The new poll set size.
*/
{
/* The common array. */
if (!pvNew)
return VERR_NO_MEMORY;
/* OS specific handles */
#if defined(RT_OS_WINDOWS)
if (!pvNew)
return VERR_NO_MEMORY;
if (!pvNew)
return VERR_NO_MEMORY;
if (!pvNew)
return VERR_NO_MEMORY;
if (pThis->cHandlesAllocated == 0)
#else
if (!pvNew)
return VERR_NO_MEMORY;
#endif
return VINF_SUCCESS;
}
{
/*
* Validate the input (tedious).
*/
if (!pHandle)
return VINF_SUCCESS;
AssertReturn(pHandle->enmType > RTHANDLETYPE_INVALID && pHandle->enmType < RTHANDLETYPE_END, VERR_INVALID_PARAMETER);
/*
* Set the busy flag and do the job.
*/
int rc = VINF_SUCCESS;
{
case RTHANDLETYPE_PIPE:
return VINF_SUCCESS;
break;
case RTHANDLETYPE_SOCKET:
return VINF_SUCCESS;
break;
case RTHANDLETYPE_FILE:
AssertMsgFailed(("Files are always ready for reading/writing and thus not pollable. Use native APIs for special devices.\n"));
break;
case RTHANDLETYPE_THREAD:
AssertMsgFailed(("Thread handles are currently not pollable\n"));
break;
default:
AssertMsgFailed(("\n"));
break;
}
if (RT_SUCCESS(rc))
{
/* Check that the handle ID doesn't exist already. */
uint32_t j = i;
while (j-- > 0)
{
{
break;
}
iPrev = j;
}
/* Check that we won't overflow the poll set now. */
if ( RT_SUCCESS(rc)
&& i + 1 > RTPOLL_SET_MAX)
/* Grow the tables if necessary. */
if (RT_SUCCESS(rc))
{
/*
* Add the handles to the two parallel arrays.
*/
#ifdef RT_OS_WINDOWS
#else
if (fEvents & RTPOLL_EVT_READ)
if (fEvents & RTPOLL_EVT_WRITE)
if (fEvents & RTPOLL_EVT_ERROR)
#endif
if (iPrev != UINT32_MAX)
{
}
/*
* Validations and OS specific updates.
*/
#ifdef RT_OS_WINDOWS
/* none */
#else /* POSIX */
{
}
#endif /* POSIX */
if (RT_SUCCESS(rc))
{
/*
* Commit it to the set.
*/
rc = VINF_SUCCESS;
}
}
}
return rc;
}
{
/*
* Validate the input.
*/
/*
* Set the busy flag and do the job.
*/
int rc = VERR_POLL_HANDLE_ID_NOT_FOUND;
while (i-- > 0)
{
/* Save some details for the duplicate searching. */
#ifdef RT_OS_OS2
#endif
/* Remove the entry. */
if (cToMove)
{
#if defined(RT_OS_WINDOWS) || defined(RT_OS_OS2)
#else
#endif
}
/* Check for duplicate and set the fFinalEntry flag. */
if (fFinalEntry)
while (i-- > 0)
{
break;
}
#ifdef RT_OS_OS2
/*
* Update OS/2 wait structures.
*/
uint32_t fNewEvents = 0;
while (i-- > 0)
if (enmType == RTHANDLETYPE_PIPE)
{
if (fNewEvents == 0)
{
}
}
&& enmType == RTHANDLETYPE_SOCKET)
{
if (fRemovedEvents & RTPOLL_EVT_ERROR)
rtPollSetOs2RemoveSocket(pThis, pThis->cReadSockets + pThis->cWriteSockets, &pThis->cXcptSockets, (int)hNative);
if (fRemovedEvents & RTPOLL_EVT_WRITE)
if (fRemovedEvents & RTPOLL_EVT_READ)
}
#endif /* RT_OS_OS2 */
rc = VINF_SUCCESS;
break;
}
return rc;
}
{
/*
* Validate the input.
*/
/*
* Set the busy flag and do the job.
*/
int rc = VERR_POLL_HANDLE_ID_NOT_FOUND;
while (i-- > 0)
{
if (pHandle)
{
}
rc = VINF_SUCCESS;
break;
}
return rc;
}
{
/*
* Validate the input.
*/
/*
* Set the busy flag and do the job.
*/
return cHandles;
}
{
/*
* Validate the input.
*/
/*
* Set the busy flag and do the job.
*/
int rc = VERR_POLL_HANDLE_ID_NOT_FOUND;
while (i-- > 0)
{
{
#if defined(RT_OS_WINDOWS)
/*nothing*/
{
uint32_t fOldEvents = 0;
while (j-- > 0)
&& j != i)
if (fOldEvents != fEvents)
{
else if (fChangedEvents & RTPOLL_EVT_READ)
else if (fChangedEvents & RTPOLL_EVT_WRITE)
else if (fChangedEvents & RTPOLL_EVT_ERROR)
fdSocket);
}
}
#else
if (fEvents & RTPOLL_EVT_READ)
if (fEvents & RTPOLL_EVT_WRITE)
if (fEvents & RTPOLL_EVT_ERROR)
#endif
}
rc = VINF_SUCCESS;
break;
}
return rc;
}