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