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
     1 /*
     2   Simple DirectMedia Layer
     3   Copyright (C) 1997-2012 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_config.h"
    22 
    23 extern "C" {
    24 #include "SDL_thread.h"
    25 #include "SDL_systhread_c.h"
    26 #include "SDL_log.h"
    27 }
    28 
    29 #include <exception>
    30 
    31 #include "SDL_sysmutex_c.h"
    32 #include <Windows.h>
    33 
    34 
    35 /* Create a mutex */
    36 extern "C"
    37 SDL_mutex *
    38 SDL_CreateMutex(void)
    39 {
    40     /* Allocate and initialize the mutex */
    41     try {
    42         SDL_mutex * mutex = new SDL_mutex;
    43         return mutex;
    44     } catch (std::exception & ex) {
    45         SDL_SetError("unable to create C++ mutex: %s", ex.what());
    46         return NULL;
    47     } catch (...) {
    48         SDL_SetError("unable to create C++ mutex due to an unknown exception");
    49         return NULL;
    50     }
    51 }
    52 
    53 /* Free the mutex */
    54 extern "C"
    55 void
    56 SDL_DestroyMutex(SDL_mutex * mutex)
    57 {
    58     if (mutex) {
    59         try {
    60             delete mutex;
    61         } catch (...) {
    62             // catch any and all exceptions, just in case something happens
    63         }
    64     }
    65 }
    66 
    67 /* Lock the semaphore */
    68 extern "C"
    69 int
    70 SDL_mutexP(SDL_mutex * mutex)
    71 {
    72     SDL_threadID threadID = SDL_ThreadID();
    73     DWORD realThreadID = GetCurrentThreadId();
    74     if (mutex == NULL) {
    75         SDL_SetError("Passed a NULL mutex");
    76         return -1;
    77     }
    78 
    79     try {
    80         mutex->cpp_mutex.lock();
    81         return 0;
    82     } catch (std::exception & ex) {
    83         SDL_SetError("unable to lock C++ mutex: %s", ex.what());
    84         return -1;
    85     } catch (...) {
    86         SDL_SetError("unable to lock C++ mutex due to an unknown exception");
    87         return -1;
    88     }
    89 }
    90 
    91 /* Unlock the mutex */
    92 extern "C"
    93 int
    94 SDL_mutexV(SDL_mutex * mutex)
    95 {
    96     SDL_threadID threadID = SDL_ThreadID();
    97     DWORD realThreadID = GetCurrentThreadId();
    98     if (mutex == NULL) {
    99         SDL_SetError("Passed a NULL mutex");
   100         return -1;
   101     }
   102 
   103     try {
   104         mutex->cpp_mutex.unlock();
   105         return 0;
   106     } catch (...) {
   107         // catch any and all exceptions, just in case something happens.
   108         SDL_SetError("unable to unlock C++ mutex due to an unknown exception");
   109         return -1;
   110     }
   111 }
   112 
   113 /* vi: set ts=4 sw=4 expandtab: */