src/thread/windows/SDL_sysmutex.c
author Sam Lantinga <slouken@libsdl.org>
Mon, 24 Jan 2011 21:20:30 -0800
changeset 5090 327f181542f1
parent 5062 e8916fe9cfc8
child 5262 b530ef003506
permissions -rw-r--r--
Include windows.h in a single point in the source, so we can be consistent about the definition of UNICODE and have core utility functions for Windows that all modules can share.

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