1.1 --- a/src/thread/stdcpp/SDL_sysmutex.cpp Sat Nov 24 11:16:45 2012 -0500
1.2 +++ b/src/thread/stdcpp/SDL_sysmutex.cpp Sat Nov 24 11:17:23 2012 -0500
1.3 @@ -20,41 +20,34 @@
1.4 */
1.5 #include "SDL_config.h"
1.6
1.7 -/* An implementation of mutexes using semaphores */
1.8 -
1.9 +extern "C" {
1.10 #include "SDL_thread.h"
1.11 #include "SDL_systhread_c.h"
1.12 -
1.13 +#include "SDL_log.h"
1.14 +}
1.15
1.16 -struct SDL_mutex
1.17 -{
1.18 - int recursive;
1.19 - SDL_threadID owner;
1.20 - SDL_sem *sem;
1.21 -};
1.22 +#include <exception>
1.23 +
1.24 +#include "SDL_sysmutex_c.h"
1.25 +#include <Windows.h>
1.26 +
1.27
1.28 /* Create a mutex */
1.29 extern "C"
1.30 SDL_mutex *
1.31 SDL_CreateMutex(void)
1.32 {
1.33 - SDL_mutex *mutex;
1.34 -
1.35 - /* Allocate mutex memory */
1.36 - mutex = (SDL_mutex *) SDL_malloc(sizeof(*mutex));
1.37 - if (mutex) {
1.38 - /* Create the mutex semaphore, with initial value 1 */
1.39 - mutex->sem = SDL_CreateSemaphore(1);
1.40 - mutex->recursive = 0;
1.41 - mutex->owner = 0;
1.42 - if (!mutex->sem) {
1.43 - SDL_free(mutex);
1.44 - mutex = NULL;
1.45 - }
1.46 - } else {
1.47 - SDL_OutOfMemory();
1.48 + /* Allocate and initialize the mutex */
1.49 + try {
1.50 + SDL_mutex * mutex = new SDL_mutex;
1.51 + return mutex;
1.52 + } catch (std::exception & ex) {
1.53 + SDL_SetError("unable to create C++ mutex: %s", ex.what());
1.54 + return NULL;
1.55 + } catch (...) {
1.56 + SDL_SetError("unable to create C++ mutex due to an unknown exception");
1.57 + return NULL;
1.58 }
1.59 - return mutex;
1.60 }
1.61
1.62 /* Free the mutex */
1.63 @@ -63,10 +56,11 @@
1.64 SDL_DestroyMutex(SDL_mutex * mutex)
1.65 {
1.66 if (mutex) {
1.67 - if (mutex->sem) {
1.68 - SDL_DestroySemaphore(mutex->sem);
1.69 + try {
1.70 + delete mutex;
1.71 + } catch (...) {
1.72 + // catch any and all exceptions, just in case something happens
1.73 }
1.74 - SDL_free(mutex);
1.75 }
1.76 }
1.77
1.78 @@ -75,31 +69,23 @@
1.79 int
1.80 SDL_mutexP(SDL_mutex * mutex)
1.81 {
1.82 -#if SDL_THREADS_DISABLED
1.83 - return 0;
1.84 -#else
1.85 - SDL_threadID this_thread;
1.86 -
1.87 + SDL_threadID threadID = SDL_ThreadID();
1.88 + DWORD realThreadID = GetCurrentThreadId();
1.89 if (mutex == NULL) {
1.90 SDL_SetError("Passed a NULL mutex");
1.91 return -1;
1.92 }
1.93
1.94 - this_thread = SDL_ThreadID();
1.95 - if (mutex->owner == this_thread) {
1.96 - ++mutex->recursive;
1.97 - } else {
1.98 - /* The order of operations is important.
1.99 - We set the locking thread id after we obtain the lock
1.100 - so unlocks from other threads will fail.
1.101 - */
1.102 - SDL_SemWait(mutex->sem);
1.103 - mutex->owner = this_thread;
1.104 - mutex->recursive = 0;
1.105 + try {
1.106 + mutex->cpp_mutex.lock();
1.107 + return 0;
1.108 + } catch (std::exception & ex) {
1.109 + SDL_SetError("unable to lock C++ mutex: %s", ex.what());
1.110 + return -1;
1.111 + } catch (...) {
1.112 + SDL_SetError("unable to lock C++ mutex due to an unknown exception");
1.113 + return -1;
1.114 }
1.115 -
1.116 - return 0;
1.117 -#endif /* SDL_THREADS_DISABLED */
1.118 }
1.119
1.120 /* Unlock the mutex */
1.121 @@ -107,33 +93,21 @@
1.122 int
1.123 SDL_mutexV(SDL_mutex * mutex)
1.124 {
1.125 -#if SDL_THREADS_DISABLED
1.126 - return 0;
1.127 -#else
1.128 + SDL_threadID threadID = SDL_ThreadID();
1.129 + DWORD realThreadID = GetCurrentThreadId();
1.130 if (mutex == NULL) {
1.131 SDL_SetError("Passed a NULL mutex");
1.132 return -1;
1.133 }
1.134
1.135 - /* If we don't own the mutex, we can't unlock it */
1.136 - if (SDL_ThreadID() != mutex->owner) {
1.137 - SDL_SetError("mutex not owned by this thread");
1.138 + try {
1.139 + mutex->cpp_mutex.unlock();
1.140 + return 0;
1.141 + } catch (...) {
1.142 + // catch any and all exceptions, just in case something happens.
1.143 + SDL_SetError("unable to unlock C++ mutex due to an unknown exception");
1.144 return -1;
1.145 }
1.146 -
1.147 - if (mutex->recursive) {
1.148 - --mutex->recursive;
1.149 - } else {
1.150 - /* The order of operations is important.
1.151 - First reset the owner so another thread doesn't lock
1.152 - the mutex and set the ownership before we reset it,
1.153 - then release the lock semaphore.
1.154 - */
1.155 - mutex->owner = 0;
1.156 - SDL_SemPost(mutex->sem);
1.157 - }
1.158 - return 0;
1.159 -#endif /* SDL_THREADS_DISABLED */
1.160 }
1.161
1.162 /* vi: set ts=4 sw=4 expandtab: */