src/thread/win32/SDL_sysmutex.c
author Sam Lantinga <slouken@libsdl.org>
Mon, 29 May 2006 04:04:35 +0000
branchSDL-1.3
changeset 1668 4da1ee79c9af
parent 1662 782fd950bd46
permissions -rw-r--r--
more tweaking indent options
slouken@0
     1
/*
slouken@0
     2
    SDL - Simple DirectMedia Layer
slouken@1312
     3
    Copyright (C) 1997-2006 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@1662
    32
struct SDL_mutex
slouken@1662
    33
{
slouken@1662
    34
    HANDLE id;
slouken@0
    35
};
slouken@0
    36
slouken@0
    37
/* Create a mutex */
slouken@1662
    38
SDL_mutex *
slouken@1668
    39
SDL_CreateMutex(void)
slouken@0
    40
{
slouken@1662
    41
    SDL_mutex *mutex;
slouken@0
    42
slouken@1662
    43
    /* Allocate mutex memory */
slouken@1668
    44
    mutex = (SDL_mutex *) SDL_malloc(sizeof(*mutex));
slouken@1662
    45
    if (mutex) {
slouken@1662
    46
        /* Create the mutex, with initial value signaled */
slouken@1668
    47
        mutex->id = CreateMutex(NULL, FALSE, NULL);
slouken@1662
    48
        if (!mutex->id) {
slouken@1668
    49
            SDL_SetError("Couldn't create mutex");
slouken@1668
    50
            SDL_free(mutex);
slouken@1662
    51
            mutex = NULL;
slouken@1662
    52
        }
slouken@1662
    53
    } else {
slouken@1668
    54
        SDL_OutOfMemory();
slouken@1662
    55
    }
slouken@1662
    56
    return (mutex);
slouken@0
    57
}
slouken@0
    58
slouken@0
    59
/* Free the mutex */
slouken@1662
    60
void
slouken@1668
    61
SDL_DestroyMutex(SDL_mutex * mutex)
slouken@0
    62
{
slouken@1662
    63
    if (mutex) {
slouken@1662
    64
        if (mutex->id) {
slouken@1668
    65
            CloseHandle(mutex->id);
slouken@1662
    66
            mutex->id = 0;
slouken@1662
    67
        }
slouken@1668
    68
        SDL_free(mutex);
slouken@1662
    69
    }
slouken@0
    70
}
slouken@0
    71
slouken@0
    72
/* Lock the mutex */
slouken@1662
    73
int
slouken@1668
    74
SDL_mutexP(SDL_mutex * mutex)
slouken@0
    75
{
slouken@1662
    76
    if (mutex == NULL) {
slouken@1668
    77
        SDL_SetError("Passed a NULL mutex");
slouken@1662
    78
        return -1;
slouken@1662
    79
    }
slouken@1668
    80
    if (WaitForSingleObject(mutex->id, INFINITE) == WAIT_FAILED) {
slouken@1668
    81
        SDL_SetError("Couldn't wait on mutex");
slouken@1662
    82
        return -1;
slouken@1662
    83
    }
slouken@1662
    84
    return (0);
slouken@0
    85
}
slouken@0
    86
slouken@0
    87
/* Unlock the mutex */
slouken@1662
    88
int
slouken@1668
    89
SDL_mutexV(SDL_mutex * mutex)
slouken@0
    90
{
slouken@1662
    91
    if (mutex == NULL) {
slouken@1668
    92
        SDL_SetError("Passed a NULL mutex");
slouken@1662
    93
        return -1;
slouken@1662
    94
    }
slouken@1668
    95
    if (ReleaseMutex(mutex->id) == FALSE) {
slouken@1668
    96
        SDL_SetError("Couldn't release mutex");
slouken@1662
    97
        return -1;
slouken@1662
    98
    }
slouken@1662
    99
    return (0);
slouken@0
   100
}
slouken@1662
   101
slouken@1662
   102
/* vi: set ts=4 sw=4 expandtab: */