src/thread/windows/SDL_sysmutex.c
author Edward Rudd <urkle@outoforder.cc>
Wed, 06 Mar 2013 10:37:27 -0500
changeset 6969 74cf567e8618
parent 6966 090df160f6e0
child 6977 b73d51026c68
permissions -rw-r--r--
move variable declaration to beginning of function
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
     1
/*
5535
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5336
diff changeset
     2
  Simple DirectMedia Layer
6885
700f1b25f77f Happy New Year!
Sam Lantinga <slouken@libsdl.org>
parents: 6430
diff changeset
     3
  Copyright (C) 1997-2013 Sam Lantinga <slouken@libsdl.org>
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
     4
5535
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5336
diff changeset
     5
  This software is provided 'as-is', without any express or implied
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5336
diff changeset
     6
  warranty.  In no event will the authors be held liable for any damages
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5336
diff changeset
     7
  arising from the use of this software.
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
     8
5535
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5336
diff changeset
     9
  Permission is granted to anyone to use this software for any purpose,
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5336
diff changeset
    10
  including commercial applications, and to alter it and redistribute it
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5336
diff changeset
    11
  freely, subject to the following restrictions:
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    12
5535
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5336
diff changeset
    13
  1. The origin of this software must not be misrepresented; you must not
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5336
diff changeset
    14
     claim that you wrote the original software. If you use this software
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5336
diff changeset
    15
     in a product, an acknowledgment in the product documentation would be
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5336
diff changeset
    16
     appreciated but is not required.
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5336
diff changeset
    17
  2. Altered source versions must be plainly marked as such, and must not be
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5336
diff changeset
    18
     misrepresented as being the original software.
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5336
diff changeset
    19
  3. This notice may not be removed or altered from any source distribution.
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    20
*/
1402
d910939febfa Use consistent identifiers for the various platforms we support.
Sam Lantinga <slouken@libsdl.org>
parents: 1358
diff changeset
    21
#include "SDL_config.h"
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    22
6044
35448a5ea044 Lots of fixes importing SDL source wholesale into a new iOS project
Sam Lantinga <slouken@libsdl.org>
parents: 5535
diff changeset
    23
#if SDL_THREAD_WINDOWS
35448a5ea044 Lots of fixes importing SDL source wholesale into a new iOS project
Sam Lantinga <slouken@libsdl.org>
parents: 5535
diff changeset
    24
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    25
/* Mutex functions using the Win32 API */
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    26
5090
327f181542f1 Include windows.h in a single point in the source, so we can be consistent about the definition of UNICODE and have core utility functions for Windows that all modules can share.
Sam Lantinga <slouken@libsdl.org>
parents: 5062
diff changeset
    27
#include "../../core/windows/SDL_windows.h"
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    28
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    29
#include "SDL_mutex.h"
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    30
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    31
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1433
diff changeset
    32
struct SDL_mutex
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1433
diff changeset
    33
{
5335
2bb1233924f1 Fixed bug 1128
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
    34
    CRITICAL_SECTION cs;
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    35
};
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    36
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    37
/* Create a mutex */
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1433
diff changeset
    38
SDL_mutex *
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1433
diff changeset
    39
SDL_CreateMutex(void)
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    40
{
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1433
diff changeset
    41
    SDL_mutex *mutex;
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    42
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1433
diff changeset
    43
    /* Allocate mutex memory */
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1433
diff changeset
    44
    mutex = (SDL_mutex *) SDL_malloc(sizeof(*mutex));
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1433
diff changeset
    45
    if (mutex) {
5335
2bb1233924f1 Fixed bug 1128
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
    46
        /* Initialize */
2bb1233924f1 Fixed bug 1128
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
    47
        /* On SMP systems, a non-zero spin count generally helps performance */
5336
66d3c4a6d6f8 Our minimum Windows version is Windows 2000, so it's okay to directly call InitializeCriticalSectionAndSpinCount()..
Sam Lantinga <slouken@libsdl.org>
parents: 5335
diff changeset
    48
        InitializeCriticalSectionAndSpinCount(&mutex->cs, 2000);
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1433
diff changeset
    49
    } else {
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1433
diff changeset
    50
        SDL_OutOfMemory();
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1433
diff changeset
    51
    }
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1433
diff changeset
    52
    return (mutex);
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    53
}
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    54
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    55
/* Free the mutex */
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1433
diff changeset
    56
void
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1433
diff changeset
    57
SDL_DestroyMutex(SDL_mutex * mutex)
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    58
{
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1433
diff changeset
    59
    if (mutex) {
5335
2bb1233924f1 Fixed bug 1128
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
    60
        DeleteCriticalSection(&mutex->cs);
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1433
diff changeset
    61
        SDL_free(mutex);
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1433
diff changeset
    62
    }
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    63
}
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    64
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    65
/* Lock the mutex */
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1433
diff changeset
    66
int
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1433
diff changeset
    67
