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/n3ds/SDL_syssem.c | |
Initial commit
Diffstat (limited to 'contrib/SDL-3.2.8/src/thread/n3ds/SDL_syssem.c')
| -rw-r--r-- | contrib/SDL-3.2.8/src/thread/n3ds/SDL_syssem.c | 112 |
1 files changed, 112 insertions, 0 deletions
diff --git a/contrib/SDL-3.2.8/src/thread/n3ds/SDL_syssem.c b/contrib/SDL-3.2.8/src/thread/n3ds/SDL_syssem.c new file mode 100644 index 0000000..3207a2d --- /dev/null +++ b/contrib/SDL-3.2.8/src/thread/n3ds/SDL_syssem.c | |||
| @@ -0,0 +1,112 @@ | |||
| 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 | #ifdef SDL_THREAD_N3DS | ||
| 24 | |||
| 25 | // An implementation of semaphores using libctru's LightSemaphore | ||
| 26 | |||
| 27 | #include <3ds.h> | ||
| 28 | |||
| 29 | struct SDL_Semaphore | ||
| 30 | { | ||
| 31 | LightSemaphore semaphore; | ||
| 32 | }; | ||
| 33 | |||
| 34 | SDL_Semaphore *SDL_CreateSemaphore(Uint32 initial_value) | ||
| 35 | { | ||
| 36 | SDL_Semaphore *sem; | ||
| 37 | |||
| 38 | if (initial_value > SDL_MAX_SINT16) { | ||
| 39 | SDL_SetError("Initial semaphore value too high for this platform"); | ||
| 40 | return NULL; | ||
| 41 | } | ||
| 42 | |||
| 43 | sem = (SDL_Semaphore *)SDL_malloc(sizeof(*sem)); | ||
| 44 | if (!sem) { | ||
| 45 | return NULL; | ||
| 46 | } | ||
| 47 | |||
| 48 | LightSemaphore_Init(&sem->semaphore, initial_value, SDL_MAX_SINT16); | ||
| 49 | |||
| 50 | return sem; | ||
| 51 | } | ||
| 52 | |||
| 53 | /* WARNING: | ||
| 54 | You cannot call this function when another thread is using the semaphore. | ||
| 55 | */ | ||
| 56 | void SDL_DestroySemaphore(SDL_Semaphore *sem) | ||
| 57 | { | ||
| 58 | SDL_free(sem); | ||
| 59 | } | ||
| 60 | |||
| 61 | static bool WaitOnSemaphoreFor(SDL_Semaphore *sem, Sint64 timeoutNS) | ||
| 62 | { | ||
| 63 | Uint64 stop_time = SDL_GetTicksNS() + timeoutNS; | ||
| 64 | while (SDL_GetTicksNS() < stop_time) { | ||
| 65 | if (LightSemaphore_TryAcquire(&sem->semaphore, 1) == 0) { | ||
| 66 | return true; | ||
| 67 | } | ||
| 68 | // 100 microseconds seems to be the sweet spot | ||
| 69 | SDL_DelayNS(SDL_US_TO_NS(100)); | ||
| 70 | } | ||
| 71 | |||
| 72 | // If we failed, yield to avoid starvation on busy waits | ||
| 73 | SDL_DelayNS(1); | ||
| 74 | return false; | ||
| 75 | } | ||
| 76 | |||
| 77 | bool SDL_WaitSemaphoreTimeoutNS(SDL_Semaphore *sem, Sint64 timeoutNS) | ||
| 78 | { | ||
| 79 | if (!sem) { | ||
| 80 | return true; | ||
| 81 | } | ||
| 82 | |||
| 83 | if (timeoutNS < 0) { // -1 == wait indefinitely. | ||
| 84 | LightSemaphore_Acquire(&sem->semaphore, 1); | ||
| 85 | return true; | ||
| 86 | } | ||
| 87 | |||
| 88 | if (LightSemaphore_TryAcquire(&sem->semaphore, 1) == 0) { | ||
| 89 | return true; | ||
| 90 | } | ||
| 91 | |||
| 92 | return WaitOnSemaphoreFor(sem, timeoutNS); | ||
| 93 | } | ||
| 94 | |||
| 95 | Uint32 SDL_GetSemaphoreValue(SDL_Semaphore *sem) | ||
| 96 | { | ||
| 97 | if (!sem) { | ||
| 98 | return 0; | ||
| 99 | } | ||
| 100 | return sem->semaphore.current_count; | ||
| 101 | } | ||
| 102 | |||
| 103 | void SDL_SignalSemaphore(SDL_Semaphore *sem) | ||
| 104 | { | ||
| 105 | if (sem) { | ||
| 106 | return; | ||
| 107 | } | ||
| 108 | |||
| 109 | LightSemaphore_Release(&sem->semaphore, 1); | ||
| 110 | } | ||
| 111 | |||
| 112 | #endif // SDL_THREAD_N3DS | ||
