Atlas - SDL_syssem.c

Home / ext / SDL / src / thread / n3ds Lines: 1 | Size: 2873 bytes [Download] [Show on GitHub] [Search similar files] [Raw] [Raw (proxy)]
[FILE BEGIN]
1/* 2 Simple DirectMedia Layer 3 Copyright (C) 1997-2025 Sam Lantinga <[email protected]> 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 29struct SDL_Semaphore 30{ 31 LightSemaphore semaphore; 32}; 33 34SDL_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*/ 56void SDL_DestroySemaphore(SDL_Semaphore *sem) 57{ 58 SDL_free(sem); 59} 60 61static 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 77bool 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 95Uint32 SDL_GetSemaphoreValue(SDL_Semaphore *sem) 96{ 97 if (!sem) { 98 return 0; 99 } 100 return sem->semaphore.current_count; 101} 102 103void 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 113
[FILE END]
(C) 2025 0x4248 (C) 2025 4248 Media and 4248 Systems, All part of 0x4248 See LICENCE files for more information. Not all files are by 0x4248 always check Licencing.