src/thread/windows/SDL_sysmutex.c
author Sam Lantinga <slouken@libsdl.org>
Thu, 07 Mar 2013 20:12:40 -0800
changeset 6977 b73d51026c68
parent 6969 74cf567e8618
child 7037 3fedf1f25b94
permissions -rw-r--r--
Changed the name of SDL_mutexP() SDL_mutexV()
     1 /*
     2   Simple DirectMedia Layer
     3   Copyright (C) 1997-2013 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         /* On SMP systems, a non-zero spin count generally helps performance */
    48         InitializeCriticalSectionAndSpinCount(&mutex->cs, 2000);
    49     } else {
    50         SDL_OutOfMemory();
    51     }
    52     return (mutex);
    53 }
    54 
    55 /* Free the mutex */
    56 void
    57 SDL_DestroyMutex(SDL_mutex * mutex)
    58 {
    59     if (mutex) {
    60         DeleteCriticalSection(&mutex->cs);
    61         SDL_free(mutex);
    62     }
    63 }
    64 
    65 /* Lock the mutex */
    66 int
    67 SDL_LockMutex(SDL_mutex * mutex)
    68 {
    69     if (mutex == NULL) {
    70         SDL_SetError("Passed a NULL mutex");
    71         return -1;
    72     }
    73 
    74     EnterCriticalSection(&mutex->cs);
    75     return (0);
    76 }
    77 
    78 /* TryLock the mutex */
    79 int
    80 SDL_TryLockMutex(SDL_mutex * mutex)
    81 {
    82     int retval = 0;
    83     if (mutex == NULL) {
    84         SDL_SetError("Passed a NULL mutex");
    85         return -1;
    86     }
    87 
    88     if (TryEnterCriticalSection(&mutex->cs) == 0) {
    89         retval = SDL_MUTEX_TIMEDOUT;
    90     }
    91     return retval;
    92 }
    93 
    94 /* Unlock the mutex */
    95 int
    96 SDL_UnlockMutex(SDL_mutex * mutex)
    97 {
    98     if (mutex == NULL) {
    99         SDL_SetError("Passed a NULL mutex");
   100         return -1;
   101     }
   102 
   103     LeaveCriticalSection(&mutex->cs);
   104     return (0);
   105 }
   106 
   107 #endif /* SDL_THREAD_WINDOWS */
   108 
   109 /* vi: set ts=4 sw=4 expandtab: */