src/thread/windows/SDL_sysmutex.c
author Sam Lantinga <slouken@libsdl.org>
Fri, 15 Feb 2013 08:47:44 -0800
changeset 6885 700f1b25f77f
parent 6430 48d519500f7e
child 6966 090df160f6e0
permissions -rw-r--r--
Happy New Year!
slouken@0
     1
/*
slouken@5535
     2
  Simple DirectMedia Layer
slouken@6885
     3
  Copyright (C) 1997-2013 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@5335
    47
        /* On SMP systems, a non-zero spin count generally helps performance */
slouken@5336
    48
        InitializeCriticalSectionAndSpinCount(&mutex->cs, 2000);
slouken@1895
    49
    } else {
slouken@1895
    50
        SDL_OutOfMemory();
slouken@1895
    51
    }
slouken@1895
    52
    return (mutex);
slouken@0
    53
}
slouken@0
    54
slouken@0
    55
/* Free the mutex */
slouken@1895
    56
void
slouken@1895
    57
SDL_DestroyMutex(SDL_mutex * mutex)
slouken@0
    58
{
slouken@1895
    59
    if (mutex) {
slouken@5335
    60
        DeleteCriticalSection(&mutex->cs);
slouken@1895
    61
        SDL_free(mutex);
slouken@1895
    62
    }
slouken@0
    63
}
slouken@0
    64
slouken@0
    65
/* Lock the mutex */
slouken@1895
    66
int
slouken@1895
    67
SDL_mutexP(SDL_mutex * mutex)
slouken@0
    68
{
slouken@1895
    69
    if (mutex == NULL) {
slouken@1895
    70
        SDL_SetError("Passed a NULL mutex");
slouken@1895
    71
        return -1;
slouken@1895
    72
    }
slouken@5335
    73
slouken@5335
    74
    EnterCriticalSection(&mutex->cs);
slouken@1895
    75
    return (0);
slouken@0
    76
}
slouken@0
    77
slouken@0
    78
/* Unlock the mutex */
slouken@1895
    79
int
slouken@1895
    80
SDL_mutexV(SDL_mutex * mutex)
slouken@0
    81
{
slouken@1895
    82
    if (mutex == NULL) {
slouken@1895
    83
        SDL_SetError("Passed a NULL mutex");
slouken@1895
    84
        return -1;
slouken@1895
    85
    }
slouken@5335
    86
slouken@5335
    87
    LeaveCriticalSection(&mutex->cs);
slouken@1895
    88
    return (0);
slouken@0
    89
}
slouken@1895
    90
slouken@6044
    91
#endif /* SDL_THREAD_WINDOWS */
slouken@6044
    92
slouken@1895
    93
/* vi: set ts=4 sw=4 expandtab: */