src/thread/windows/SDL_sysmutex.c
author Sam Lantinga <slouken@libsdl.org>
Thu, 17 Feb 2011 09:26:15 -0800
changeset 5336 66d3c4a6d6f8
parent 5335 2bb1233924f1
child 5535 96594ac5fd1a
permissions -rwxr-xr-x
Our minimum Windows version is Windows 2000, so it's okay to directly call InitializeCriticalSectionAndSpinCount()..
     1 /*
     2     SDL - Simple DirectMedia Layer
     3     Copyright (C) 1997-2011 Sam Lantinga
     4 
     5     This library is free software; you can redistribute it and/or
     6     modify it under the terms of the GNU Lesser General Public
     7     License as published by the Free Software Foundation; either
     8     version 2.1 of the License, or (at your option) any later version.
     9 
    10     This library is distributed in the hope that it will be useful,
    11     but WITHOUT ANY WARRANTY; without even the implied warranty of
    12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    13     Lesser General Public License for more details.
    14 
    15     You should have received a copy of the GNU Lesser General Public
    16     License along with this library; if not, write to the Free Software
    17     Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
    18 
    19     Sam Lantinga
    20     slouken@libsdl.org
    21 */
    22 #include "SDL_config.h"
    23 
    24 /* Mutex functions using the Win32 API */
    25 
    26 #include "../../core/windows/SDL_windows.h"
    27 
    28 #include "SDL_mutex.h"
    29 
    30 
    31 struct SDL_mutex
    32 {
    33     CRITICAL_SECTION cs;
    34 };
    35 
    36 /* Create a mutex */
    37 SDL_mutex *
    38 SDL_CreateMutex(void)
    39 {
    40     SDL_mutex *mutex;
    41 
    42     /* Allocate mutex memory */
    43     mutex = (SDL_mutex *) SDL_malloc(sizeof(*mutex));
    44     if (mutex) {
    45         /* Initialize */
    46 #ifdef _WIN32_WCE
    47         InitializeCriticalSection(&mutex->cs);
    48 #else
    49         /* On SMP systems, a non-zero spin count generally helps performance */
    50         InitializeCriticalSectionAndSpinCount(&mutex->cs, 2000);
    51 #endif
    52     } else {
    53         SDL_OutOfMemory();
    54     }
    55     return (mutex);
    56 }
    57 
    58 /* Free the mutex */
    59 void
    60 SDL_DestroyMutex(SDL_mutex * mutex)
    61 {
    62     if (mutex) {
    63         DeleteCriticalSection(&mutex->cs);
    64         SDL_free(mutex);
    65     }
    66 }
    67 
    68 /* Lock the mutex */
    69 int
    70 SDL_mutexP(SDL_mutex * mutex)
    71 {
    72     if (mutex == NULL) {
    73         SDL_SetError("Passed a NULL mutex");
    74         return -1;
    75     }
    76 
    77     EnterCriticalSection(&mutex->cs);
    78     return (0);
    79 }
    80 
    81 /* Unlock the mutex */
    82 int
    83 SDL_mutexV(SDL_mutex * mutex)
    84 {
    85     if (mutex == NULL) {
    86         SDL_SetError("Passed a NULL mutex");
    87         return -1;
    88     }
    89 
    90     LeaveCriticalSection(&mutex->cs);
    91     return (0);
    92 }
    93 
    94 /* vi: set ts=4 sw=4 expandtab: */