SDL_syssem.c (3921B)
1 /* 2 Simple DirectMedia Layer 3 Copyright (C) 1997-2020 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 #if SDL_THREAD_PSP 24 25 /* Semaphore functions for the PSP. */ 26 27 #include <stdio.h> 28 #include <stdlib.h> 29 30 #include "SDL_error.h" 31 #include "SDL_thread.h" 32 33 #include <pspthreadman.h> 34 #include <pspkerror.h> 35 36 struct SDL_semaphore { 37 SceUID semid; 38 }; 39 40 41 /* Create a semaphore */ 42 SDL_sem *SDL_CreateSemaphore(Uint32 initial_value) 43 { 44 SDL_sem *sem; 45 46 sem = (SDL_sem *) malloc(sizeof(*sem)); 47 if (sem != NULL) { 48 /* TODO: Figure out the limit on the maximum value. */ 49 sem->semid = sceKernelCreateSema("SDL sema", 0, initial_value, 255, NULL); 50 if (sem->semid < 0) { 51 SDL_SetError("Couldn't create semaphore"); 52 free(sem); 53 sem = NULL; 54 } 55 } else { 56 SDL_OutOfMemory(); 57 } 58 59 return sem; 60 } 61 62 /* Free the semaphore */ 63 void SDL_DestroySemaphore(SDL_sem *sem) 64 { 65 if (sem != NULL) { 66 if (sem->semid > 0) { 67 sceKernelDeleteSema(sem->semid); 68 sem->semid = 0; 69 } 70 71 free(sem); 72 } 73 } 74 75 /* TODO: This routine is a bit overloaded. 76 * If the timeout is 0 then just poll the semaphore; if it's SDL_MUTEX_MAXWAIT, pass 77 * NULL to sceKernelWaitSema() so that it waits indefinitely; and if the timeout 78 * is specified, convert it to microseconds. */ 79 int SDL_SemWaitTimeout(SDL_sem *sem, Uint32 timeout) 80 { 81 Uint32 *pTimeout; 82 int res; 83 84 if (sem == NULL) { 85 SDL_SetError("Passed a NULL sem"); 86 return 0; 87 } 88 89 if (timeout == 0) { 90 res = sceKernelPollSema(sem->semid, 1); 91 if (res < 0) { 92 return SDL_MUTEX_TIMEDOUT; 93 } 94 return 0; 95 } 96 97 if (timeout == SDL_MUTEX_MAXWAIT) { 98 pTimeout = NULL; 99 } else { 100 timeout *= 1000; /* Convert to microseconds. */ 101 pTimeout = &timeout; 102 } 103 104 res = sceKernelWaitSema(sem->semid, 1, pTimeout); 105 switch (res) { 106 case SCE_KERNEL_ERROR_OK: 107 return 0; 108 case SCE_KERNEL_ERROR_WAIT_TIMEOUT: 109 return SDL_MUTEX_TIMEDOUT; 110 default: 111 return SDL_SetError("sceKernelWaitSema() failed"); 112 } 113 } 114 115 int SDL_SemTryWait(SDL_sem *sem) 116 { 117 return SDL_SemWaitTimeout(sem, 0); 118 } 119 120 int SDL_SemWait(SDL_sem *sem) 121 { 122 return SDL_SemWaitTimeout(sem, SDL_MUTEX_MAXWAIT); 123 } 124 125 /* Returns the current count of the semaphore */ 126 Uint32 SDL_SemValue(SDL_sem *sem) 127 { 128 SceKernelSemaInfo info; 129 130 if (sem == NULL) { 131 SDL_SetError("Passed a NULL sem"); 132 return 0; 133 } 134 135 if (sceKernelReferSemaStatus(sem->semid, &info) >= 0) { 136 return info.currentCount; 137 } 138 139 return 0; 140 } 141 142 int SDL_SemPost(SDL_sem *sem) 143 { 144 int res; 145 146 if (sem == NULL) { 147 return SDL_SetError("Passed a NULL sem"); 148 } 149 150 res = sceKernelSignalSema(sem->semid, 1); 151 if (res < 0) { 152 return SDL_SetError("sceKernelSignalSema() failed"); 153 } 154 155 return 0; 156 } 157 158 #endif /* SDL_THREAD_PSP */ 159 160 /* vim: ts=4 sw=4 161 */