diff options
| author | 3gg <3gg@shellblade.net> | 2025-12-27 12:03:39 -0800 |
|---|---|---|
| committer | 3gg <3gg@shellblade.net> | 2025-12-27 12:03:39 -0800 |
| commit | 5a079a2d114f96d4847d1ee305d5b7c16eeec50e (patch) | |
| tree | 8926ab44f168acf787d8e19608857b3af0f82758 /contrib/SDL-3.2.8/src/thread/generic/SDL_syssem.c | |
Initial commit
Diffstat (limited to 'contrib/SDL-3.2.8/src/thread/generic/SDL_syssem.c')
| -rw-r--r-- | contrib/SDL-3.2.8/src/thread/generic/SDL_syssem.c | 183 |
1 files changed, 183 insertions, 0 deletions
diff --git a/contrib/SDL-3.2.8/src/thread/generic/SDL_syssem.c b/contrib/SDL-3.2.8/src/thread/generic/SDL_syssem.c new file mode 100644 index 0000000..dcd6fcc --- /dev/null +++ b/contrib/SDL-3.2.8/src/thread/generic/SDL_syssem.c | |||
| @@ -0,0 +1,183 @@ | |||
| 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 | // An implementation of semaphores using mutexes and condition variables | ||
| 24 | |||
| 25 | #include "SDL_systhread_c.h" | ||
| 26 | |||
| 27 | #ifdef SDL_THREADS_DISABLED | ||
| 28 | |||
| 29 | SDL_Semaphore *SDL_CreateSemaphore(Uint32 initial_value) | ||
| 30 | { | ||
| 31 | SDL_SetError("SDL not built with thread support"); | ||
| 32 | return NULL; | ||
| 33 | } | ||
| 34 | |||
| 35 | void SDL_DestroySemaphore(SDL_Semaphore *sem) | ||
| 36 | { | ||
| 37 | } | ||
| 38 | |||
| 39 | bool SDL_WaitSemaphoreTimeoutNS(SDL_Semaphore *sem, Sint64 timeoutNS) | ||
| 40 | { | ||
| 41 | return true; | ||
| 42 | } | ||
| 43 | |||
| 44 | Uint32 SDL_GetSemaphoreValue(SDL_Semaphore *sem) | ||
| 45 | { | ||
| 46 | return 0; | ||
| 47 | } | ||
| 48 | |||
| 49 | void SDL_SignalSemaphore(SDL_Semaphore *sem) | ||
| 50 | { | ||
| 51 | return; | ||
| 52 | } | ||
| 53 | |||
| 54 | #else | ||
| 55 | |||
| 56 | struct SDL_Semaphore | ||
| 57 | { | ||
| 58 | Uint32 count; | ||
| 59 | Uint32 waiters_count; | ||
| 60 | SDL_Mutex *count_lock; | ||
| 61 | SDL_Condition *count_nonzero; | ||
| 62 | }; | ||
| 63 | |||
| 64 | SDL_Semaphore *SDL_CreateSemaphore(Uint32 initial_value) | ||
| 65 | { | ||
| 66 | SDL_Semaphore *sem; | ||
| 67 | |||
| 68 | sem = (SDL_Semaphore *)SDL_malloc(sizeof(*sem)); | ||
| 69 | if (!sem) { | ||
| 70 | return NULL; | ||
| 71 | } | ||
| 72 | sem->count = initial_value; | ||
| 73 | sem->waiters_count = 0; | ||
| 74 | |||
| 75 | sem->count_lock = SDL_CreateMutex(); | ||
| 76 | sem->count_nonzero = SDL_CreateCondition(); | ||
| 77 | if (!sem->count_lock || !sem->count_nonzero) { | ||
| 78 | SDL_DestroySemaphore(sem); | ||
| 79 | return NULL; | ||
| 80 | } | ||
| 81 | |||
| 82 | return sem; | ||
| 83 | } | ||
| 84 | |||
| 85 | /* WARNING: | ||
| 86 | You cannot call this function when another thread is using the semaphore. | ||
| 87 | */ | ||
| 88 | void SDL_DestroySemaphore(SDL_Semaphore *sem) | ||
| 89 | { | ||
| 90 | if (sem) { | ||
| 91 | sem->count = 0xFFFFFFFF; | ||
| 92 | while (sem->waiters_count > 0) { | ||
| 93 | SDL_SignalCondition(sem->count_nonzero); | ||
| 94 | SDL_Delay(10); | ||
| 95 | } | ||
| 96 | SDL_DestroyCondition(sem->count_nonzero); | ||
| 97 | if (sem->count_lock) { | ||
| 98 | SDL_LockMutex(sem->count_lock); | ||
| 99 | SDL_UnlockMutex(sem->count_lock); | ||
| 100 | SDL_DestroyMutex(sem->count_lock); | ||
| 101 | } | ||
| 102 | SDL_free(sem); | ||
| 103 | } | ||
| 104 | } | ||
| 105 | |||
| 106 | bool SDL_WaitSemaphoreTimeoutNS(SDL_Semaphore *sem, Sint64 timeoutNS) | ||
| 107 | { | ||
| 108 | bool result = false; | ||
| 109 | |||
| 110 | if (!sem) { | ||
| 111 | return true; | ||
| 112 | } | ||
| 113 | |||
| 114 | if (timeoutNS == 0) { | ||
| 115 | SDL_LockMutex(sem->count_lock); | ||
| 116 | if (sem->count > 0) { | ||
| 117 | --sem->count; | ||
| 118 | result = true; | ||
| 119 | } | ||
| 120 | SDL_UnlockMutex(sem->count_lock); | ||
| 121 | } else if (timeoutNS < 0) { | ||
| 122 | SDL_LockMutex(sem->count_lock); | ||
| 123 | ++sem->waiters_count; | ||
| 124 | while (sem->count == 0) { | ||
| 125 | SDL_WaitConditionTimeoutNS(sem->count_nonzero, sem->count_lock, -1); | ||
| 126 | } | ||
| 127 | --sem->waiters_count; | ||
| 128 | --sem->count; | ||
| 129 | result = true; | ||
| 130 | SDL_UnlockMutex(sem->count_lock); | ||
| 131 | } else { | ||
| 132 | Uint64 stop_time = SDL_GetTicksNS() + timeoutNS; | ||
| 133 | |||
| 134 | SDL_LockMutex(sem->count_lock); | ||
| 135 | ++sem->waiters_count; | ||
| 136 | while (sem->count == 0) { | ||
| 137 | Sint64 waitNS = (Sint64)(stop_time - SDL_GetTicksNS()); | ||
| 138 | if (waitNS > 0) { | ||
| 139 | SDL_WaitConditionTimeoutNS(sem->count_nonzero, sem->count_lock, waitNS); | ||
| 140 | } else { | ||
| 141 | break; | ||
| 142 | } | ||
| 143 | } | ||
| 144 | --sem->waiters_count; | ||
| 145 | |||
| 146 | if (sem->count > 0) { | ||
| 147 | --sem->count; | ||
| 148 | result = true; | ||
| 149 | } | ||
| 150 | SDL_UnlockMutex(sem->count_lock); | ||
| 151 | } | ||
| 152 | |||
| 153 | return result; | ||
| 154 | } | ||
| 155 | |||
| 156 | Uint32 SDL_GetSemaphoreValue(SDL_Semaphore *sem) | ||
| 157 | { | ||
| 158 | Uint32 value; | ||
| 159 | |||
| 160 | value = 0; | ||
| 161 | if (sem) { | ||
| 162 | SDL_LockMutex(sem->count_lock); | ||
| 163 | value = sem->count; | ||
| 164 | SDL_UnlockMutex(sem->count_lock); | ||
| 165 | } | ||
| 166 | return value; | ||
| 167 | } | ||
| 168 | |||
| 169 | void SDL_SignalSemaphore(SDL_Semaphore *sem) | ||
| 170 | { | ||
| 171 | if (!sem) { | ||
| 172 | return; | ||
| 173 | } | ||
| 174 | |||
| 175 | SDL_LockMutex(sem->count_lock); | ||
| 176 | if (sem->waiters_count > 0) { | ||
| 177 | SDL_SignalCondition(sem->count_nonzero); | ||
| 178 | } | ||
| 179 | ++sem->count; | ||
| 180 | SDL_UnlockMutex(sem->count_lock); | ||
| 181 | } | ||
| 182 | |||
| 183 | #endif // SDL_THREADS_DISABLED | ||
