src/thread/windows/SDL_sysmutex.c
author Sam Lantinga <slouken@libsdl.org>
Sat, 31 Dec 2011 09:28:07 -0500
changeset 6138 4c64952a58fb
parent 6044 35448a5ea044
child 6430 48d519500f7e
permissions -rwxr-xr-x
Happy New Year!
     1 /*
     2   Simple DirectMedia Layer
     3   Copyright (C) 1997-2012 Sam Lantinga <slouken@libsdl.org>
     4 
     5   This software is provided 'as-is', without any express or implied
     6   warranty.  In no event will the authors be held liable for any damages
     7   arising from the use of this software.
     8 
     9   Permission is granted to anyone to use this software for any purpose,
    10   including commercial applications, and to alter it and redistribute it
    11   freely, subject to the following restrictions:
    12 
    13   1. The origin of this software must not be misrepresented; you must not
    14      claim that you wrote the original software. If you use this software
    15      in a product, an acknowledgment in the product documentation would be
    16      appreciated but is not required.
    17   2. Altered source versions must be plainly marked as such, and must not be
    18      misrepresented as being the original software.
    19   3. This notice may not be removed or altered from any source distribution.
    20 */
    21 #include "SDL_config.h"
    22 
    23 #if SDL_THREAD_WINDOWS
    24 
    25 /* Mutex functions using the Win32 API */
    26 
    27 #include "../../core/windows/SDL_windows.h"
    28 
    29 #include "SDL_mutex.h"
    30 
    31 
    32 struct SDL_mutex
    33 {
    34     CRITICAL_SECTION cs;
    35 };
    36 
    37 /* Create a mutex */
    38 SDL_mutex *
    39 SDL_CreateMutex(void)
    40 {
    41     SDL_mutex *mutex;
    42 
    43     /* Allocate mutex memory */
    44     mutex = (SDL_mutex *) SDL_malloc(sizeof(*mutex));
    45     if (mutex) {
    46         /* Initialize */
    47 #ifdef _WIN32_WCE
    48         InitializeCriticalSection(&mutex->cs);
    49 #else
    50         /* On SMP systems, a non-zero spin count generally helps performance */
    51         InitializeCriticalSectionAndSpinCount(&mutex->cs, 2000);
    52 #endif
    53     } else {
    54         SDL_OutOfMemory();
    55     }
    56     return (mutex);
    57 }
    58 
    59 /* Free the mutex */
    60 void
    61 SDL_DestroyMutex(SDL_mutex * mutex)
    62 {
    63     if (mutex) {
    64         DeleteCriticalSection(&mutex->cs);
    65         SDL_free(mutex);
    66     }
    67 }
    68 
    69 /* Lock the mutex */
    70 int
    71 SDL_mutexP(SDL_mutex * mutex)
    72 {
    73     if (mutex == NULL) {
    74         SDL_SetError("Passed a NULL mutex");
    75         return -1;
    76     }
    77 
    78     EnterCriticalSection(&mutex->cs);
    79     return (0);
    80 }
    81 
    82 /* Unlock the mutex */
    83 int
    84 SDL_mutexV(SDL_mutex * mutex)
    85 {
    86     if (mutex == NULL) {
    87         SDL_SetError("Passed a NULL mutex");
    88         return -1;
    89     }
    90 
    91     LeaveCriticalSection(&mutex->cs);
    92     return (0);
    93 }
    94 
    95 #endif /* SDL_THREAD_WINDOWS */
    96 
    97 /* vi: set ts=4 sw=4 expandtab: */