A modern Music Player Daemon based on Rockbox open source high quality audio player
libadwaita
audio
rust
zig
deno
mpris
rockbox
mpd
1/*
2 SDL - Simple DirectMedia Layer
3 Copyright (C) 1997-2012 Sam Lantinga
4
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
9
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public
16 License along with this library; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18
19 Sam Lantinga
20 slouken@libsdl.org
21*/
22#include "SDL_config.h"
23
24/* Rockbox semaphore wrapper */
25
26#include "SDL_timer.h"
27#include "SDL_thread.h"
28#include "SDL_systhread_c.h"
29
30
31#if SDL_THREADS_DISABLED
32
33SDL_sem *SDL_CreateSemaphore(Uint32 initial_value)
34{
35 SDL_SetError("SDL not configured with thread support");
36 return (SDL_sem *)0;
37}
38
39void SDL_DestroySemaphore(SDL_sem *sem)
40{
41 return;
42}
43
44int SDL_SemTryWait(SDL_sem *sem)
45{
46 SDL_SetError("SDL not configured with thread support");
47 return -1;
48}
49
50int SDL_SemWaitTimeout(SDL_sem *sem, Uint32 timeout)
51{
52 SDL_SetError("SDL not configured with thread support");
53 return -1;
54}
55
56int SDL_SemWait(SDL_sem *sem)
57{
58 SDL_SetError("SDL not configured with thread support");
59 return -1;
60}
61
62Uint32 SDL_SemValue(SDL_sem *sem)
63{
64 return 0;
65}
66
67int SDL_SemPost(SDL_sem *sem)
68{
69 SDL_SetError("SDL not configured with thread support");
70 return -1;
71}
72
73#else
74
75struct SDL_semaphore
76{
77 struct semaphore s;
78};
79
80SDL_sem *SDL_CreateSemaphore(Uint32 initial_value)
81{
82 SDL_sem *sem;
83
84 sem = (SDL_sem *)SDL_malloc(sizeof(*sem));
85 if ( ! sem ) {
86 SDL_OutOfMemory();
87 return NULL;
88 }
89
90 rb->semaphore_init(&sem->s, 99, initial_value);
91
92 return sem;
93}
94
95/* WARNING:
96 You cannot call this function when another thread is using the semaphore.
97*/
98void SDL_DestroySemaphore(SDL_sem *sem)
99{
100 if ( sem ) {
101 SDL_free(sem);
102 }
103}
104
105int SDL_SemTryWait(SDL_sem *sem)
106{
107 int retval;
108
109 if ( ! sem ) {
110 SDL_SetError("Passed a NULL semaphore");
111 return -1;
112 }
113
114 retval = SDL_MUTEX_TIMEDOUT;
115
116 if(rb->semaphore_wait(&sem->s, 0) == OBJ_WAIT_SUCCEEDED)
117 retval = 0;
118
119 return retval;
120}
121
122int SDL_SemWaitTimeout(SDL_sem *sem, Uint32 timeout)
123{
124 if ( ! sem ) {
125 SDL_SetError("Passed a NULL semaphore");
126 return -1;
127 }
128
129 /* A timeout of 0 is an easy case */
130 if ( timeout == 0 ) {
131 return SDL_SemTryWait(sem);
132 }
133
134 if(rb->semaphore_wait(&sem->s, timeout / (1000 / HZ)) == OBJ_WAIT_SUCCEEDED)
135 return 0;
136 else
137 return SDL_MUTEX_TIMEDOUT;
138
139 return SDL_MUTEX_TIMEDOUT;
140}
141
142int SDL_SemWait(SDL_sem *sem)
143{
144 return SDL_SemWaitTimeout(sem, SDL_MUTEX_MAXWAIT);
145}
146
147Uint32 SDL_SemValue(SDL_sem *sem)
148{
149 Uint32 value;
150
151 value = 0;
152 if ( sem ) {
153 value = sem->s.count;
154 }
155 return value;
156}
157
158int SDL_SemPost(SDL_sem *sem)
159{
160 if ( ! sem ) {
161 SDL_SetError("Passed a NULL semaphore");
162 return -1;
163 }
164
165 rb->semaphore_release(&sem->s);
166
167 return 0;
168}
169
170#endif /* SDL_THREADS_DISABLED */