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
authorDavid Ludwig <dludwig@pobox.com>
Tue, 20 Aug 2013 21:54:34 -0400
changeset 8484 7258989352eb
parent 8483 d403ded25942
child 8485 f0ede9ce6a44
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
src/thread/stdcpp/SDL_syscond.cpp
src/thread/stdcpp/SDL_sysmutex.cpp
src/thread/stdcpp/SDL_systhread.cpp
--- a/src/thread/stdcpp/SDL_syscond.cpp	Tue Aug 20 21:22:32 2013 -0400
+++ b/src/thread/stdcpp/SDL_syscond.cpp	Tue Aug 20 21:54:34 2013 -0400
@@ -26,8 +26,8 @@
 
 #include <chrono>
 #include <condition_variable>
-#include <exception>
 #include <ratio>
+#include <system_error>
 
 #include "SDL_sysmutex_c.h"
 
@@ -45,11 +45,11 @@
     try {
         SDL_cond * cond = new SDL_cond;
         return cond;
-    } catch (std::exception & ex) {
-        SDL_SetError("unable to create C++ condition variable: %s", ex.what());
+    } catch (std::system_error & ex) {
+        SDL_SetError("unable to create a C++ condition variable: code=%d; %s", ex.code(), ex.what());
         return NULL;
-    } catch (...) {
-        SDL_SetError("unable to create C++ condition variable due to an unknown exception");
+    } catch (std::bad_alloc &) {
+        SDL_OutOfMemory();
         return NULL;
     }
 }
@@ -60,11 +60,7 @@
 SDL_DestroyCond(SDL_cond * cond)
 {
     if (cond) {
-        try {
-            delete cond;
-        } catch (...) {
-            // catch any and all exceptions, just in case something happens
-        }
+        delete cond;
     }
 }
 
@@ -78,14 +74,8 @@
         return -1;
     }
 
-    try {
-        cond->cpp_cond.notify_one();
-        return 0;
-    } catch (...) {
-        // catch any and all exceptions, just in case something happens
-        SDL_SetError("unable to signal C++ condition variable due to an unknown exception");
-        return -1;
-    }
+    cond->cpp_cond.notify_one();
+    return 0;
 }
 
 /* Restart all threads that are waiting on the condition variable */
@@ -98,14 +88,8 @@
         return -1;
     }
 
-    try {
-        cond->cpp_cond.notify_all();
-        return 0;
-    } catch (...) {
-        // catch any and all exceptions, just in case something happens
-        SDL_SetError("unable to broadcast C++ condition variable due to an unknown exception");
-        return -1;
-    }
+    cond->cpp_cond.notify_all();
+    return 0;
 }
 
 /* Wait on the condition variable for at most 'ms' milliseconds.
@@ -163,11 +147,8 @@
                 return 0;
             }
         }
-    } catch (std::exception & ex) {
-        SDL_SetError("unable to wait on C++ condition variable: %s", ex.what());
-        return -1;
-    } catch (...) {
-        SDL_SetError("unable to lock wait on C++ condition variable due to an unknown exception");
+    } catch (std::system_error & ex) {
+        SDL_SetError("unable to wait on a C++ condition variable: code=%d; %s", ex.code(), ex.what());
         return -1;
     }
 }
--- a/src/thread/stdcpp/SDL_sysmutex.cpp	Tue Aug 20 21:22:32 2013 -0400
+++ b/src/thread/stdcpp/SDL_sysmutex.cpp	Tue Aug 20 21:54:34 2013 -0400
@@ -26,7 +26,7 @@
 #include "SDL_log.h"
 }
 
-#include <exception>
+#include <system_error>
 
 #include "SDL_sysmutex_c.h"
 #include <Windows.h>
@@ -41,11 +41,11 @@
     try {
         SDL_mutex * mutex = new SDL_mutex;
         return mutex;
-    } catch (std::exception & ex) {
-        SDL_SetError("unable to create C++ mutex: %s", ex.what());
+    } catch (std::system_error & ex) {
+        SDL_SetError("unable to create a C++ mutex: code=%d; %s", ex.code(), ex.what());
         return NULL;
-    } catch (...) {
-        SDL_SetError("unable to create C++ mutex due to an unknown exception");
+    } catch (std::bad_alloc &) {
+        SDL_OutOfMemory();
         return NULL;
     }
 }
@@ -56,11 +56,7 @@
 SDL_DestroyMutex(SDL_mutex * mutex)
 {
     if (mutex) {
-        try {
-            delete mutex;
-        } catch (...) {
-            // catch any and all exceptions, just in case something happens
-        }
+        delete mutex;
     }
 }
 
@@ -79,11 +75,8 @@
     try {
         mutex->cpp_mutex.lock();
         return 0;
-    } catch (std::exception & ex) {
-        SDL_SetError("unable to lock C++ mutex: %s", ex.what());
-        return -1;
-    } catch (...) {
-        SDL_SetError("unable to lock C++ mutex due to an unknown exception");
+    } catch (std::system_error & ex) {
+        SDL_SetError("unable to lock a C++ mutex: code=%d; %s", ex.code(), ex.what());
         return -1;
     }
 }
@@ -100,14 +93,8 @@
         return -1;
     }
 
-    try {
-        mutex->cpp_mutex.unlock();
-        return 0;
-    } catch (...) {
-        // catch any and all exceptions, just in case something happens.
-        SDL_SetError("unable to unlock C++ mutex due to an unknown exception");
-        return -1;
-    }
+    mutex->cpp_mutex.unlock();
+    return 0;
 }
 
 /* vi: set ts=4 sw=4 expandtab: */
--- a/src/thread/stdcpp/SDL_systhread.cpp	Tue Aug 20 21:22:32 2013 -0400
+++ b/src/thread/stdcpp/SDL_systhread.cpp	Tue Aug 20 21:54:34 2013 -0400
@@ -31,6 +31,7 @@
 
 #include <mutex>
 #include <thread>
+#include <system_error>
 
 // HACK: Mimic C++11's thread_local keyword on Visual C++ 2012 (aka. VC++ 11)
 // TODO: make sure this hack doesn't get used if and when Visual C++ supports
@@ -55,11 +56,11 @@
         std::thread cpp_thread(RunThread, args);
         thread->handle = (void *) new std::thread(std::move(cpp_thread));
         return 0;
-    } catch (std::exception & ex) {
-        SDL_SetError("unable to create a C++ thread: %s", ex.what());
+    } catch (std::system_error & ex) {
+        SDL_SetError("unable to start a C++ thread: code=%d; %s", ex.code(), ex.what());
         return -1;
-    } catch (...) {
-        SDL_SetError("unable to create a C++ thread due to an unknown exception");
+    } catch (std::bad_alloc &) {
+        SDL_OutOfMemory();
         return -1;
     }
 }
@@ -114,10 +115,10 @@
         if (cpp_thread->joinable()) {
             cpp_thread->join();
         }
-    } catch (...) {
-        // Catch any exceptions, just in case.
-        // Report nothing, as SDL_WaitThread does not seem to offer a means
-        // to report errors to its callers.
+    } catch (std::system_error &) {
+        // An error occurred when joining the thread.  SDL_WaitThread does not,
+        // however, seem to provide a means to report errors to its callers
+        // though!
     }
 }