src/thread/win32/SDL_sysmutex.c
author Sam Lantinga <slouken@libsdl.org>
Tue, 21 Feb 2006 08:46:50 +0000
changeset 1402 d910939febfa
parent 1358 c71e05b4dc2e
child 1433 bb6839704ed6
permissions -rw-r--r--
Use consistent identifiers for the various platforms we support.
Make sure every source file includes SDL_config.h, so the proper system
headers are chosen.
     1 /*
     2     SDL - Simple DirectMedia Layer
     3     Copyright (C) 1997-2006 Sam Lantinga
     4 
     5     This library is free software; you can redistribute it and/or
     6     modify it under the terms of the GNU Lesser General Public
     7     License as published by the Free Software Foundation; either
     8     version 2.1 of the License, or (at your option) any later version.
     9 
    10     This library is distributed in the hope that it will be useful,
    11     but WITHOUT ANY WARRANTY; without even the implied warranty of
    12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    13     Lesser General Public License for more details.
    14 
    15     You should have received a copy of the GNU Lesser General Public
    16     License along with this library; if not, write to the Free Software
    17     Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
    18 
    19     Sam Lantinga
    20     slouken@libsdl.org
    21 */
    22 #include "SDL_config.h"
    23 
    24 /* Mutex functions using the Win32 API */
    25 
    26 #include "SDL_windows.h"
    27 
    28 #include "SDL_mutex.h"
    29 
    30 
    31 struct SDL_mutex {
    32 	HANDLE id;
    33 };
    34 
    35 /* Create a mutex */
    36 SDL_mutex *SDL_CreateMutex(void)
    37 {
    38 	SDL_mutex *mutex;
    39 
    40 	/* Allocate mutex memory */
    41 	mutex = (SDL_mutex *)SDL_malloc(sizeof(*mutex));
    42 	if ( mutex ) {
    43 		/* Create the mutex, with initial value signaled */
    44 		mutex->id = CreateMutex(NULL, FALSE, NULL);
    45 		if ( ! mutex->id ) {
    46 			SDL_SetError("Couldn't create mutex");
    47 			SDL_free(mutex);
    48 			mutex = NULL;
    49 		}
    50 	} else {
    51 		SDL_OutOfMemory();
    52 	}
    53 	return(mutex);
    54 }
    55 
    56 /* Free the mutex */
    57 void SDL_DestroyMutex(SDL_mutex *mutex)
    58 {
    59 	if ( mutex ) {
    60 		if ( mutex->id ) {
    61 			CloseHandle(mutex->id);
    62 			mutex->id = 0;
    63 		}
    64 		SDL_free(mutex);
    65 	}
    66 }
    67 
    68 /* Lock the mutex */
    69 int SDL_mutexP(SDL_mutex *mutex)
    70 {
    71 	if ( mutex == NULL ) {
    72 		SDL_SetError("Passed a NULL mutex");
    73 		return -1;
    74 	}
    75 	if ( WaitForSingleObject(mutex->id, INFINITE) == WAIT_FAILED ) {
    76 		SDL_SetError("Couldn't wait on mutex");
    77 		return -1;
    78 	}
    79 	return(0);
    80 }
    81 
    82 /* Unlock the mutex */
    83 int SDL_mutexV(SDL_mutex *mutex)
    84 {
    85 	if ( mutex == NULL ) {
    86 		SDL_SetError("Passed a NULL mutex");
    87 		return -1;
    88 	}
    89 	if ( ReleaseMutex(mutex->id) == FALSE ) {
    90 		SDL_SetError("Couldn't release mutex");
    91 		return -1;
    92 	}
    93 	return(0);
    94 }