src/thread/windows/SDL_sysmutex.c
author Sam Lantinga <slouken@libsdl.org>
Mon, 31 Oct 2011 05:56:58 -0400
changeset 6044 35448a5ea044
parent 5535 96594ac5fd1a
child 6138 4c64952a58fb
permissions -rw-r--r--
Lots of fixes importing SDL source wholesale into a new iOS project
slouken@0
     1
/*
slouken@5535
     2
  Simple DirectMedia Layer
slouken@5535
     3
  Copyright (C) 1997-2011 Sam Lantinga <slouken@libsdl.org>
slouken@0
     4
slouken@5535
     5
  This software is provided 'as-is', without any express or implied
slouken@5535
     6
  warranty.  In no event will the authors be held liable for any damages
slouken@5535
     7
  arising from the use of this software.
slouken@0
     8
slouken@5535
     9
  Permission is granted to anyone to use this software for any purpose,
slouken@5535
    10
  including commercial applications, and to alter it and redistribute it
slouken@5535
    11
  freely, subject to the following restrictions:
slouken@0
    12
slouken@5535
    13
  1. The origin of this software must not be misrepresented; you must not
slouken@5535
    14
     claim that you wrote the original software. If you use this software
slouken@5535
    15
     in a product, an acknowledgment in the product documentation would be
slouken@5535
    16
     appreciated but is not required.
slouken@5535
    17
  2. Altered source versions must be plainly marked as such, and must not be
slouken@5535
    18
     misrepresented as being the original software.
slouken@5535
    19
  3. This notice may not be removed or altered from any source distribution.
slouken@0
    20
*/
slouken@1402
    21
#include "SDL_config.h"
slouken@0
    22
slouken@6044
    23
#if SDL_THREAD_WINDOWS
slouken@6044
    24
slouken@0
    25
/* Mutex functions using the Win32 API */
slouken@0
    26
slouken@5090
    27
#include "../../core/windows/SDL_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@5335
    34
    CRITICAL_SECTION cs;
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@5335
    46
        /* Initialize */
slouken@5336
    47
#ifdef _WIN32_WCE
slouken@5335
    48
        InitializeCriticalSection(&mutex->cs);
slouken@5336
    49
#else
slouken@5335
    50
        /* On SMP systems, a non-zero spin count generally helps performance */
slouken@5336
    51
        InitializeCriticalSectionAndSpinCount(&mutex->cs, 2000);
slouken@5336
    52
#endif
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@5335
    64
        DeleteCriticalSection(&mutex->cs);
slouken@1895
    65
        SDL_free(mutex);
slouken@1895
    66
    }
slouken@0
    67
}
slouken@0
    68
slouken@0
    69
/* Lock the mutex */
slouken@1895
    70
int
slouken@1895
    71
SDL_mutexP(SDL_mutex * mutex)
slouken@0
    72
{
slouken@1895
    73
    if (mutex == NULL) {
slouken@1895
    74
        SDL_SetError("Passed a NULL mutex");
slouken@1895
    75
        return -1;
slouken@1895
    76
    }
slouken@5335
    77
slouken@5335
    78
    EnterCriticalSection(&mutex->cs);
slouken@1895
    79
    return (0);
slouken@0
    80
}
slouken@0
    81
slouken@0
    82
/* Unlock the mutex */
slouken@1895
    83
int
slouken@1895
    84
SDL_mutexV(SDL_mutex * mutex)
slouken@0
    85
{
slouken@1895
    86
    if (mutex == NULL) {
slouken@1895
    87
        SDL_SetError("Passed a NULL mutex");
slouken@1895
    88
        return -1;
slouken@1895
    89
    }
slouken@5335
    90
slouken@5335
    91
    LeaveCriticalSection(&mutex->cs);
slouken@1895
    92
    return (0);
slouken@0
    93
}
slouken@1895
    94
slouken@6044
    95
#endif /* SDL_THREAD_WINDOWS */
slouken@6044
    96
slouken@1895
    97
/* vi: set ts=4 sw=4 expandtab: */