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 /* An implementation of mutexes using semaphores */
26 
27 #include "SDL_thread.h"
28 #include "SDL_systhread_c.h"
29 
30 
31 struct SDL_mutex
32 {
33     int recursive;
34     SDL_threadID owner;
35     SDL_sem *sem;
36 };
37 
38 /* Create a mutex */
39 SDL_mutex *
SDL_CreateMutex(void)40 SDL_CreateMutex(void)
41 {
42     SDL_mutex *mutex;
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 */
48         mutex->sem = SDL_CreateSemaphore(1);
49         mutex->recursive = 0;
50         mutex->owner = 0;
51         if (!mutex->sem) {
52             SDL_free(mutex);
53             mutex = NULL;
54         }
55     } else {
56         SDL_OutOfMemory();
57     }
58     return mutex;
59 }
60 
61 /* Free the mutex */
62 void
SDL_DestroyMutex(SDL_mutex * mutex)63 SDL_DestroyMutex(SDL_mutex * mutex)
64 {
65     if (mutex) {
66         if (mutex->sem) {
67             SDL_DestroySemaphore(mutex->sem);
68         }
69         SDL_free(mutex);
70     }
71 }
72 
73 /* Lock the semaphore */
74 int
SDL_mutexP(SDL_mutex * mutex)75 SDL_mutexP(SDL_mutex * mutex)
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) {
88         ++mutex->recursive;
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          */
94         SDL_SemWait(mutex->sem);
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 */
104 int
SDL_mutexV(SDL_mutex * mutex)105 SDL_mutexV(SDL_mutex * mutex)
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;
128         SDL_SemPost(mutex->sem);
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: */
137