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