src/thread/windows/SDL_sysmutex.c
author Sam Lantinga <slouken@libsdl.org>
Sat, 02 Jan 2016 10:10:34 -0800
changeset 9998 f67cf37e9cd4
parent 9930 84285c1d8ca4
child 10737 3406a0f8b041
permissions -rw-r--r--
Updated copyright to 2016
slouken@0
     1
/*
slouken@5535
     2
  Simple DirectMedia Layer
slouken@9998
     3
  Copyright (C) 1997-2016 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
*/
icculus@8093
    21
#include "../../SDL_internal.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 */
dludwig@9930
    48
#if __WINRT__
dludwig@9930
    49
        InitializeCriticalSectionEx(&mutex->cs, 2000, 0);
dludwig@9930
    50
#else
slouken@5336
    51
        InitializeCriticalSectionAndSpinCount(&mutex->cs, 2000);
dludwig@9930
    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@6977
    71
SDL_LockMutex(SDL_mutex * mutex)
slouken@0
    72
{
slouken@1895
    73
    if (mutex == NULL) {
icculus@7037
    74
        return SDL_SetError("Passed a NULL mutex");
slouken@1895
    75
    }
slouken@5335
    76
slouken@5335
    77
    EnterCriticalSection(&mutex->cs);
slouken@1895
    78
    return (0);
slouken@0
    79
}
slouken@0
    80
urkle@6966
    81
/* TryLock the mutex */
urkle@6966
    82
int
urkle@6966
    83
SDL_TryLockMutex(SDL_mutex * mutex)
urkle@6966
    84
{
urkle@6969
    85
    int retval = 0;
urkle@6966
    86
    if (mutex == NULL) {
icculus@7037
    87
        return SDL_SetError("Passed a NULL mutex");
urkle@6966
    88
    }
urkle@6966
    89
urkle@6966
    90
    if (TryEnterCriticalSection(&mutex->cs) == 0) {
urkle@6966
    91
        retval = SDL_MUTEX_TIMEDOUT;
urkle@6966
    92
    }
urkle@6966
    93
    return retval;
urkle@6966
    94
}
urkle@6966
    95
slouken@0
    96
/* Unlock the mutex */
slouken@1895
    97
int
slouken@6977
    98
SDL_UnlockMutex(SDL_mutex * mutex)
slouken@0
    99
{
slouken@1895
   100
    if (mutex == NULL) {
icculus@7037
   101
        return SDL_SetError("Passed a NULL mutex");
slouken@1895
   102
    }
slouken@5335
   103
slouken@5335
   104
    LeaveCriticalSection(&mutex->cs);
slouken@1895
   105
    return (0);
slouken@0
   106
}
slouken@1895
   107
slouken@6044
   108
#endif /* SDL_THREAD_WINDOWS */
slouken@6044
   109
slouken@1895
   110
/* vi: set ts=4 sw=4 expandtab: */