src/thread/generic/SDL_sysmutex.c
author Sam Lantinga
Mon, 06 Feb 2006 08:28:51 +0000
changeset 1330 450721ad5436
parent 1312 c9b51268668f
child 1336 3692456e7b0f
permissions -rw-r--r--
It's now possible to build SDL without any C runtime at all on Windows,
using Visual C++ 2005
slouken@0
     1
/*
slouken@0
     2
    SDL - Simple DirectMedia Layer
slouken@1312
     3
    Copyright (C) 1997-2006 Sam Lantinga
slouken@0
     4
slouken@0
     5
    This library is free software; you can redistribute it and/or
slouken@1312
     6
    modify it under the terms of the GNU Lesser General Public
slouken@0
     7
    License as published by the Free Software Foundation; either
slouken@1312
     8
    version 2.1 of the License, or (at your option) any later version.
slouken@0
     9
slouken@0
    10
    This library is distributed in the hope that it will be useful,
slouken@0
    11
    but WITHOUT ANY WARRANTY; without even the implied warranty of
slouken@0
    12
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
slouken@1312
    13
    Lesser General Public License for more details.
slouken@0
    14
slouken@1312
    15
    You should have received a copy of the GNU Lesser General Public
slouken@1312
    16
    License along with this library; if not, write to the Free Software
slouken@1312
    17
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
slouken@0
    18
slouken@0
    19
    Sam Lantinga
slouken@252
    20
    slouken@libsdl.org
slouken@0
    21
*/
slouken@0
    22
slouken@0
    23
/* An implementation of mutexes using semaphores */
slouken@0
    24
slouken@0
    25
#include "SDL_error.h"
slouken@0
    26
#include "SDL_thread.h"
slouken@1330
    27
#include "SDL_stdlib.h"
slouken@0
    28
#include "SDL_systhread_c.h"
slouken@0
    29
slouken@0
    30
slouken@0
    31
struct SDL_mutex {
slouken@0
    32
	int recursive;
slouken@0
    33
	Uint32 owner;
slouken@0
    34
	SDL_sem *sem;
slouken@0
    35
};
slouken@0
    36
slouken@0
    37
/* Create a mutex */
slouken@0
    38
SDL_mutex *SDL_CreateMutex(void)
slouken@0
    39
{
slouken@0
    40
	SDL_mutex *mutex;
slouken@0
    41
slouken@0
    42
	/* Allocate mutex memory */
slouken@0
    43
	mutex = (SDL_mutex *)malloc(sizeof(*mutex));
slouken@0
    44
	if ( mutex ) {
slouken@0
    45
		/* Create the mutex semaphore, with initial value 1 */
slouken@0
    46
		mutex->sem = SDL_CreateSemaphore(1);
slouken@0
    47
		mutex->recursive = 0;
slouken@0
    48
		mutex->owner = 0;
slouken@0
    49
		if ( ! mutex->sem ) {
slouken@0
    50
			free(mutex);
slouken@0
    51
			mutex = NULL;
slouken@0
    52
		}
slouken@0
    53
	} else {
slouken@0
    54
		SDL_OutOfMemory();
slouken@0
    55
	}
slouken@0
    56
	return mutex;
slouken@0
    57
}
slouken@0
    58
slouken@0
    59
/* Free the mutex */
slouken@0
    60
void SDL_DestroyMutex(SDL_mutex *mutex)
slouken@0
    61
{
slouken@0
    62
	if ( mutex ) {
slouken@0
    63
		if ( mutex->sem ) {
slouken@0
    64
			SDL_DestroySemaphore(mutex->sem);
slouken@0
    65
		}
slouken@0
    66
		free(mutex);
slouken@0
    67
	}
slouken@0
    68
}
slouken@0
    69
slouken@0
    70
/* Lock the semaphore */
slouken@0
    71
int SDL_mutexP(SDL_mutex *mutex)
slouken@0
    72
{
slouken@0
    73
#ifdef DISABLE_THREADS
slouken@0
    74
	return 0;
slouken@0
    75
#else
slouken@0
    76
	Uint32 this_thread;
slouken@0
    77
slouken@0
    78
	if ( mutex == NULL ) {
slouken@0
    79
		SDL_SetError("Passed a NULL mutex");
slouken@0
    80
		return -1;
slouken@0
    81
	}
slouken@0
    82
slouken@0
    83
	this_thread = SDL_ThreadID();
slouken@0
    84
	if ( mutex->owner == this_thread ) {
slouken@0
    85
		++mutex->recursive;
slouken@0
    86
	} else {
slouken@0
    87
		/* The order of operations is important.
slouken@0
    88
		   We set the locking thread id after we obtain the lock
slouken@0
    89
		   so unlocks from other threads will fail.
slouken@0
    90
		*/
slouken@0
    91
		SDL_SemWait(mutex->sem);
slouken@0
    92
		mutex->owner = this_thread;
slouken@0
    93
		mutex->recursive = 0;
slouken@0
    94
	}
slouken@0
    95
slouken@0
    96
	return 0;
slouken@0
    97
#endif /* DISABLE_THREADS */
slouken@0
    98
}
slouken@0
    99
slouken@0
   100
/* Unlock the mutex */
slouken@0
   101
int SDL_mutexV(SDL_mutex *mutex)
slouken@0
   102
{
slouken@0
   103
#ifdef DISABLE_THREADS
slouken@0
   104
	return 0;
slouken@0
   105
#else
slouken@0
   106
	if ( mutex == NULL ) {
slouken@0
   107
		SDL_SetError("Passed a NULL mutex");
slouken@0
   108
		return -1;
slouken@0
   109
	}
slouken@0
   110
slouken@0
   111
	/* If we don't own the mutex, we can't unlock it */
slouken@0
   112
	if ( SDL_ThreadID() != mutex->owner ) {
slouken@0
   113
		SDL_SetError("mutex not owned by this thread");
slouken@0
   114
		return -1;
slouken@0
   115
	}
slouken@0
   116
slouken@0
   117
	if ( mutex->recursive ) {
slouken@0
   118
		--mutex->recursive;
slouken@0
   119
	} else {
slouken@0
   120
		/* The order of operations is important.
slouken@0
   121
		   First reset the owner so another thread doesn't lock
slouken@0
   122
		   the mutex and set the ownership before we reset it,
slouken@0
   123
		   then release the lock semaphore.
slouken@0
   124
		 */
slouken@0
   125
		mutex->owner = 0;
slouken@0
   126
		SDL_SemPost(mutex->sem);
slouken@0
   127
	}
slouken@0
   128
	return 0;
slouken@0
   129
#endif /* DISABLE_THREADS */
slouken@0
   130
}