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
30struct SDL_mutex {
31	int recursive;
32	Uint32 owner;
33	SDL_sem *sem;
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		/* Create the mutex semaphore, with initial value 1 */
45		mutex->sem = SDL_CreateSemaphore(1);
46		mutex->recursive = 0;
47		mutex->owner = 0;
48		if ( ! mutex->sem ) {
49			SDL_free(mutex);
50			mutex = NULL;
51		}
52	} else {
53		SDL_OutOfMemory();
54	}
55	return mutex;
56}
57
58/* Free the mutex */
59void SDL_DestroyMutex(SDL_mutex *mutex)
60{
61	if ( mutex ) {
62		if ( mutex->sem ) {
63			SDL_DestroySemaphore(mutex->sem);
64		}
65		SDL_free(mutex);
66	}
67}
68
69/* Lock the semaphore */
70int SDL_mutexP(SDL_mutex *mutex)
71{
72#if SDL_THREADS_DISABLED
73	return 0;
74#else
75	Uint32 this_thread;
76
77	if ( mutex == NULL ) {
78		SDL_SetError("Passed a NULL mutex");
79		return -1;
80	}
81
82	this_thread = SDL_ThreadID();
83	if ( mutex->owner == this_thread ) {
84		++mutex->recursive;
85	} else {
86		/* The order of operations is important.
87		   We set the locking thread id after we obtain the lock
88		   so unlocks from other threads will fail.
89		*/
90		SDL_SemWait(mutex->sem);
91		mutex->owner = this_thread;
92		mutex->recursive = 0;
93	}
94
95	return 0;
96#endif /* SDL_THREADS_DISABLED */
97}
98
99/* Unlock the mutex */
100int SDL_mutexV(SDL_mutex *mutex)
101{
102#if SDL_THREADS_DISABLED
103	return 0;
104#else
105	if ( mutex == NULL ) {
106		SDL_SetError("Passed a NULL mutex");
107		return -1;
108	}
109
110	/* If we don't own the mutex, we can't unlock it */
111	if ( SDL_ThreadID() != mutex->owner ) {
112		SDL_SetError("mutex not owned by this thread");
113		return -1;
114	}
115
116	if ( mutex->recursive ) {
117		--mutex->recursive;
118	} else {
119		/* The order of operations is important.
120		   First reset the owner so another thread doesn't lock
121		   the mutex and set the ownership before we reset it,
122		   then release the lock semaphore.
123		 */
124		mutex->owner = 0;
125		SDL_SemPost(mutex->sem);
126	}
127	return 0;
128#endif /* SDL_THREADS_DISABLED */
129}
130