src/core/unix/SDL_poll.c
author Sam Lantinga <slouken@libsdl.org>
Mon, 14 Aug 2017 20:25:53 -0700
changeset 11297 905d34b4033c
child 11783 c7577c45151a
permissions -rw-r--r--
Added missing files from the previous commit
     1 /*
     2   Simple DirectMedia Layer
     3   Copyright (C) 1997-2017 Sam Lantinga <slouken@libsdl.org>
     4 
     5   This software is provided 'as-is', without any express or implied
     6   warranty.  In no event will the authors be held liable for any damages
     7   arising from the use of this software.
     8 
     9   Permission is granted to anyone to use this software for any purpose,
    10   including commercial applications, and to alter it and redistribute it
    11   freely, subject to the following restrictions:
    12 
    13   1. The origin of this software must not be misrepresented; you must not
    14      claim that you wrote the original software. If you use this software
    15      in a product, an acknowledgment in the product documentation would be
    16      appreciated but is not required.
    17   2. Altered source versions must be plainly marked as such, and must not be
    18      misrepresented as being the original software.
    19   3. This notice may not be removed or altered from any source distribution.
    20 */
    21 
    22 #include "../../SDL_internal.h"
    23 
    24 #include "SDL_assert.h"
    25 #include "SDL_poll.h"
    26 
    27 #undef HAVE_POLL
    28 #ifdef HAVE_POLL
    29 #include <poll.h>
    30 #else
    31 #include <sys/time.h>
    32 #include <sys/types.h>
    33 #include <unistd.h>
    34 #endif
    35 #include <errno.h>
    36 
    37 
    38 int
    39 SDL_IOReady(int fd, SDL_bool forWrite, int timeoutMS)
    40 {
    41     int result;
    42 
    43     /* Note: We don't bother to account for elapsed time if we get EINTR */
    44     do
    45     {
    46 #ifdef HAVE_POLL
    47         struct pollfd info;
    48 
    49         info.fd = fd;
    50         if (forWrite) {
    51             info.events = POLLOUT;
    52         } else {
    53             info.events = POLLIN | POLLPRI;
    54         }
    55         result = poll(&info, 1, timeoutMS);
    56 #else
    57         fd_set rfdset, *rfdp = NULL;
    58         fd_set wfdset, *wfdp = NULL;
    59         struct timeval tv, *tvp = NULL;
    60 
    61         /* If this assert triggers we'll corrupt memory here */
    62         SDL_assert(fd >= 0 && fd < FD_SETSIZE);
    63 
    64         if (forWrite) {
    65             FD_ZERO(&wfdset);
    66             FD_SET(fd, &wfdset);
    67             wfdp = &wfdset;
    68         } else {
    69             FD_ZERO(&rfdset);
    70             FD_SET(fd, &rfdset);
    71             rfdp = &rfdset;
    72         }
    73 
    74         if (timeoutMS >= 0) {
    75             tv.tv_sec = timeoutMS / 1000;
    76             tv.tv_usec = (timeoutMS % 1000) * 1000;
    77             tvp = &tv;
    78         }
    79 
    80         result = select(fd + 1, rfdp, wfdp, NULL, tvp);
    81 #endif /* HAVE_POLL */
    82 
    83     } while ( result < 0 && errno == EINTR );
    84 
    85     return result;
    86 }
    87 
    88 /* vi: set ts=4 sw=4 expandtab: */