view Isolated/SimpleMutex.c @ 66:5c9eaf0cc385

Macro cleanups for selecting which decoders to compile in. Before this was hacked for Apple vs. Android. But now it is Apple vs. whatever to be a little more flexible. If Apple, Core Audio will always be picked for WAV/MP3. This could be more flexible, but for shipping apps, there are not a lot of compelling reasons to not use Core Audio when it is available. Ogg Vorbis/Tremor are in common areas so it is directly selectable since Core Audio does not support Ogg. You are responsible for not trying to compile/link both Vorbis/Tremor at the same time. They are mutually exclusive.
author Eric Wing <ewing . public |-at-| gmail . com>
date Tue, 19 Jun 2012 00:40:32 -0700
parents 71b465ff0622
children
line wrap: on
line source

#include "SimpleMutex.h"
#include <stdlib.h>

#if defined(DEBUG)
#include <stdio.h>
#define MUTEXDBG(x) printf x
#else
#define MUTEXDBG(x)
#endif

#if defined(_WIN32) && !defined(__CYGWIN32__)
		#include <windows.h>
		#include <winbase.h> /* For CreateMutex(), LockFile() */

		struct SimpleMutex
		{
			HANDLE nativeMutex;
		};


		SimpleMutex* SimpleMutex_CreateMutex()
		{
			SimpleMutex* simple_mutex = (SimpleMutex*)malloc(sizeof(SimpleMutex));
			if(NULL == simple_mutex)
			{
				MUTEXDBG(("Out of memory.\n"));				
				return NULL;		
			}
			simple_mutex->nativeMutex = CreateMutex(NULL, FALSE, NULL);
			if(NULL == simple_mutex->nativeMutex)
			{
				MUTEXDBG(("Out of memory.\n"));				
				free(simple_mutex);
				return NULL;		
			}
			return simple_mutex;
		}
		void SimpleMutex_DestroyMutex(SimpleMutex* simple_mutex)
		{
			if(NULL == simple_mutex)
			{
				return;
			}
			CloseHandle(simple_mutex->nativeMutex);
			free(simple_mutex);
		}
		/* This will return true if locking is successful, false if not.
		 */
		int SimpleMutex_LockMutex(SimpleMutex* simple_mutex)
		{
#ifdef DEBUG
			if(NULL == simple_mutex)
			{
				MUTEXDBG(("SimpleMutex_LockMutex was passed NULL\n"));	
				return 0;
			}
#endif
			return(
				WaitForSingleObject(
					simple_mutex->nativeMutex,
					INFINITE
				) != WAIT_FAILED
			);
		}
		void SimpleMutex_UnlockMutex(SimpleMutex* simple_mutex)
		{
#ifdef DEBUG
			if(NULL == simple_mutex)
			{
				MUTEXDBG(("SimpleMutex_UnlockMutex was passed NULL\n"));	
				return;
			}
#endif
			ReleaseMutex(
				simple_mutex->nativeMutex
			);
		}
#else /* Assuming POSIX...maybe not a good assumption. */
		#include <pthread.h>

		struct SimpleMutex
		{
			pthread_mutex_t* nativeMutex;
		};

		SimpleMutex* SimpleMutex_CreateMutex()
		{
			int ret_val;
			SimpleMutex* simple_mutex = (SimpleMutex*)malloc(sizeof(SimpleMutex));
			if(NULL == simple_mutex)
			{
				MUTEXDBG(("Out of memory.\n"));				
				return NULL;		
			}
			simple_mutex->nativeMutex = (pthread_mutex_t*)malloc(sizeof(pthread_mutex_t));
			if(NULL == simple_mutex->nativeMutex)
			{
				MUTEXDBG(("Out of memory.\n"));
				free(simple_mutex);
				return NULL;		
			}

			ret_val = pthread_mutex_init(simple_mutex->nativeMutex, NULL);
			if(0 != ret_val)
			{
				free(simple_mutex->nativeMutex);
				free(simple_mutex);
				return NULL;
			}
			return simple_mutex;
		}
		void SimpleMutex_DestroyMutex(SimpleMutex* simple_mutex)
		{
			if(NULL != simple_mutex)
			{
				pthread_mutex_destroy(simple_mutex->nativeMutex);
				free(simple_mutex->nativeMutex);
				free(simple_mutex);
			}
		}
		/* This will return true if locking is successful, false if not.
		 * (This is the opposite of pthread_mutex_lock which returns 
		 * 0 for success.)
		 */
		int SimpleMutex_LockMutex(SimpleMutex* simple_mutex)
		{
#ifdef DEBUG
			if(NULL == simple_mutex)
			{
				MUTEXDBG(("SimpleMutex_LockMutex was passed NULL\n"));	
				return 0;
			}
#endif
			return(
				pthread_mutex_lock(
					simple_mutex->nativeMutex
				) == 0
			);
		}
		void SimpleMutex_UnlockMutex(SimpleMutex* simple_mutex)
		{
#ifdef DEBUG			
			if(NULL == simple_mutex)
			{
				MUTEXDBG(("SimpleMutex_LockMutex was passed NULL\n"));	
				return;
			}
#endif
			pthread_mutex_unlock(
				simple_mutex->nativeMutex
			);
		}
#endif