summaryrefslogtreecommitdiff
path: root/contrib/SDL-3.2.8/src/thread/vita/SDL_syssem.c
diff options
context:
space:
mode:
Diffstat (limited to 'contrib/SDL-3.2.8/src/thread/vita/SDL_syssem.c')
-rw-r--r--contrib/SDL-3.2.8/src/thread/vita/SDL_syssem.c122
1 files changed, 122 insertions, 0 deletions
diff --git a/contrib/SDL-3.2.8/src/thread/vita/SDL_syssem.c b/contrib/SDL-3.2.8/src/thread/vita/SDL_syssem.c
new file mode 100644
index 0000000..3c6d28a
--- /dev/null
+++ b/contrib/SDL-3.2.8/src/thread/vita/SDL_syssem.c
@@ -0,0 +1,122 @@
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_VITA
24
25// Semaphore functions for the VITA.
26
27#include <stdio.h>
28#include <stdlib.h>
29
30#include <psp2/types.h>
31#include <psp2/kernel/error.h>
32#include <psp2/kernel/threadmgr.h>
33
34struct SDL_Semaphore
35{
36 SceUID semid;
37};
38
39// Create a semaphore
40SDL_Semaphore *SDL_CreateSemaphore(Uint32 initial_value)
41{
42 SDL_Semaphore *sem;
43
44 sem = (SDL_Semaphore *)SDL_malloc(sizeof(*sem));
45 if (sem) {
46 // TODO: Figure out the limit on the maximum value.
47 sem->semid = sceKernelCreateSema("SDL sema", 0, initial_value, 255, NULL);
48 if (sem->semid < 0) {
49 SDL_SetError("Couldn't create semaphore");
50 SDL_free(sem);
51 sem = NULL;
52 }
53 }
54
55 return sem;
56}
57
58// Free the semaphore
59void SDL_DestroySemaphore(SDL_Semaphore *sem)
60{
61 if (sem) {
62 if (sem->semid > 0) {
63 sceKernelDeleteSema(sem->semid);
64 sem->semid = 0;
65 }
66
67 SDL_free(sem);
68 }
69}
70
71/* TODO: This routine is a bit overloaded.
72 * If the timeout is 0 then just poll the semaphore; if it's -1, pass
73 * NULL to sceKernelWaitSema() so that it waits indefinitely; and if the timeout
74 * is specified, convert it to microseconds. */
75bool SDL_WaitSemaphoreTimeoutNS(SDL_Semaphore *sem, Sint64 timeoutNS)
76{
77 SceUInt timeoutUS;
78 SceUInt *pTimeout = NULL;
79
80 if (!sem) {
81 return true;
82 }
83
84 if (timeoutNS == 0) {
85 return (sceKernelPollSema(sem->semid, 1) == 0);
86 }
87
88 if (timeoutNS > 0) {
89 timeoutUS = (SceUInt)SDL_NS_TO_US(timeoutNS); // Convert to microseconds.
90 pTimeout = &timeoutUS;
91 }
92
93 return (sceKernelWaitSema(sem->semid, 1, pTimeout) == 0);
94}
95
96// Returns the current count of the semaphore
97Uint32 SDL_GetSemaphoreValue(SDL_Semaphore *sem)
98{
99 SceKernelSemaInfo info;
100 info.size = sizeof(info);
101
102 if (!sem) {
103 return 0;
104 }
105
106 if (sceKernelGetSemaInfo(sem->semid, &info) >= 0) {
107 return info.currentCount;
108 }
109
110 return 0;
111}
112
113void SDL_SignalSemaphore(SDL_Semaphore *sem)
114{
115 if (!sem) {
116 return;
117 }
118
119 sceKernelSignalSema(sem->semid, 1);
120}
121
122#endif // SDL_THREAD_VITA