src/thread/win32/SDL_sysmutex.c
author Sam Lantinga <slouken@libsdl.org>
Mon, 21 Sep 2009 08:58:51 +0000
branchSDL-1.2
changeset 4214 4250beeb5ad1
parent 4159 a1b03ba2fcd0
child 6137 4720145f848b
permissions -rw-r--r--
Oh yeah, we have GLX support too.
slouken@0
     1
/*
slouken@0
     2
    SDL - Simple DirectMedia Layer
slouken@4159
     3
    Copyright (C) 1997-2009 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@1402
    22
#include "SDL_config.h"
slouken@0
    23
slouken@0
    24
/* Mutex functions using the Win32 API */
slouken@0
    25
slouken@1433
    26
#define WIN32_LEAN_AND_MEAN
slouken@1433
    27
#include <windows.h>
slouken@0
    28
slouken@0
    29
#include "SDL_mutex.h"
slouken@0
    30
slouken@0
    31
slouken@0
    32
struct SDL_mutex {
slouken@0
    33
	HANDLE id;
slouken@0
    34
};
slouken@0
    35
slouken@0
    36
/* Create a mutex */
slouken@0
    37
SDL_mutex *SDL_CreateMutex(void)
slouken@0
    38
{
slouken@0
    39
	SDL_mutex *mutex;
slouken@0
    40
slouken@0
    41
	/* Allocate mutex memory */
slouken@1336
    42
	mutex = (SDL_mutex *)SDL_malloc(sizeof(*mutex));
slouken@0
    43
	if ( mutex ) {
slouken@0
    44
		/* Create the mutex, with initial value signaled */
slouken@0
    45
		mutex->id = CreateMutex(NULL, FALSE, NULL);
slouken@0
    46
		if ( ! mutex->id ) {
slouken@0
    47
			SDL_SetError("Couldn't create mutex");
slouken@1336
    48
			SDL_free(mutex);
slouken@0
    49
			mutex = NULL;
slouken@0
    50
		}
slouken@0
    51
	} else {
slouken@0
    52
		SDL_OutOfMemory();
slouken@0
    53
	}
slouken@0
    54
	return(mutex);
slouken@0
    55
}
slouken@0
    56
slouken@0
    57
/* Free the mutex */
slouken@0
    58
void SDL_DestroyMutex(SDL_mutex *mutex)
slouken@0
    59
{
slouken@0
    60
	if ( mutex ) {
slouken@0
    61
		if ( mutex->id ) {
slouken@0
    62
			CloseHandle(mutex->id);
slouken@0
    63
			mutex->id = 0;
slouken@0
    64
		}
slouken@1336
    65
		SDL_free(mutex);
slouken@0
    66
	}
slouken@0
    67
}
slouken@0
    68
slouken@0
    69
/* Lock the mutex */
slouken@0
    70
int SDL_mutexP(SDL_mutex *mutex)
slouken@0
    71
{
slouken@0
    72
	if ( mutex == NULL ) {
slouken@0
    73
		SDL_SetError("Passed a NULL mutex");
slouken@0
    74
		return -1;
slouken@0
    75
	}
slouken@0
    76
	if ( WaitForSingleObject(mutex->id, INFINITE) == WAIT_FAILED ) {
slouken@0
    77
		SDL_SetError("Couldn't wait on mutex");
slouken@0
    78
		return -1;
slouken@0
    79
	}
slouken@0
    80
	return(0);
slouken@0
    81
}
slouken@0
    82
slouken@0
    83
/* Unlock the mutex */
slouken@0
    84
int SDL_mutexV(SDL_mutex *mutex)
slouken@0
    85
{
slouken@0
    86
	if ( mutex == NULL ) {
slouken@0
    87
		SDL_SetError("Passed a NULL mutex");
slouken@0
    88
		return -1;
slouken@0
    89
	}
slouken@0
    90
	if ( ReleaseMutex(mutex->id) == FALSE ) {
slouken@0
    91
		SDL_SetError("Couldn't release mutex");
slouken@0
    92
		return -1;
slouken@0
    93
	}
slouken@0
    94
	return(0);
slouken@0
    95
}