src/thread/stdcpp/SDL_sysmutex.cpp
author David Ludwig
Sat, 24 Nov 2012 11:17:23 -0500
changeset 8360 7f1bc00e59fc
parent 8357 8d788bb003f2
child 8484 7258989352eb
permissions -rw-r--r--
WinRT: added a functional threading backend using C++11 apis
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@8360
    29
#include <exception>
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@8360
    44
    } catch (std::exception & ex) {
dludwig@8360
    45
        SDL_SetError("unable to create C++ mutex: %s", ex.what());
dludwig@8360
    46
        return NULL;
dludwig@8360
    47
    } catch (...) {
dludwig@8360
    48
        SDL_SetError("unable to create C++ mutex due to an unknown exception");
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@8360
    59
        try {
dludwig@8360
    60
            delete mutex;
dludwig@8360
    61
        } catch (...) {
dludwig@8360
    62
            // catch any and all exceptions, just in case something happens
dludwig@8356
    63
        }
dludwig@8356
    64
    }
dludwig@8356
    65
}
dludwig@8356
    66
dludwig@8356
    67
/* Lock the semaphore */
dludwig@8357
    68
extern "C"
dludwig@8356
    69
int
dludwig@8356
    70
SDL_mutexP(SDL_mutex * mutex)
dludwig@8356
    71
{
dludwig@8360
    72
    SDL_threadID threadID = SDL_ThreadID();
dludwig@8360
    73
    DWORD realThreadID = GetCurrentThreadId();
dludwig@8356
    74
    if (mutex == NULL) {
dludwig@8356
    75
        SDL_SetError("Passed a NULL mutex");
dludwig@8356
    76
        return -1;
dludwig@8356
    77
    }
dludwig@8356
    78
dludwig@8360
    79
    try {
dludwig@8360
    80
        mutex->cpp_mutex.lock();
dludwig@8360
    81
        return 0;
dludwig@8360
    82
    } catch (std::exception & ex) {
dludwig@8360
    83
        SDL_SetError("unable to lock C++ mutex: %s", ex.what());
dludwig@8360
    84
        return -1;
dludwig@8360
    85
    } catch (...) {
dludwig@8360
    86
        SDL_SetError("unable to lock C++ mutex due to an unknown exception");
dludwig@8360
    87
        return -1;
dludwig@8356
    88
    }
dludwig@8356
    89
}
dludwig@8356
    90
dludwig@8356
    91
/* Unlock the mutex */
dludwig@8357
    92
extern "C"
dludwig@8356
    93
int
dludwig@8356
    94
SDL_mutexV(SDL_mutex * mutex)
dludwig@8356
    95
{
dludwig@8360
    96
    SDL_threadID threadID = SDL_ThreadID();
dludwig@8360
    97
    DWORD realThreadID = GetCurrentThreadId();
dludwig@8356
    98
    if (mutex == NULL) {
dludwig@8356
    99
        SDL_SetError("Passed a NULL mutex");
dludwig@8356
   100
        return -1;
dludwig@8356
   101
    }
dludwig@8356
   102
dludwig@8360
   103
    try {
dludwig@8360
   104
        mutex->cpp_mutex.unlock();
dludwig@8360
   105
        return 0;
dludwig@8360
   106
    } catch (...) {
dludwig@8360
   107
        // catch any and all exceptions, just in case something happens.
dludwig@8360
   108
        SDL_SetError("unable to unlock C++ mutex due to an unknown exception");
dludwig@8356
   109
        return -1;
dludwig@8356
   110
    }
dludwig@8356
   111
}
dludwig@8356
   112
dludwig@8356
   113
/* vi: set ts=4 sw=4 expandtab: */