src/thread/win32/SDL_sysmutex.c
author Sam Lantinga <slouken@libsdl.org>
Wed, 01 Feb 2006 06:32:25 +0000
changeset 1312 c9b51268668f
parent 769 b8d311d90021
child 1330 450721ad5436
permissions -rw-r--r--
Updated copyright information and removed rcs id lines (problematic in branch merges)
I batch edited these files, so please let me know if I've accidentally removed anybody's
credit here.
     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 
    23 /* Mutex functions using the Win32 API */
    24 
    25 #include <stdio.h>
    26 #include <stdlib.h>
    27 #include <windows.h>
    28 
    29 #include "SDL_error.h"
    30 #include "SDL_mutex.h"
    31 
    32 
    33 struct SDL_mutex {
    34 	HANDLE id;
    35 };
    36 
    37 /* Create a mutex */
    38 SDL_mutex *SDL_CreateMutex(void)
    39 {
    40 	SDL_mutex *mutex;
    41 
    42 	/* Allocate mutex memory */
    43 	mutex = (SDL_mutex *)malloc(sizeof(*mutex));
    44 	if ( mutex ) {
    45 		/* Create the mutex, with initial value signaled */
    46 		mutex->id = CreateMutex(NULL, FALSE, NULL);
    47 		if ( ! mutex->id ) {
    48 			SDL_SetError("Couldn't create mutex");
    49 			free(mutex);
    50 			mutex = NULL;
    51 		}
    52 	} else {
    53 		SDL_OutOfMemory();
    54 	}
    55 	return(mutex);
    56 }
    57 
    58 /* Free the mutex */
    59 void SDL_DestroyMutex(SDL_mutex *mutex)
    60 {
    61 	if ( mutex ) {
    62 		if ( mutex->id ) {
    63 			CloseHandle(mutex->id);
    64 			mutex->id = 0;
    65 		}
    66 		free(mutex);
    67 	}
    68 }
    69 
    70 /* Lock the mutex */
    71 int SDL_mutexP(SDL_mutex *mutex)
    72 {
    73 	if ( mutex == NULL ) {
    74 		SDL_SetError("Passed a NULL mutex");
    75 		return -1;
    76 	}
    77 	if ( WaitForSingleObject(mutex->id, INFINITE) == WAIT_FAILED ) {
    78 		SDL_SetError("Couldn't wait on mutex");
    79 		return -1;
    80 	}
    81 	return(0);
    82 }
    83 
    84 /* Unlock the mutex */
    85 int SDL_mutexV(SDL_mutex *mutex)
    86 {
    87 	if ( mutex == NULL ) {
    88 		SDL_SetError("Passed a NULL mutex");
    89 		return -1;
    90 	}
    91 	if ( ReleaseMutex(mutex->id) == FALSE ) {
    92 		SDL_SetError("Couldn't release mutex");
    93 		return -1;
    94 	}
    95 	return(0);
    96 }