src/thread/win32/SDL_sysmutex.c
author Sam Lantinga <slouken@libsdl.org>
Sun, 24 Jan 2010 21:10:53 +0000
changeset 3697 f7b03b6838cb
parent 2859 99210400e8b9
permissions -rw-r--r--
Fixed bug #926

Updated copyright to LGPL version 2.1 and year 2010
     1 /*
     2     SDL - Simple DirectMedia Layer
     3     Copyright (C) 1997-2010 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 #define WIN32_LEAN_AND_MEAN
    27 #include <windows.h>
    28 
    29 #include "SDL_mutex.h"
    30 
    31 
    32 struct SDL_mutex
    33 {
    34     HANDLE id;
    35 };
    36 
    37 /* Create a mutex */
    38 SDL_mutex *
    39 SDL_CreateMutex(void)
    40 {
    41     SDL_mutex *mutex;
    42 
    43     /* Allocate mutex memory */
    44     mutex = (SDL_mutex *) SDL_malloc(sizeof(*mutex));
    45     if (mutex) {
    46         /* Create the mutex, with initial value signaled */
    47         mutex->id = CreateMutex(NULL, FALSE, NULL);
    48         if (!mutex->id) {
    49             SDL_SetError("Couldn't create mutex");
    50             SDL_free(mutex);
    51             mutex = NULL;
    52         }
    53     } else {
    54         SDL_OutOfMemory();
    55     }
    56     return (mutex);
    57 }
    58 
    59 /* Free the mutex */
    60 void
    61 SDL_DestroyMutex(SDL_mutex * mutex)
    62 {
    63     if (mutex) {
    64         if (mutex->id) {
    65             CloseHandle(mutex->id);
    66             mutex->id = 0;
    67         }
    68         SDL_free(mutex);
    69     }
    70 }
    71 
    72 /* Lock the mutex */
    73 int
    74 SDL_mutexP(SDL_mutex * mutex)
    75 {
    76     if (mutex == NULL) {
    77         SDL_SetError("Passed a NULL mutex");
    78         return -1;
    79     }
    80     if (WaitForSingleObject(mutex->id, INFINITE) == WAIT_FAILED) {
    81         SDL_SetError("Couldn't wait on mutex");
    82         return -1;
    83     }
    84     return (0);
    85 }
    86 
    87 /* Unlock the mutex */
    88 int
    89 SDL_mutexV(SDL_mutex * mutex)
    90 {
    91     if (mutex == NULL) {
    92         SDL_SetError("Passed a NULL mutex");
    93         return -1;
    94     }
    95     if (ReleaseMutex(mutex->id) == FALSE) {
    96         SDL_SetError("Couldn't release mutex");
    97         return -1;
    98     }
    99     return (0);
   100 }
   101 
   102 /* vi: set ts=4 sw=4 expandtab: */