src/thread/windows/SDL_sysmutex.c
author Philipp Wiesemann <philipp.wiesemann@arcor.de>
Sun, 08 Jun 2014 12:51:02 +0200
changeset 8844 d35c1b1798c8
parent 8149 681eb46b8ac4
child 9619 b94b6d0bff0f
permissions -rw-r--r--
Fixed typo in source comment.
slouken@0
     1
/*
slouken@5535
     2
  Simple DirectMedia Layer
slouken@8149
     3
  Copyright (C) 1997-2014 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 */
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@6977
    67
SDL_LockMutex(SDL_mutex * mutex)
slouken@0
    68
{
slouken@1895
    69
    if (mutex == NULL) {
icculus@7037
    70
        return SDL_SetError("Passed a NULL mutex");
slouken@1895
    71
    }
slouken@5335
    72
slouken@5335
    73
    EnterCriticalSection(&mutex->cs);
slouken@1895
    74
    return (0);
slouken@0
    75
}
slouken@0
    76
urkle@6966
    77
/* TryLock the mutex */
urkle@6966
    78
int
urkle@6966
    79
SDL_TryLockMutex(SDL_mutex * mutex)
urkle@6966
    80
{
urkle@6969
    81
    int retval = 0;
urkle@6966
    82
    if (mutex == NULL) {
icculus@7037
    83
        return SDL_SetError("Passed a NULL mutex");
urkle@6966
    84
    }
urkle@6966
    85
urkle@6966
    86
    if (TryEnterCriticalSection(&mutex->cs) == 0) {
urkle@6966
    87
        retval = SDL_MUTEX_TIMEDOUT;
urkle@6966
    88
    }
urkle@6966
    89
    return retval;
urkle@6966
    90
}
urkle@6966
    91
slouken@0
    92
/* Unlock the mutex */
slouken@1895
    93
int
slouken@6977
    94
SDL_UnlockMutex(SDL_mutex * mutex)
slouken@0
    95
{
slouken@1895
    96
    if (mutex == NULL) {
icculus@7037
    97
        return SDL_SetError("Passed a NULL mutex");
slouken@1895
    98
    }
slouken@5335
    99
slouken@5335
   100
    LeaveCriticalSection(&mutex->cs);
slouken@1895
   101
    return (0);
slouken@0
   102
}
slouken@1895
   103
slouken@6044
   104
#endif /* SDL_THREAD_WINDOWS */
slouken@6044
   105
slouken@1895
   106
/* vi: set ts=4 sw=4 expandtab: */