Atlas - SDL_syssem.c

Home / ext / SDL / src / thread / psp Lines: 1 | Size: 3027 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_PSP 24 25// Semaphore functions for the PSP. 26 27#include <stdio.h> 28#include <stdlib.h> 29 30#include <pspthreadman.h> 31#include <pspkerror.h> 32 33struct SDL_Semaphore 34{ 35 SceUID semid; 36}; 37 38// Create a semaphore 39SDL_Semaphore *SDL_CreateSemaphore(Uint32 initial_value) 40{ 41 SDL_Semaphore *sem; 42 43 sem = (SDL_Semaphore *)SDL_malloc(sizeof(*sem)); 44 if (sem) { 45 // TODO: Figure out the limit on the maximum value. 46 sem->semid = sceKernelCreateSema("SDL sema", 0, initial_value, 255, NULL); 47 if (sem->semid < 0) { 48 SDL_SetError("Couldn't create semaphore"); 49 SDL_free(sem); 50 sem = NULL; 51 } 52 } 53 54 return sem; 55} 56 57// Free the semaphore 58void SDL_DestroySemaphore(SDL_Semaphore *sem) 59{ 60 if (sem) { 61 if (sem->semid > 0) { 62 sceKernelDeleteSema(sem->semid); 63 sem->semid = 0; 64 } 65 66 SDL_free(sem); 67 } 68} 69 70/* TODO: This routine is a bit overloaded. 71 * If the timeout is 0 then just poll the semaphore; if it's -1, pass 72 * NULL to sceKernelWaitSema() so that it waits indefinitely; and if the timeout 73 * is specified, convert it to microseconds. */ 74bool SDL_WaitSemaphoreTimeoutNS(SDL_Semaphore *sem, Sint64 timeoutNS) 75{ 76 SceUInt timeoutUS; 77 SceUInt *pTimeout = NULL; 78 79 if (!sem) { 80 return true; 81 } 82 83 if (timeoutNS == 0) { 84 return (sceKernelPollSema(sem->semid, 1) == 0); 85 } 86 87 if (timeoutNS > 0) { 88 timeoutUS = (SceUInt)SDL_NS_TO_US(timeoutNS); // Convert to microseconds. 89 pTimeout = &timeoutUS; 90 } 91 92 return (sceKernelWaitSema(sem->semid, 1, pTimeout) == 0); 93} 94 95// Returns the current count of the semaphore 96Uint32 SDL_GetSemaphoreValue(SDL_Semaphore *sem) 97{ 98 SceKernelSemaInfo info; 99 100 if (!sem) { 101 return 0; 102 } 103 104 if (sceKernelReferSemaStatus(sem->semid, &info) == 0) { 105 return info.currentCount; 106 } 107 return 0; 108} 109 110void SDL_SignalSemaphore(SDL_Semaphore *sem) 111{ 112 if (!sem) { 113 return; 114 } 115 116 sceKernelSignalSema(sem->semid, 1); 117} 118 119#endif // SDL_THREAD_PSP 120
[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.