src/thread/stdcpp/SDL_sysmutex.cpp
author Sam Lantinga <slouken@libsdl.org>
Tue, 26 May 2015 06:27:46 -0700
changeset 9619 b94b6d0bff0f
parent 8615 097646deaef2
child 9998 f67cf37e9cd4
permissions -rw-r--r--
Updated the copyright year to 2015
dludwig@8356
     1
/*
dludwig@8356
     2
  Simple DirectMedia Layer
slouken@9619
     3
  Copyright (C) 1997-2015 Sam Lantinga <slouken@libsdl.org>
dludwig@8356
     4
dludwig@8356
     5
  This software is provided 'as-is', without any express or implied
dludwig@8356
     6
  warranty.  In no event will the authors be held liable for any damages
dludwig@8356
     7
  arising from the use of this software.
dludwig@8356
     8
dludwig@8356
     9
  Permission is granted to anyone to use this software for any purpose,
dludwig@8356
    10
  including commercial applications, and to alter it and redistribute it
dludwig@8356
    11
  freely, subject to the following restrictions:
dludwig@8356
    12
dludwig@8356
    13
  1. The origin of this software must not be misrepresented; you must not
dludwig@8356
    14
     claim that you wrote the original software. If you use this software
dludwig@8356
    15
     in a product, an acknowledgment in the product documentation would be
dludwig@8356
    16
     appreciated but is not required.
dludwig@8356
    17
  2. Altered source versions must be plainly marked as such, and must not be
dludwig@8356
    18
     misrepresented as being the original software.
dludwig@8356
    19
  3. This notice may not be removed or altered from any source distribution.
dludwig@8356
    20
*/
dludwig@8600
    21
#include "../../SDL_internal.h"
dludwig@8356
    22
dludwig@8360
    23
extern "C" {
dludwig@8356
    24
#include "SDL_thread.h"
dludwig@8356
    25
#include "SDL_systhread_c.h"
dludwig@8360
    26
#include "SDL_log.h"
dludwig@8360
    27
}
dludwig@8356
    28
dludwig@8484
    29
#include <system_error>
dludwig@8356
    30
dludwig@8360
    31
#include "SDL_sysmutex_c.h"
dludwig@8360
    32
#include <Windows.h>
dludwig@8360
    33
dludwig@8356
    34
dludwig@8356
    35
/* Create a mutex */
dludwig@8357
    36
extern "C"
dludwig@8356
    37
SDL_mutex *
dludwig@8356
    38
SDL_CreateMutex(void)
dludwig@8356
    39
{
dludwig@8360
    40
    /* Allocate and initialize the mutex */
dludwig@8360
    41
    try {
dludwig@8360
    42
        SDL_mutex * mutex = new SDL_mutex;
dludwig@8360
    43
        return mutex;
dludwig@8484
    44
    } catch (std::system_error & ex) {
dludwig@8484
    45
        SDL_SetError("unable to create a C++ mutex: code=%d; %s", ex.code(), ex.what());
dludwig@8360
    46
        return NULL;
dludwig@8484
    47
    } catch (std::bad_alloc &) {
dludwig@8484
    48
        SDL_OutOfMemory();
dludwig@8360
    49
        return NULL;
dludwig@8356
    50
    }
dludwig@8356
    51
}
dludwig@8356
    52
dludwig@8356
    53
/* Free the mutex */
dludwig@8357
    54
extern "C"
dludwig@8356
    55
void
dludwig@8356
    56
SDL_DestroyMutex(SDL_mutex * mutex)
dludwig@8356
    57
{
dludwig@8356
    58
    if (mutex) {
dludwig@8484
    59
        delete mutex;
dludwig@8356
    60
    }
dludwig@8356
    61
}
dludwig@8356
    62
dludwig@8356
    63
/* Lock the semaphore */
dludwig@8357
    64
extern "C"
dludwig@8356
    65
int
dludwig@8356
    66
SDL_mutexP(SDL_mutex * mutex)
dludwig@8356
    67
{
dludwig@8356
    68
    if (mutex == NULL) {
dludwig@8356
    69
        SDL_SetError("Passed a NULL mutex");
dludwig@8356
    70
        return -1;
dludwig@8356
    71
    }
dludwig@8356
    72
dludwig@8360
    73
    try {
dludwig@8360
    74
        mutex->cpp_mutex.lock();
dludwig@8360
    75
        return 0;
dludwig@8484
    76
    } catch (std::system_error & ex) {
dludwig@8484
    77
        SDL_SetError("unable to lock a C++ mutex: code=%d; %s", ex.code(), ex.what());
dludwig@8360
    78
        return -1;
dludwig@8356
    79
    }
dludwig@8356
    80
}
dludwig@8356
    81
dludwig@8611
    82
/* TryLock the mutex */
dludwig@8611
    83
int
dludwig@8611
    84
SDL_TryLockMutex(SDL_mutex * mutex)
dludwig@8611
    85
{
dludwig@8611
    86
    int retval = 0;
dludwig@8611
    87
    if (mutex == NULL) {
dludwig@8611
    88
        return SDL_SetError("Passed a NULL mutex");
dludwig@8611
    89
    }
dludwig@8611
    90
dludwig@8611
    91
    if (mutex->cpp_mutex.try_lock() == false) {
dludwig@8611
    92
        retval = SDL_MUTEX_TIMEDOUT;
dludwig@8611
    93
    }
dludwig@8611
    94
    return retval;
dludwig@8611
    95
}
dludwig@8611
    96
dludwig@8356
    97
/* Unlock the mutex */
dludwig@8357
    98
extern "C"
dludwig@8356
    99
int
dludwig@8356
   100
SDL_mutexV(SDL_mutex * mutex)
dludwig@8356
   101
{
dludwig@8356
   102
    if (mutex == NULL) {
dludwig@8356
   103
        SDL_SetError("Passed a NULL mutex");
dludwig@8356
   104
        return -1;
dludwig@8356
   105
    }
dludwig@8356
   106
dludwig@8484
   107
    mutex->cpp_mutex.unlock();
dludwig@8484
   108
    return 0;
dludwig@8356
   109
}
dludwig@8356
   110
dludwig@8356
   111
/* vi: set ts=4 sw=4 expandtab: */