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