src/thread/stdcpp/SDL_syscond.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 }
    26 
    27 #include <chrono>
    28 #include <condition_variable>
    29 #include <exception>
    30 #include <ratio>
    31 
    32 #include "SDL_sysmutex_c.h"
    33 
    34 struct SDL_cond
    35 {
    36     std::condition_variable_any cpp_cond;
    37 };
    38 
    39 /* Create a condition variable */
    40 extern "C"
    41 SDL_cond *
    42 SDL_CreateCond(void)
    43 {
    44     /* Allocate and initialize the condition variable */
    45     try {
    46         SDL_cond * cond = new SDL_cond;
    47         return cond;
    48     } catch (std::exception & ex) {
    49         SDL_SetError("unable to create C++ condition variable: %s", ex.what());
    50         return NULL;
    51     } catch (...) {
    52         SDL_SetError("unable to create C++ condition variable due to an unknown exception");
    53         return NULL;
    54     }
    55 }
    56 
    57 /* Destroy a condition variable */
    58 extern "C"
    59 void
    60 SDL_DestroyCond(SDL_cond * cond)
    61 {
    62     if (cond) {
    63         try {
    64             delete cond;
    65         } catch (...) {
    66             // catch any and all exceptions, just in case something happens
    67         }
    68     }
    69 }
    70 
    71 /* Restart one of the threads that are waiting on the condition variable */
    72 extern "C"
    73 int
    74 SDL_CondSignal(SDL_cond * cond)
    75 {
    76     if (!cond) {
    77         SDL_SetError("Passed a NULL condition variable");
    78         return -1;
    79     }
    80 
    81     try {
    82         cond->cpp_cond.notify_one();
    83         return 0;
    84     } catch (...) {
    85         // catch any and all exceptions, just in case something happens
    86         SDL_SetError("unable to signal C++ condition variable due to an unknown exception");
    87         return -1;
    88     }
    89 }
    90 
    91 /* Restart all threads that are waiting on the condition variable */
    92 extern "C"
    93 int
    94 SDL_CondBroadcast(SDL_cond * cond)
    95 {
    96     if (!cond) {
    97         SDL_SetError("Passed a NULL condition variable");
    98         return -1;
    99     }
   100 
   101     try {
   102         cond->cpp_cond.notify_all();
   103         return 0;
   104     } catch (...) {
   105         // catch any and all exceptions, just in case something happens
   106         SDL_SetError("unable to broadcast C++ condition variable due to an unknown exception");
   107         return -1;
   108     }
   109 }
   110 
   111 /* Wait on the condition variable for at most 'ms' milliseconds.
   112    The mutex must be locked before entering this function!
   113    The mutex is unlocked during the wait, and locked again after the wait.
   114 
   115 Typical use:
   116 
   117 Thread A:
   118     SDL_LockMutex(lock);
   119     while ( ! condition ) {
   120         SDL_CondWait(cond, lock);
   121     }
   122     SDL_UnlockMutex(lock);
   123 
   124 Thread B:
   125     SDL_LockMutex(lock);
   126     ...
   127     condition = true;
   128     ...
   129     SDL_CondSignal(cond);
   130     SDL_UnlockMutex(lock);
   131  */
   132 extern "C"
   133 int
   134 SDL_CondWaitTimeout(SDL_cond * cond, SDL_mutex * mutex, Uint32 ms)
   135 {
   136     if (!cond) {
   137         SDL_SetError("Passed a NULL condition variable");
   138         return -1;
   139     }
   140 
   141     if (!mutex) {
   142         SDL_SetError("Passed a NULL mutex variable");
   143         return -1;
   144     }
   145 
   146     try {
   147         std::unique_lock<std::recursive_mutex> cpp_lock(mutex->cpp_mutex, std::defer_lock_t());
   148         if (ms == SDL_MUTEX_MAXWAIT) {
   149             cond->cpp_cond.wait(
   150                 cpp_lock
   151                 );
   152             cpp_lock.release();
   153             return 0;
   154         } else {
   155             auto wait_result = cond->cpp_cond.wait_for(
   156                 cpp_lock,
   157                 std::chrono::duration<Uint32, std::milli>(ms)
   158                 );
   159             cpp_lock.release();
   160             if (wait_result == std::cv_status::timeout) {
   161                 return SDL_MUTEX_TIMEDOUT;
   162             } else {
   163                 return 0;
   164             }
   165         }
   166     } catch (std::exception & ex) {
   167         SDL_SetError("unable to wait on C++ condition variable: %s", ex.what());
   168         return -1;
   169     } catch (...) {
   170         SDL_SetError("unable to lock wait on C++ condition variable due to an unknown exception");
   171         return -1;
   172     }
   173 }
   174 
   175 /* Wait on the condition variable forever */
   176 extern "C"
   177 int
   178 SDL_CondWait(SDL_cond * cond, SDL_mutex * mutex)
   179 {
   180     return SDL_CondWaitTimeout(cond, mutex, SDL_MUTEX_MAXWAIT);
   181 }
   182 
   183 /* vi: set ts=4 sw=4 expandtab: */