cscg22-gearboy

CSCG 2022 Challenge 'Gearboy'
git clone https://git.sinitax.com/sinitax/cscg22-gearboy
Log | Files | Refs | sfeed.txt

SDL_sysmutex.c (2498B)


      1/*
      2  Simple DirectMedia Layer
      3  Copyright (C) 1997-2014 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_internal.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
     32struct SDL_mutex
     33{
     34    CRITICAL_SECTION cs;
     35};
     36
     37/* Create a mutex */
     38SDL_mutex *
     39SDL_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 */
     56void
     57SDL_DestroyMutex(SDL_mutex * mutex)
     58{
     59    if (mutex) {
     60        DeleteCriticalSection(&mutex->cs);
     61        SDL_free(mutex);
     62    }
     63}
     64
     65/* Lock the mutex */
     66int
     67SDL_LockMutex(SDL_mutex * mutex)
     68{
     69    if (mutex == NULL) {
     70        return SDL_SetError("Passed a NULL mutex");
     71    }
     72
     73    EnterCriticalSection(&mutex->cs);
     74    return (0);
     75}
     76
     77/* TryLock the mutex */
     78int
     79SDL_TryLockMutex(SDL_mutex * mutex)
     80{
     81    int retval = 0;
     82    if (mutex == NULL) {
     83        return SDL_SetError("Passed a NULL mutex");
     84    }
     85
     86    if (TryEnterCriticalSection(&mutex->cs) == 0) {
     87        retval = SDL_MUTEX_TIMEDOUT;
     88    }
     89    return retval;
     90}
     91
     92/* Unlock the mutex */
     93int
     94SDL_UnlockMutex(SDL_mutex * mutex)
     95{
     96    if (mutex == NULL) {
     97        return SDL_SetError("Passed a NULL mutex");
     98    }
     99
    100    LeaveCriticalSection(&mutex->cs);
    101    return (0);
    102}
    103
    104#endif /* SDL_THREAD_WINDOWS */
    105
    106/* vi: set ts=4 sw=4 expandtab: */