Atlas - SDL_syssem.c

Home / ext / SDL / src / thread / dos Lines: 1 | Size: 2999 bytes [Download] [Show on GitHub] [Search similar files] [Raw] [Raw (proxy)]
[FILE BEGIN]
1/* 2 Simple DirectMedia Layer 3 Copyright (C) 1997-2026 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_DOS 24 25/* Semaphore implementation for DOS cooperative threading. 26 Uses cli/sti for atomicity and cooperative yielding for waits. */ 27 28#include "../../core/dos/SDL_dos.h" 29#include "../../core/dos/SDL_dos_scheduler.h" 30 31struct SDL_Semaphore 32{ 33 volatile Uint32 count; 34}; 35 36SDL_Semaphore *SDL_CreateSemaphore(Uint32 initial_value) 37{ 38 SDL_Semaphore *sem = (SDL_Semaphore *)SDL_malloc(sizeof(*sem)); 39 if (sem) { 40 sem->count = initial_value; 41 } 42 return sem; 43} 44 45void SDL_DestroySemaphore(SDL_Semaphore *sem) 46{ 47 if (sem) { 48 SDL_free(sem); 49 } 50} 51 52bool SDL_WaitSemaphoreTimeoutNS(SDL_Semaphore *sem, Sint64 timeoutNS) 53{ 54 if (!sem) { 55 return true; 56 } 57 58 /* Try-wait (poll): check and decrement if possible */ 59 if (timeoutNS == 0) { 60 bool acquired = false; 61 DOS_DisableInterrupts(); 62 if (sem->count > 0) { 63 sem->count--; 64 acquired = true; 65 } 66 DOS_EnableInterrupts(); 67 return acquired; 68 } 69 70 /* Indefinite wait (-1) or timed wait */ 71 Uint64 deadline = 0; 72 if (timeoutNS > 0) { 73 deadline = SDL_GetPerformanceCounter() + 74 (Uint64)((double)timeoutNS * (double)SDL_GetPerformanceFrequency() / 1e9); 75 } 76 77 for (;;) { 78 DOS_DisableInterrupts(); 79 if (sem->count > 0) { 80 sem->count--; 81 DOS_EnableInterrupts(); 82 return true; /* Acquired */ 83 } 84 DOS_EnableInterrupts(); 85 86 /* Check timeout */ 87 if (timeoutNS > 0 && SDL_GetPerformanceCounter() >= deadline) { 88 return false; /* Timed out */ 89 } 90 91 /* Yield to other threads instead of busy-spinning */ 92 DOS_Yield(); 93 } 94} 95 96Uint32 SDL_GetSemaphoreValue(SDL_Semaphore *sem) 97{ 98 if (!sem) { 99 return 0; 100 } 101 return sem->count; 102} 103 104void SDL_SignalSemaphore(SDL_Semaphore *sem) 105{ 106 if (!sem) { 107 return; 108 } 109 110 DOS_DisableInterrupts(); 111 sem->count++; 112 DOS_EnableInterrupts(); 113} 114 115#endif /* SDL_THREAD_DOS */ 116
[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.