Path: blob/master/thirdparty/sdl/core/unix/SDL_poll.c
9905 views
/*1Simple DirectMedia Layer2Copyright (C) 1997-2025 Sam Lantinga <[email protected]>34This software is provided 'as-is', without any express or implied5warranty. In no event will the authors be held liable for any damages6arising from the use of this software.78Permission is granted to anyone to use this software for any purpose,9including commercial applications, and to alter it and redistribute it10freely, subject to the following restrictions:11121. The origin of this software must not be misrepresented; you must not13claim that you wrote the original software. If you use this software14in a product, an acknowledgment in the product documentation would be15appreciated but is not required.162. Altered source versions must be plainly marked as such, and must not be17misrepresented as being the original software.183. This notice may not be removed or altered from any source distribution.19*/2021#include "SDL_internal.h"2223#include "SDL_poll.h"2425#ifdef HAVE_POLL26#include <poll.h>27#else28#include <sys/time.h>29#include <sys/types.h>30#include <unistd.h>31#endif32#include <errno.h>3334int SDL_IOReady(int fd, int flags, Sint64 timeoutNS)35{36int result;3738SDL_assert(flags & (SDL_IOR_READ | SDL_IOR_WRITE));3940// Note: We don't bother to account for elapsed time if we get EINTR41do {42#ifdef HAVE_POLL43struct pollfd info;44int timeoutMS;4546info.fd = fd;47info.events = 0;48if (flags & SDL_IOR_READ) {49info.events |= POLLIN | POLLPRI;50}51if (flags & SDL_IOR_WRITE) {52info.events |= POLLOUT;53}54// FIXME: Add support for ppoll() for nanosecond precision55if (timeoutNS > 0) {56timeoutMS = (int)SDL_NS_TO_MS(timeoutNS + (SDL_NS_PER_MS - 1));57} else if (timeoutNS == 0) {58timeoutMS = 0;59} else {60timeoutMS = -1;61}62result = poll(&info, 1, timeoutMS);63#else64fd_set rfdset, *rfdp = NULL;65fd_set wfdset, *wfdp = NULL;66struct timeval tv, *tvp = NULL;6768// If this assert triggers we'll corrupt memory here69SDL_assert(fd >= 0 && fd < FD_SETSIZE);7071if (flags & SDL_IOR_READ) {72FD_ZERO(&rfdset);73FD_SET(fd, &rfdset);74rfdp = &rfdset;75}76if (flags & SDL_IOR_WRITE) {77FD_ZERO(&wfdset);78FD_SET(fd, &wfdset);79wfdp = &wfdset;80}8182if (timeoutNS >= 0) {83tv.tv_sec = (timeoutNS / SDL_NS_PER_SECOND);84tv.tv_usec = SDL_NS_TO_US((timeoutNS % SDL_NS_PER_SECOND) + (SDL_NS_PER_US - 1));85tvp = &tv;86}8788result = select(fd + 1, rfdp, wfdp, NULL, tvp);89#endif // HAVE_POLL9091} while (result < 0 && errno == EINTR && !(flags & SDL_IOR_NO_RETRY));9293return result;94}959697