src/thread/windows/SDL_sysmutex.c
author Sam Lantinga <slouken@libsdl.org>
Fri, 11 Feb 2011 22:37:15 -0800
changeset 5262 b530ef003506
parent 5090 327f181542f1
child 5335 2bb1233924f1
permissions -rw-r--r--
Happy 2011! :)
     1 /*
     2     SDL - Simple DirectMedia Layer
     3     Copyright (C) 1997-2011 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 "../../core/windows/SDL_windows.h"
    27 
    28 #include "SDL_mutex.h"
    29 
    30 
    31 struct SDL_mutex
    32 {
    33     HANDLE id;
    34 };
    35 
    36 /* Create a mutex */
    37 SDL_mutex *
    38 SDL_CreateMutex(void)
    39 {
    40     SDL_mutex *mutex;
    41 
    42     /* Allocate mutex memory */
    43     mutex = (SDL_mutex *) SDL_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             SDL_free(mutex);
    50             mutex = NULL;
    51         }
    52     } else {
    53         SDL_OutOfMemory();
    54     }
    55     return (mutex);
    56 }
    57 
    58 /* Free the mutex */
    59 void
    60 SDL_DestroyMutex(SDL_mutex * mutex)
    61 {
    62     if (mutex) {
    63         if (mutex->id) {
    64             CloseHandle(mutex->id);
    65             mutex->id = 0;
    66         }
    67         SDL_free(mutex);
    68     }
    69 }
    70 
    71 /* Lock the mutex */
    72 int
    73 SDL_mutexP(SDL_mutex * mutex)
    74 {
    75     if (mutex == NULL) {
    76         SDL_SetError("Passed a NULL mutex");
    77         return -1;
    78     }
    79     if (WaitForSingleObject(mutex->id, INFINITE) == WAIT_FAILED) {
    80         SDL_SetError("Couldn't wait on mutex");
    81         return -1;
    82     }
    83     return (0);
    84 }
    85 
    86 /* Unlock the mutex */
    87 int
    88 SDL_mutexV(SDL_mutex * mutex)
    89 {
    90     if (mutex == NULL) {
    91         SDL_SetError("Passed a NULL mutex");
    92         return -1;
    93     }
    94     if (ReleaseMutex(mutex->id) == FALSE) {
    95         SDL_SetError("Couldn't release mutex");
    96         return -1;
    97     }
    98     return (0);
    99 }
   100 
   101 /* vi: set ts=4 sw=4 expandtab: */