src/thread/stdcpp/SDL_sysmutex.cpp
author David Ludwig
Tue, 20 Aug 2013 21:54:34 -0400
changeset 8484 7258989352eb
parent 8360 7f1bc00e59fc
child 8488 bb48911cd9c8
permissions -rw-r--r--
WinRT: made the C++11-based threading backend only try to catch exceptions that it knows it (the threading APIs) might throw, rather than all exceptions
dludwig@8356
     1
/*
dludwig@8356
     2
  Simple DirectMedia Layer
dludwig@8356
     3
  Copyright (C) 1997-2012 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@8356
    21
#include "SDL_config.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@8360
    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@8360
    68
    SDL_threadID threadID = SDL_ThreadID();
dludwig@8360
    69
    DWORD realThreadID = GetCurrentThreadId();
dludwig@8356
    70
    if (mutex == NULL) {
dludwig@8356
    71
        SDL_SetError("Passed a NULL mutex");
dludwig@8356
    72
        return -1;
dludwig@8356
    73
    }
dludwig@8356
    74
dludwig@8360
    75
    try {
dludwig@8360
    76
        mutex->cpp_mutex.lock();
dludwig@8360
    77
        return 0;
dludwig@8484
    78
    } catch (std::system_error & ex) {
dludwig@8484
    79
        SDL_SetError("unable to lock a C++ mutex: code=%d; %s", ex.code(), ex.what());
dludwig@8360
    80
        return -1;
dludwig@8356
    81
    }
dludwig@8356
    82
}
dludwig@8356
    83
dludwig@8356
    84
/* Unlock the mutex */
dludwig@8357
    85
extern "C"
dludwig@8356
    86
int
dludwig@8356
    87
SDL_mutexV(SDL_mutex * mutex)
dludwig@8356
    88
{
dludwig@8360
    89
    SDL_threadID threadID = SDL_ThreadID();
dludwig@8360
    90
    DWORD realThreadID = GetCurrentThreadId();
dludwig@8356
    91
    if (mutex == NULL) {
dludwig@8356
    92
        SDL_SetError("Passed a NULL mutex");
dludwig@8356
    93
        return -1;
dludwig@8356
    94
    }
dludwig@8356
    95
dludwig@8484
    96
    mutex->cpp_mutex.unlock();
dludwig@8484
    97
    return 0;
dludwig@8356
    98
}
dludwig@8356
    99
dludwig@8356
   100
/* vi: set ts=4 sw=4 expandtab: */