src/thread/windows/SDL_sysmutex.c
author Sam Lantinga <slouken@libsdl.org>
Fri, 08 Apr 2011 13:03:26 -0700
changeset 5535 96594ac5fd1a
parent 5336 66d3c4a6d6f8
child 6044 35448a5ea044
permissions -rw-r--r--
SDL 1.3 is now under the zlib license.
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@0
    23
/* Mutex functions using the Win32 API */
slouken@0
    24
slouken@5090
    25
#include "../../core/windows/SDL_windows.h"
slouken@0
    26
slouken@0
    27
#include "SDL_mutex.h"
slouken@0
    28
slouken@0
    29
slouken@1895
    30
struct SDL_mutex
slouken@1895
    31
{
slouken@5335
    32
    CRITICAL_SECTION cs;
slouken@0
    33
};
slouken@0
    34
slouken@0
    35
/* Create a mutex */
slouken@1895
    36
SDL_mutex *
slouken@1895
    37
SDL_CreateMutex(void)
slouken@0
    38
{
slouken@1895
    39
    SDL_mutex *mutex;
slouken@0
    40
slouken@1895
    41
    /* Allocate mutex memory */
slouken@1895
    42
    mutex = (SDL_mutex *) SDL_malloc(sizeof(*mutex));
slouken@1895
    43
    if (mutex) {
slouken@5335
    44
        /* Initialize */
slouken@5336
    45
#ifdef _WIN32_WCE
slouken@5335
    46
        InitializeCriticalSection(&mutex->cs);
slouken@5336
    47
#else
slouken@5335
    48
        /* On SMP systems, a non-zero spin count generally helps performance */
slouken@5336
    49
        InitializeCriticalSectionAndSpinCount(&mutex->cs, 2000);
slouken@5336
    50
#endif
slouken@1895
    51
    } else {
slouken@1895
    52
        SDL_OutOfMemory();
slouken@1895
    53
    }
slouken@1895
    54
    return (mutex);
slouken@0
    55
}
slouken@0
    56
slouken@0
    57
/* Free the mutex */
slouken@1895
    58
void
slouken@1895
    59
SDL_DestroyMutex(SDL_mutex * mutex)
slouken@0
    60
{
slouken@1895
    61
    if (mutex) {
slouken@5335
    62
        DeleteCriticalSection(&mutex->cs);
slouken@1895
    63
        SDL_free(mutex);
slouken@1895
    64
    }
slouken@0
    65
}
slouken@0
    66
slouken@0
    67
/* Lock the mutex */
slouken@1895
    68
int
slouken@1895
    69
SDL_mutexP(SDL_mutex * mutex)
slouken@0
    70
{
slouken@1895
    71
    if (mutex == NULL) {
slouken@1895
    72
        SDL_SetError("Passed a NULL mutex");
slouken@1895
    73
        return -1;
slouken@1895
    74
    }
slouken@5335
    75
slouken@5335
    76
    EnterCriticalSection(&mutex->cs);
slouken@1895
    77
    return (0);
slouken@0
    78
}
slouken@0
    79
slouken@0
    80
/* Unlock the mutex */
slouken@1895
    81
int
slouken@1895
    82
SDL_mutexV(SDL_mutex * mutex)
slouken@0
    83
{
slouken@1895
    84
    if (mutex == NULL) {
slouken@1895
    85
        SDL_SetError("Passed a NULL mutex");
slouken@1895
    86
        return -1;
slouken@1895
    87
    }
slouken@5335
    88
slouken@5335
    89
    LeaveCriticalSection(&mutex->cs);
slouken@1895
    90
    return (0);
slouken@0
    91
}
slouken@1895
    92
slouken@1895
    93
/* vi: set ts=4 sw=4 expandtab: */