view Isolated/SimpleMutex.c @ 73:32757e494675

Fixed bug that sets the wrong variable for fading in Pause. There still seems to be another bug related to fading though based on Johnson Lin's test case. Audio no longer stops and triggers the completion callback, but the volume doesn't seem to go up to max for long fade-ins. Still investigating.
author Eric Wing <ewing . public |-at-| gmail . com>
date Fri, 10 Aug 2012 22:53:14 -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