19682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/*
29682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    SDL - Simple DirectMedia Layer
39682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    Copyright (C) 1997-2012 Sam Lantinga
49682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
59682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    This library is free software; you can redistribute it and/or
69682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    modify it under the terms of the GNU Lesser General Public
79682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    License as published by the Free Software Foundation; either
89682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    version 2.1 of the License, or (at your option) any later version.
99682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    This library is distributed in the hope that it will be useful,
119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    but WITHOUT ANY WARRANTY; without even the implied warranty of
129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    Lesser General Public License for more details.
149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    You should have received a copy of the GNU Lesser General Public
169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    License along with this library; if not, write to the Free Software
179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    Sam Lantinga
209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    slouken@libsdl.org
219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall*/
229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "SDL_config.h"
239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* An implementation of mutexes using semaphores */
259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "SDL_thread.h"
279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "SDL_systhread_c.h"
289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstruct SDL_mutex {
319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	int recursive;
329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	Uint32 owner;
339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_sem *sem;
349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall};
359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* Create a mutex */
379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse HallSDL_mutex *SDL_CreateMutex(void)
389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_mutex *mutex;
409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Allocate mutex memory */
429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	mutex = (SDL_mutex *)SDL_malloc(sizeof(*mutex));
439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( mutex ) {
449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		/* Create the mutex semaphore, with initial value 1 */
459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		mutex->sem = SDL_CreateSemaphore(1);
469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		mutex->recursive = 0;
479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		mutex->owner = 0;
489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if ( ! mutex->sem ) {
499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			SDL_free(mutex);
509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			mutex = NULL;
519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	} else {
539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_OutOfMemory();
549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return mutex;
569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* Free the mutex */
599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallvoid SDL_DestroyMutex(SDL_mutex *mutex)
609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( mutex ) {
629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if ( mutex->sem ) {
639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			SDL_DestroySemaphore(mutex->sem);
649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_free(mutex);
669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* Lock the semaphore */
709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallint SDL_mutexP(SDL_mutex *mutex)
719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#if SDL_THREADS_DISABLED
739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return 0;
749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#else
759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	Uint32 this_thread;
769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( mutex == NULL ) {
789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_SetError("Passed a NULL mutex");
799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		return -1;
809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	this_thread = SDL_ThreadID();
839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( mutex->owner == this_thread ) {
849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		++mutex->recursive;
859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	} else {
869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		/* The order of operations is important.
879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		   We set the locking thread id after we obtain the lock
889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		   so unlocks from other threads will fail.
899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		*/
909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_SemWait(mutex->sem);
919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		mutex->owner = this_thread;
929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		mutex->recursive = 0;
939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return 0;
969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif /* SDL_THREADS_DISABLED */
979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* Unlock the mutex */
1009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallint SDL_mutexV(SDL_mutex *mutex)
1019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
1029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#if SDL_THREADS_DISABLED
1039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return 0;
1049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#else
1059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( mutex == NULL ) {
1069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_SetError("Passed a NULL mutex");
1079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		return -1;
1089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
1099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* If we don't own the mutex, we can't unlock it */
1119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( SDL_ThreadID() != mutex->owner ) {
1129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_SetError("mutex not owned by this thread");
1139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		return -1;
1149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
1159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( mutex->recursive ) {
1179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		--mutex->recursive;
1189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	} else {
1199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		/* The order of operations is important.
1209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		   First reset the owner so another thread doesn't lock
1219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		   the mutex and set the ownership before we reset it,
1229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		   then release the lock semaphore.
1239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		 */
1249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		mutex->owner = 0;
1259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_SemPost(mutex->sem);
1269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
1279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return 0;
1289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif /* SDL_THREADS_DISABLED */
1299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
130