SDL_mutexP(SDL_mutex * mutex)
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    68
{
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1433
diff changeset
    69
    if (mutex == NULL) {
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1433
diff changeset
    70
        SDL_SetError("Passed a NULL mutex");
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1433
diff changeset
    71
        return -1;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1433
diff changeset
    72
    }
5335
2bb1233924f1 Fixed bug 1128
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
    73
2bb1233924f1 Fixed bug 1128
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
    74
    EnterCriticalSection(&mutex->cs);
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1433
diff changeset
    75
    return (0);
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    76
}
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    77
6966
090df160f6e0 Add SDL_TryLockMutex and implementations for all platforms
Edward Rudd <urkle@outoforder.cc>
parents: 6885
diff changeset
    78
/* TryLock the mutex */
090df160f6e0 Add SDL_TryLockMutex and implementations for all platforms
Edward Rudd <urkle@outoforder.cc>
parents: 6885
diff changeset
    79
int
090df160f6e0 Add SDL_TryLockMutex and implementations for all platforms
Edward Rudd <urkle@outoforder.cc>
parents: 6885
diff changeset
    80
SDL_TryLockMutex(SDL_mutex * mutex)
090df160f6e0 Add SDL_TryLockMutex and implementations for all platforms
Edward Rudd <urkle@outoforder.cc>
parents: 6885
diff changeset
    81
{
6969
74cf567e8618 move variable declaration to beginning of function
Edward Rudd <urkle@outoforder.cc>
parents: 6966
diff changeset
    82
    int retval = 0;
6966
090df160f6e0 Add SDL_TryLockMutex and implementations for all platforms
Edward Rudd <urkle@outoforder.cc>
parents: 6885
diff changeset
    83
    if (mutex == NULL) {
090df160f6e0 Add SDL_TryLockMutex and implementations for all platforms
Edward Rudd <urkle@outoforder.cc>
parents: 6885
diff changeset
    84
        SDL_SetError("Passed a NULL mutex");
090df160f6e0 Add SDL_TryLockMutex and implementations for all platforms
Edward Rudd <urkle@outoforder.cc>
parents: 6885
diff changeset
    85
        return -1;
090df160f6e0 Add SDL_TryLockMutex and implementations for all platforms
Edward Rudd <urkle@outoforder.cc>
parents: 6885
diff changeset
    86
    }
090df160f6e0 Add SDL_TryLockMutex and implementations for all platforms
Edward Rudd <urkle@outoforder.cc>
parents: 6885
diff changeset
    87
090df160f6e0 Add SDL_TryLockMutex and implementations for all platforms
Edward Rudd <urkle@outoforder.cc>
parents: 6885
diff changeset
    88
    if (TryEnterCriticalSection(&mutex->cs) == 0) {
090df160f6e0 Add SDL_TryLockMutex and implementations for all platforms
Edward Rudd <urkle@outoforder.cc>
parents: 6885
diff changeset
    89
        retval = SDL_MUTEX_TIMEDOUT;
090df160f6e0 Add SDL_TryLockMutex and implementations for all platforms
Edward Rudd <urkle@outoforder.cc>
parents: 6885
diff changeset
    90
    }
090df160f6e0 Add SDL_TryLockMutex and implementations for all platforms
Edward Rudd <urkle@outoforder.cc>
parents: 6885
diff changeset
    91
    return retval;
090df160f6e0 Add SDL_TryLockMutex and implementations for all platforms
Edward Rudd <urkle@outoforder.cc>
parents: 6885
diff changeset
    92
}
090df160f6e0 Add SDL_TryLockMutex and implementations for all platforms
Edward Rudd <urkle@outoforder.cc>
parents: 6885
diff changeset
    93
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    94
/* Unlock the mutex */
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1433
diff changeset
    95
int
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1433
diff changeset
    96
SDL_mutexV(SDL_mutex * mutex)
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    97
{
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1433
diff changeset
    98
    if (mutex == NULL) {
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1433
diff changeset
    99
        SDL_SetError("Passed a NULL mutex");
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1433
diff changeset
   100
        return -1;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1433
diff changeset
   101
    }
5335
2bb1233924f1 Fixed bug 1128
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   102
2bb1233924f1 Fixed bug 1128
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   103
    LeaveCriticalSection(&mutex->cs);
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1433
diff changeset
   104
    return (0);
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   105
}
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1433
diff changeset
   106
6044
35448a5ea044 Lots of fixes importing SDL source wholesale into a new iOS project
Sam Lantinga <slouken@libsdl.org>
parents: 5535
diff changeset
   107
#endif /* SDL_THREAD_WINDOWS */
35448a5ea044 Lots of fixes importing SDL source wholesale into a new iOS project
Sam Lantinga <slouken@libsdl.org>
parents: 5535
diff changeset
   108
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1433
diff changeset
   109
/* vi: set ts=4 sw=4 expandtab: */