| 1 | /* |
| 2 | Simple DirectMedia Layer |
| 3 | Copyright (C) 1997-2025 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 | #include "SDL_internal.h" |
| 22 | |
| 23 | #include <errno.h> |
| 24 | #include <pthread.h> |
| 25 | #include <semaphore.h> |
| 26 | #include <sys/time.h> |
| 27 | #include <time.h> |
| 28 | |
| 29 | // Wrapper around POSIX 1003.1b semaphores |
| 30 | |
| 31 | #if defined(SDL_PLATFORM_MACOS) || defined(SDL_PLATFORM_IOS) |
| 32 | // macOS doesn't support sem_getvalue() as of version 10.4 |
| 33 | #include "../generic/SDL_syssem.c" |
| 34 | #else |
| 35 | |
| 36 | struct SDL_Semaphore |
| 37 | { |
| 38 | sem_t sem; |
| 39 | }; |
| 40 | |
| 41 | // Create a semaphore, initialized with value |
| 42 | SDL_Semaphore *SDL_CreateSemaphore(Uint32 initial_value) |
| 43 | { |
| 44 | SDL_Semaphore *sem = (SDL_Semaphore *)SDL_malloc(sizeof(SDL_Semaphore)); |
| 45 | if (sem) { |
| 46 | if (sem_init(&sem->sem, 0, initial_value) < 0) { |
| 47 | SDL_SetError("sem_init() failed" ); |
| 48 | SDL_free(sem); |
| 49 | sem = NULL; |
| 50 | } |
| 51 | } |
| 52 | return sem; |
| 53 | } |
| 54 | |
| 55 | void SDL_DestroySemaphore(SDL_Semaphore *sem) |
| 56 | { |
| 57 | if (sem) { |
| 58 | sem_destroy(&sem->sem); |
| 59 | SDL_free(sem); |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | bool SDL_WaitSemaphoreTimeoutNS(SDL_Semaphore *sem, Sint64 timeoutNS) |
| 64 | { |
| 65 | #ifdef HAVE_SEM_TIMEDWAIT |
| 66 | #ifndef HAVE_CLOCK_GETTIME |
| 67 | struct timeval now; |
| 68 | #endif |
| 69 | struct timespec ts_timeout; |
| 70 | #else |
| 71 | Uint64 stop_time; |
| 72 | #endif |
| 73 | |
| 74 | if (!sem) { |
| 75 | return true; |
| 76 | } |
| 77 | |
| 78 | // Try the easy cases first |
| 79 | if (timeoutNS == 0) { |
| 80 | return (sem_trywait(&sem->sem) == 0); |
| 81 | } |
| 82 | |
| 83 | if (timeoutNS < 0) { |
| 84 | int rc; |
| 85 | do { |
| 86 | rc = sem_wait(&sem->sem); |
| 87 | } while (rc < 0 && errno == EINTR); |
| 88 | |
| 89 | return (rc == 0); |
| 90 | } |
| 91 | |
| 92 | #ifdef HAVE_SEM_TIMEDWAIT |
| 93 | /* Setup the timeout. sem_timedwait doesn't wait for |
| 94 | * a lapse of time, but until we reach a certain time. |
| 95 | * This time is now plus the timeout. |
| 96 | */ |
| 97 | #ifdef HAVE_CLOCK_GETTIME |
| 98 | clock_gettime(CLOCK_REALTIME, &ts_timeout); |
| 99 | |
| 100 | // Add our timeout to current time |
| 101 | ts_timeout.tv_sec += (timeoutNS / SDL_NS_PER_SECOND); |
| 102 | ts_timeout.tv_nsec += (timeoutNS % SDL_NS_PER_SECOND); |
| 103 | #else |
| 104 | gettimeofday(&now, NULL); |
| 105 | |
| 106 | // Add our timeout to current time |
| 107 | ts_timeout.tv_sec = now.tv_sec + (timeoutNS / SDL_NS_PER_SECOND); |
| 108 | ts_timeout.tv_nsec = SDL_US_TO_NS(now.tv_usec) + (timeoutNS % SDL_NS_PER_SECOND); |
| 109 | #endif |
| 110 | |
| 111 | // Wrap the second if needed |
| 112 | while (ts_timeout.tv_nsec >= 1000000000) { |
| 113 | ts_timeout.tv_sec += 1; |
| 114 | ts_timeout.tv_nsec -= 1000000000; |
| 115 | } |
| 116 | |
| 117 | // Wait. |
| 118 | int rc; |
| 119 | do { |
| 120 | rc = sem_timedwait(&sem->sem, &ts_timeout); |
| 121 | } while (rc < 0 && errno == EINTR); |
| 122 | |
| 123 | return (rc == 0); |
| 124 | #else |
| 125 | stop_time = SDL_GetTicksNS() + timeoutNS; |
| 126 | while (sem_trywait(&sem->sem) != 0) { |
| 127 | if (SDL_GetTicksNS() >= stop_time) { |
| 128 | return false; |
| 129 | } |
| 130 | SDL_DelayNS(100); |
| 131 | } |
| 132 | return true; |
| 133 | #endif // HAVE_SEM_TIMEDWAIT |
| 134 | } |
| 135 | |
| 136 | Uint32 SDL_GetSemaphoreValue(SDL_Semaphore *sem) |
| 137 | { |
| 138 | int ret = 0; |
| 139 | |
| 140 | if (!sem) { |
| 141 | return 0; |
| 142 | } |
| 143 | |
| 144 | sem_getvalue(&sem->sem, &ret); |
| 145 | if (ret < 0) { |
| 146 | ret = 0; |
| 147 | } |
| 148 | return (Uint32)ret; |
| 149 | } |
| 150 | |
| 151 | void SDL_SignalSemaphore(SDL_Semaphore *sem) |
| 152 | { |
| 153 | if (!sem) { |
| 154 | return; |
| 155 | } |
| 156 | |
| 157 | sem_post(&sem->sem); |
| 158 | } |
| 159 | |
| 160 | #endif // SDL_PLATFORM_MACOS |
| 161 | |