SDL 2.0
SDL_sysmutex.c
Go to the documentation of this file.
1/*
2 Simple DirectMedia Layer
3 Copyright (C) 1997-2019 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/* An implementation of mutexes using semaphores */
26
27#include "SDL_thread.h"
28#include "SDL_systhread_c.h"
29
30
31struct SDL_mutex
32{
33 int recursive;
35 SDL_sem *sem;
36};
37
38/* Create a mutex */
41{
43
44 /* Allocate mutex memory */
45 mutex = (SDL_mutex *) SDL_malloc(sizeof(*mutex));
46 if (mutex) {
47 /* Create the mutex semaphore, with initial value 1 */
49 mutex->recursive = 0;
50 mutex->owner = 0;
51 if (!mutex->sem) {
53 mutex = NULL;
54 }
55 } else {
57 }
58 return mutex;
59}
60
61/* Free the mutex */
62void
64{
65 if (mutex) {
66 if (mutex->sem) {
68 }
70 }
71}
72
73/* Lock the semaphore */
74int
76{
77#if SDL_THREADS_DISABLED
78 return 0;
79#else
80 SDL_threadID this_thread;
81
82 if (mutex == NULL) {
83 return SDL_SetError("Passed a NULL mutex");
84 }
85
86 this_thread = SDL_ThreadID();
87 if (mutex->owner == this_thread) {
89 } else {
90 /* The order of operations is important.
91 We set the locking thread id after we obtain the lock
92 so unlocks from other threads will fail.
93 */
95 mutex->owner = this_thread;
96 mutex->recursive = 0;
97 }
98
99 return 0;
100#endif /* SDL_THREADS_DISABLED */
101}
102
103/* Unlock the mutex */
104int
106{
107#if SDL_THREADS_DISABLED
108 return 0;
109#else
110 if (mutex == NULL) {
111 return SDL_SetError("Passed a NULL mutex");
112 }
113
114 /* If we don't own the mutex, we can't unlock it */
115 if (SDL_ThreadID() != mutex->owner) {
116 return SDL_SetError("mutex not owned by this thread");
117 }
118
119 if (mutex->recursive) {
120 --mutex->recursive;
121 } else {
122 /* The order of operations is important.
123 First reset the owner so another thread doesn't lock
124 the mutex and set the ownership before we reset it,
125 then release the lock semaphore.
126 */
127 mutex->owner = 0;
129 }
130 return 0;
131#endif /* SDL_THREADS_DISABLED */
132}
133
134#endif /* SDL_THREAD_PSP */
135
136/* vi: set ts=4 sw=4 expandtab: */
#define SDL_SetError
#define SDL_SemPost
#define SDL_SemWait
#define SDL_malloc
#define SDL_ThreadID
#define SDL_DestroySemaphore
#define SDL_free
#define SDL_CreateSemaphore
#define SDL_OutOfMemory()
Definition: SDL_error.h:52
int SDL_mutexP(SDL_mutex *mutex)
unsigned long SDL_threadID
Definition: SDL_thread.h:49
#define NULL
Definition: begin_code.h:167
void SDL_DestroyMutex(SDL_mutex *mutex)
Definition: SDL_sysmutex.c:61
int SDL_mutexV(SDL_mutex *mutex)
Definition: SDL_sysmutex.c:136
SDL_mutex * SDL_CreateMutex(void)
Definition: SDL_sysmutex.c:38
SDL_threadID owner
Definition: SDL_sysmutex.c:32
int recursive
Definition: SDL_sysmutex.c:31
SDL_sem * sem
Definition: SDL_sysmutex.c:33
static SDL_mutex * mutex
Definition: testlock.c:23