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 mutex wrapper */
25
26#include "SDL_thread.h"
27#include "SDL_systhread_c.h"
28
29
30struct SDL_mutex {
31 int recursive;
32 Uint32 owner;
33 struct mutex m;
34};
35
36/* Create a mutex */
37SDL_mutex *SDL_CreateMutex(void)
38{
39 SDL_mutex *mutex;
40
41 /* Allocate mutex memory */
42 mutex = (SDL_mutex *)SDL_malloc(sizeof(*mutex));
43 if ( mutex ) {
44 rb->mutex_init(&mutex->m);
45 mutex->recursive = 0;
46 mutex->owner = 0;
47 } else {
48 SDL_OutOfMemory();
49 }
50 return mutex;
51}
52
53/* Free the mutex */
54void SDL_DestroyMutex(SDL_mutex *mutex)
55{
56 if ( mutex ) {
57 SDL_free(mutex);
58 }
59}
60
61int SDL_mutexP(SDL_mutex *mutex)
62{
63#if SDL_THREADS_DISABLED
64 return 0;
65#else
66 Uint32 this_thread;
67
68 if ( mutex == NULL ) {
69 SDL_SetError("Passed a NULL mutex");
70 return -1;
71 }
72
73 this_thread = SDL_ThreadID();
74 if ( mutex->owner == this_thread ) {
75 ++mutex->recursive;
76 } else {
77 /* The order of operations is important.
78 We set the locking thread id after we obtain the lock
79 so unlocks from other threads will fail.
80 */
81 rb->mutex_lock(&mutex->m);
82 mutex->owner = this_thread;
83 mutex->recursive = 0;
84 }
85
86 return 0;
87#endif /* SDL_THREADS_DISABLED */
88}
89
90/* Unlock the mutex */
91int SDL_mutexV(SDL_mutex *mutex)
92{
93#if SDL_THREADS_DISABLED
94 return 0;
95#else
96 if ( mutex == NULL ) {
97 SDL_SetError("Passed a NULL mutex");
98 return -1;
99 }
100
101 /* If we don't own the mutex, we can't unlock it */
102 if ( SDL_ThreadID() != mutex->owner ) {
103 SDL_SetError("mutex not owned by this thread");
104 return -1;
105 }
106
107 if ( mutex->recursive ) {
108 --mutex->recursive;
109 } else {
110 /* The order of operations is important.
111 First reset the owner so another thread doesn't lock
112 the mutex and set the ownership before we reset it,
113 then release the lock semaphore.
114 */
115 mutex->owner = 0;
116 rb->mutex_unlock(&mutex->m);
117 }
118 return 0;
119#endif /* SDL_THREADS_DISABLED */
120}