• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //========================================================================
2 // GLFW 3.5 POSIX - www.glfw.org
3 //------------------------------------------------------------------------
4 // Copyright (c) 2022 Camilla Löwy <elmindreda@glfw.org>
5 //
6 // This software is provided 'as-is', without any express or implied
7 // warranty. In no event will the authors be held liable for any damages
8 // arising from the use of this software.
9 //
10 // Permission is granted to anyone to use this software for any purpose,
11 // including commercial applications, and to alter it and redistribute it
12 // freely, subject to the following restrictions:
13 //
14 // 1. The origin of this software must not be misrepresented; you must not
15 //    claim that you wrote the original software. If you use this software
16 //    in a product, an acknowledgment in the product documentation would
17 //    be appreciated but is not required.
18 //
19 // 2. Altered source versions must be plainly marked as such, and must not
20 //    be misrepresented as being the original software.
21 //
22 // 3. This notice may not be removed or altered from any source
23 //    distribution.
24 //
25 //========================================================================
26 
27 #if !defined(_GNU_SOURCE)
28 #define _GNU_SOURCE
29 #endif
30 
31 #include "internal.h"
32 
33 #if defined(GLFW_BUILD_POSIX_POLL)
34 
35 #include <signal.h>
36 #include <time.h>
37 #include <errno.h>
38 
_glfwPollPOSIX(struct pollfd * fds,nfds_t count,double * timeout)39 GLFWbool _glfwPollPOSIX(struct pollfd* fds, nfds_t count, double* timeout)
40 {
41     for (;;)
42     {
43         if (timeout)
44         {
45             const uint64_t base = _glfwPlatformGetTimerValue();
46 
47 #if defined(__linux__) || defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__CYGWIN__)
48             const time_t seconds = (time_t) *timeout;
49             const long nanoseconds = (long) ((*timeout - seconds) * 1e9);
50             const struct timespec ts = { seconds, nanoseconds };
51             const int result = ppoll(fds, count, &ts, NULL);
52 #elif defined(__NetBSD__)
53             const time_t seconds = (time_t) *timeout;
54             const long nanoseconds = (long) ((*timeout - seconds) * 1e9);
55             const struct timespec ts = { seconds, nanoseconds };
56             const int result = pollts(fds, count, &ts, NULL);
57 #else
58             const int milliseconds = (int) (*timeout * 1e3);
59             const int result = poll(fds, count, milliseconds);
60 #endif
61             const int error = errno; // clock_gettime may overwrite our error
62 
63             *timeout -= (_glfwPlatformGetTimerValue() - base) /
64                 (double) _glfwPlatformGetTimerFrequency();
65 
66             if (result > 0)
67                 return GLFW_TRUE;
68             else if (result == -1 && error != EINTR && error != EAGAIN)
69                 return GLFW_FALSE;
70             else if (*timeout <= 0.0)
71                 return GLFW_FALSE;
72         }
73         else
74         {
75             const int result = poll(fds, count, -1);
76             if (result > 0)
77                 return GLFW_TRUE;
78             else if (result == -1 && errno != EINTR && errno != EAGAIN)
79                 return GLFW_FALSE;
80         }
81     }
82 }
83 
84 #endif // GLFW_BUILD_POSIX_POLL
85 
86