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/psp/SDL_syssem.c | |
Initial commit
Diffstat (limited to 'contrib/SDL-3.2.8/src/thread/psp/SDL_syssem.c')
| -rw-r--r-- | contrib/SDL-3.2.8/src/thread/psp/SDL_syssem.c | 119 |
1 files changed, 119 insertions, 0 deletions
diff --git a/contrib/SDL-3.2.8/src/thread/psp/SDL_syssem.c b/contrib/SDL-3.2.8/src/thread/psp/SDL_syssem.c new file mode 100644 index 0000000..27e332f --- /dev/null +++ b/contrib/SDL-3.2.8/src/thread/psp/SDL_syssem.c | |||
| @@ -0,0 +1,119 @@ | |||
| 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_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 | |||
| 33 | struct SDL_Semaphore | ||
| 34 | { | ||
| 35 | SceUID semid; | ||
| 36 | }; | ||
| 37 | |||
| 38 | // Create a semaphore | ||
| 39 | SDL_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 | ||
| 58 | void 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. */ | ||
| 74 | bool 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 | ||
| 96 | Uint32 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 | |||
| 110 | void 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 | ||
