WinRT: made the skeleton C++11 thread implementation use .cpp files, not .c
authorDavid Ludwig <dludwig@pobox.com>
Thu, 22 Nov 2012 23:12:06 -0500
changeset 83578d788bb003f2
parent 8356 4d85eba58f0a
child 8358 3511af7671fd
WinRT: made the skeleton C++11 thread implementation use .cpp files, not .c
VisualC/SDL/SDL_VS2012_WinRT.vcxproj
src/thread/stdcpp/SDL_syscond.c
src/thread/stdcpp/SDL_syscond.cpp
src/thread/stdcpp/SDL_sysmutex.c
src/thread/stdcpp/SDL_sysmutex.cpp
src/thread/stdcpp/SDL_systhread.c
src/thread/stdcpp/SDL_systhread.cpp
     1.1 --- a/VisualC/SDL/SDL_VS2012_WinRT.vcxproj	Thu Nov 22 23:03:56 2012 -0500
     1.2 +++ b/VisualC/SDL/SDL_VS2012_WinRT.vcxproj	Thu Nov 22 23:12:06 2012 -0500
     1.3 @@ -91,9 +91,9 @@
     1.4      <ClCompile Include="..\..\src\stdlib\SDL_string.c" />
     1.5      <ClCompile Include="..\..\src\thread\generic\SDL_syssem.c" />
     1.6      <ClCompile Include="..\..\src\thread\SDL_thread.c" />
     1.7 -    <ClCompile Include="..\..\src\thread\stdcpp\SDL_syscond.c" />
     1.8 -    <ClCompile Include="..\..\src\thread\stdcpp\SDL_sysmutex.c" />
     1.9 -    <ClCompile Include="..\..\src\thread\stdcpp\SDL_systhread.c" />
    1.10 +    <ClCompile Include="..\..\src\thread\stdcpp\SDL_syscond.cpp" />
    1.11 +    <ClCompile Include="..\..\src\thread\stdcpp\SDL_sysmutex.cpp" />
    1.12 +    <ClCompile Include="..\..\src\thread\stdcpp\SDL_systhread.cpp" />
    1.13      <ClCompile Include="..\..\src\timer\dummy\SDL_systimer.c" />
    1.14      <ClCompile Include="..\..\src\timer\SDL_timer.c" />
    1.15      <ClCompile Include="..\..\src\video\dummy\SDL_nullevents.c" />
     2.1 --- a/src/thread/stdcpp/SDL_syscond.c	Thu Nov 22 23:03:56 2012 -0500
     2.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.3 @@ -1,223 +0,0 @@
     2.4 -/*
     2.5 -  Simple DirectMedia Layer
     2.6 -  Copyright (C) 1997-2012 Sam Lantinga <slouken@libsdl.org>
     2.7 -
     2.8 -  This software is provided 'as-is', without any express or implied
     2.9 -  warranty.  In no event will the authors be held liable for any damages
    2.10 -  arising from the use of this software.
    2.11 -
    2.12 -  Permission is granted to anyone to use this software for any purpose,
    2.13 -  including commercial applications, and to alter it and redistribute it
    2.14 -  freely, subject to the following restrictions:
    2.15 -
    2.16 -  1. The origin of this software must not be misrepresented; you must not
    2.17 -     claim that you wrote the original software. If you use this software
    2.18 -     in a product, an acknowledgment in the product documentation would be
    2.19 -     appreciated but is not required.
    2.20 -  2. Altered source versions must be plainly marked as such, and must not be
    2.21 -     misrepresented as being the original software.
    2.22 -  3. This notice may not be removed or altered from any source distribution.
    2.23 -*/
    2.24 -#include "SDL_config.h"
    2.25 -
    2.26 -/* An implementation of condition variables using semaphores and mutexes */
    2.27 -/*
    2.28 -   This implementation borrows heavily from the BeOS condition variable
    2.29 -   implementation, written by Christopher Tate and Owen Smith.  Thanks!
    2.30 - */
    2.31 -
    2.32 -#include "SDL_thread.h"
    2.33 -
    2.34 -struct SDL_cond
    2.35 -{
    2.36 -    SDL_mutex *lock;
    2.37 -    int waiting;
    2.38 -    int signals;
    2.39 -    SDL_sem *wait_sem;
    2.40 -    SDL_sem *wait_done;
    2.41 -};
    2.42 -
    2.43 -/* Create a condition variable */
    2.44 -SDL_cond *
    2.45 -SDL_CreateCond(void)
    2.46 -{
    2.47 -    SDL_cond *cond;
    2.48 -
    2.49 -    cond = (SDL_cond *) SDL_malloc(sizeof(SDL_cond));
    2.50 -    if (cond) {
    2.51 -        cond->lock = SDL_CreateMutex();
    2.52 -        cond->wait_sem = SDL_CreateSemaphore(0);
    2.53 -        cond->wait_done = SDL_CreateSemaphore(0);
    2.54 -        cond->waiting = cond->signals = 0;
    2.55 -        if (!cond->lock || !cond->wait_sem || !cond->wait_done) {
    2.56 -            SDL_DestroyCond(cond);
    2.57 -            cond = NULL;
    2.58 -        }
    2.59 -    } else {
    2.60 -        SDL_OutOfMemory();
    2.61 -    }
    2.62 -    return (cond);
    2.63 -}
    2.64 -
    2.65 -/* Destroy a condition variable */
    2.66 -void
    2.67 -SDL_DestroyCond(SDL_cond * cond)
    2.68 -{
    2.69 -    if (cond) {
    2.70 -        if (cond->wait_sem) {
    2.71 -            SDL_DestroySemaphore(cond->wait_sem);
    2.72 -        }
    2.73 -        if (cond->wait_done) {
    2.74 -            SDL_DestroySemaphore(cond->wait_done);
    2.75 -        }
    2.76 -        if (cond->lock) {
    2.77 -            SDL_DestroyMutex(cond->lock);
    2.78 -        }
    2.79 -        SDL_free(cond);
    2.80 -    }
    2.81 -}
    2.82 -
    2.83 -/* Restart one of the threads that are waiting on the condition variable */
    2.84 -int
    2.85 -SDL_CondSignal(SDL_cond * cond)
    2.86 -{
    2.87 -    if (!cond) {
    2.88 -        SDL_SetError("Passed a NULL condition variable");
    2.89 -        return -1;
    2.90 -    }
    2.91 -
    2.92 -    /* If there are waiting threads not already signalled, then
    2.93 -       signal the condition and wait for the thread to respond.
    2.94 -     */
    2.95 -    SDL_LockMutex(cond->lock);
    2.96 -    if (cond->waiting > cond->signals) {
    2.97 -        ++cond->signals;
    2.98 -        SDL_SemPost(cond->wait_sem);
    2.99 -        SDL_UnlockMutex(cond->lock);
   2.100 -        SDL_SemWait(cond->wait_done);
   2.101 -    } else {
   2.102 -        SDL_UnlockMutex(cond->lock);
   2.103 -    }
   2.104 -
   2.105 -    return 0;
   2.106 -}
   2.107 -
   2.108 -/* Restart all threads that are waiting on the condition variable */
   2.109 -int
   2.110 -SDL_CondBroadcast(SDL_cond * cond)
   2.111 -{
   2.112 -    if (!cond) {
   2.113 -        SDL_SetError("Passed a NULL condition variable");
   2.114 -        return -1;
   2.115 -    }
   2.116 -
   2.117 -    /* If there are waiting threads not already signalled, then
   2.118 -       signal the condition and wait for the thread to respond.
   2.119 -     */
   2.120 -    SDL_LockMutex(cond->lock);
   2.121 -    if (cond->waiting > cond->signals) {
   2.122 -        int i, num_waiting;
   2.123 -
   2.124 -        num_waiting = (cond->waiting - cond->signals);
   2.125 -        cond->signals = cond->waiting;
   2.126 -        for (i = 0; i < num_waiting; ++i) {
   2.127 -            SDL_SemPost(cond->wait_sem);
   2.128 -        }
   2.129 -        /* Now all released threads are blocked here, waiting for us.
   2.130 -           Collect them all (and win fabulous prizes!) :-)
   2.131 -         */
   2.132 -        SDL_UnlockMutex(cond->lock);
   2.133 -        for (i = 0; i < num_waiting; ++i) {
   2.134 -            SDL_SemWait(cond->wait_done);
   2.135 -        }
   2.136 -    } else {
   2.137 -        SDL_UnlockMutex(cond->lock);
   2.138 -    }
   2.139 -
   2.140 -    return 0;
   2.141 -}
   2.142 -
   2.143 -/* Wait on the condition variable for at most 'ms' milliseconds.
   2.144 -   The mutex must be locked before entering this function!
   2.145 -   The mutex is unlocked during the wait, and locked again after the wait.
   2.146 -
   2.147 -Typical use:
   2.148 -
   2.149 -Thread A:
   2.150 -    SDL_LockMutex(lock);
   2.151 -    while ( ! condition ) {
   2.152 -        SDL_CondWait(cond, lock);
   2.153 -    }
   2.154 -    SDL_UnlockMutex(lock);
   2.155 -
   2.156 -Thread B:
   2.157 -    SDL_LockMutex(lock);
   2.158 -    ...
   2.159 -    condition = true;
   2.160 -    ...
   2.161 -    SDL_CondSignal(cond);
   2.162 -    SDL_UnlockMutex(lock);
   2.163 - */
   2.164 -int
   2.165 -SDL_CondWaitTimeout(SDL_cond * cond, SDL_mutex * mutex, Uint32 ms)
   2.166 -{
   2.167 -    int retval;
   2.168 -
   2.169 -    if (!cond) {
   2.170 -        SDL_SetError("Passed a NULL condition variable");
   2.171 -        return -1;
   2.172 -    }
   2.173 -
   2.174 -    /* Obtain the protection mutex, and increment the number of waiters.
   2.175 -       This allows the signal mechanism to only perform a signal if there
   2.176 -       are waiting threads.
   2.177 -     */
   2.178 -    SDL_LockMutex(cond->lock);
   2.179 -    ++cond->waiting;
   2.180 -    SDL_UnlockMutex(cond->lock);
   2.181 -
   2.182 -    /* Unlock the mutex, as is required by condition variable semantics */
   2.183 -    SDL_UnlockMutex(mutex);
   2.184 -
   2.185 -    /* Wait for a signal */
   2.186 -    if (ms == SDL_MUTEX_MAXWAIT) {
   2.187 -        retval = SDL_SemWait(cond->wait_sem);
   2.188 -    } else {
   2.189 -        retval = SDL_SemWaitTimeout(cond->wait_sem, ms);
   2.190 -    }
   2.191 -
   2.192 -    /* Let the signaler know we have completed the wait, otherwise
   2.193 -       the signaler can race ahead and get the condition semaphore
   2.194 -       if we are stopped between the mutex unlock and semaphore wait,
   2.195 -       giving a deadlock.  See the following URL for details:
   2.196 -       http://www-classic.be.com/aboutbe/benewsletter/volume_III/Issue40.html
   2.197 -     */
   2.198 -    SDL_LockMutex(cond->lock);
   2.199 -    if (cond->signals > 0) {
   2.200 -        /* If we timed out, we need to eat a condition signal */
   2.201 -        if (retval > 0) {
   2.202 -            SDL_SemWait(cond->wait_sem);
   2.203 -        }
   2.204 -        /* We always notify the signal thread that we are done */
   2.205 -        SDL_SemPost(cond->wait_done);
   2.206 -
   2.207 -        /* Signal handshake complete */
   2.208 -        --cond->signals;
   2.209 -    }
   2.210 -    --cond->waiting;
   2.211 -    SDL_UnlockMutex(cond->lock);
   2.212 -
   2.213 -    /* Lock the mutex, as is required by condition variable semantics */
   2.214 -    SDL_LockMutex(mutex);
   2.215 -
   2.216 -    return retval;
   2.217 -}
   2.218 -
   2.219 -/* Wait on the condition variable forever */
   2.220 -int
   2.221 -SDL_CondWait(SDL_cond * cond, SDL_mutex * mutex)
   2.222 -{
   2.223 -    return SDL_CondWaitTimeout(cond, mutex, SDL_MUTEX_MAXWAIT);
   2.224 -}
   2.225 -
   2.226 -/* vi: set ts=4 sw=4 expandtab: */
     3.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     3.2 +++ b/src/thread/stdcpp/SDL_syscond.cpp	Thu Nov 22 23:12:06 2012 -0500
     3.3 @@ -0,0 +1,229 @@
     3.4 +/*
     3.5 +  Simple DirectMedia Layer
     3.6 +  Copyright (C) 1997-2012 Sam Lantinga <slouken@libsdl.org>
     3.7 +
     3.8 +  This software is provided 'as-is', without any express or implied
     3.9 +  warranty.  In no event will the authors be held liable for any damages
    3.10 +  arising from the use of this software.
    3.11 +
    3.12 +  Permission is granted to anyone to use this software for any purpose,
    3.13 +  including commercial applications, and to alter it and redistribute it
    3.14 +  freely, subject to the following restrictions:
    3.15 +
    3.16 +  1. The origin of this software must not be misrepresented; you must not
    3.17 +     claim that you wrote the original software. If you use this software
    3.18 +     in a product, an acknowledgment in the product documentation would be
    3.19 +     appreciated but is not required.
    3.20 +  2. Altered source versions must be plainly marked as such, and must not be
    3.21 +     misrepresented as being the original software.
    3.22 +  3. This notice may not be removed or altered from any source distribution.
    3.23 +*/
    3.24 +#include "SDL_config.h"
    3.25 +
    3.26 +/* An implementation of condition variables using semaphores and mutexes */
    3.27 +/*
    3.28 +   This implementation borrows heavily from the BeOS condition variable
    3.29 +   implementation, written by Christopher Tate and Owen Smith.  Thanks!
    3.30 + */
    3.31 +
    3.32 +#include "SDL_thread.h"
    3.33 +
    3.34 +struct SDL_cond
    3.35 +{
    3.36 +    SDL_mutex *lock;
    3.37 +    int waiting;
    3.38 +    int signals;
    3.39 +    SDL_sem *wait_sem;
    3.40 +    SDL_sem *wait_done;
    3.41 +};
    3.42 +
    3.43 +/* Create a condition variable */
    3.44 +extern "C"
    3.45 +SDL_cond *
    3.46 +SDL_CreateCond(void)
    3.47 +{
    3.48 +    SDL_cond *cond;
    3.49 +
    3.50 +    cond = (SDL_cond *) SDL_malloc(sizeof(SDL_cond));
    3.51 +    if (cond) {
    3.52 +        cond->lock = SDL_CreateMutex();
    3.53 +        cond->wait_sem = SDL_CreateSemaphore(0);
    3.54 +        cond->wait_done = SDL_CreateSemaphore(0);
    3.55 +        cond->waiting = cond->signals = 0;
    3.56 +        if (!cond->lock || !cond->wait_sem || !cond->wait_done) {
    3.57 +            SDL_DestroyCond(cond);
    3.58 +            cond = NULL;
    3.59 +        }
    3.60 +    } else {
    3.61 +        SDL_OutOfMemory();
    3.62 +    }
    3.63 +    return (cond);
    3.64 +}
    3.65 +
    3.66 +/* Destroy a condition variable */
    3.67 +extern "C"
    3.68 +void
    3.69 +SDL_DestroyCond(SDL_cond * cond)
    3.70 +{
    3.71 +    if (cond) {
    3.72 +        if (cond->wait_sem) {
    3.73 +            SDL_DestroySemaphore(cond->wait_sem);
    3.74 +        }
    3.75 +        if (cond->wait_done) {
    3.76 +            SDL_DestroySemaphore(cond->wait_done);
    3.77 +        }
    3.78 +        if (cond->lock) {
    3.79 +            SDL_DestroyMutex(cond->lock);
    3.80 +        }
    3.81 +        SDL_free(cond);
    3.82 +    }
    3.83 +}
    3.84 +
    3.85 +/* Restart one of the threads that are waiting on the condition variable */
    3.86 +extern "C"
    3.87 +int
    3.88 +SDL_CondSignal(SDL_cond * cond)
    3.89 +{
    3.90 +    if (!cond) {
    3.91 +        SDL_SetError("Passed a NULL condition variable");
    3.92 +        return -1;
    3.93 +    }
    3.94 +
    3.95 +    /* If there are waiting threads not already signalled, then
    3.96 +       signal the condition and wait for the thread to respond.
    3.97 +     */
    3.98 +    SDL_LockMutex(cond->lock);
    3.99 +    if (cond->waiting > cond->signals) {
   3.100 +        ++cond->signals;
   3.101 +        SDL_SemPost(cond->wait_sem);
   3.102 +        SDL_UnlockMutex(cond->lock);
   3.103 +        SDL_SemWait(cond->wait_done);
   3.104 +    } else {
   3.105 +        SDL_UnlockMutex(cond->lock);
   3.106 +    }
   3.107 +
   3.108 +    return 0;
   3.109 +}
   3.110 +
   3.111 +/* Restart all threads that are waiting on the condition variable */
   3.112 +extern "C"
   3.113 +int
   3.114 +SDL_CondBroadcast(SDL_cond * cond)
   3.115 +{
   3.116 +    if (!cond) {
   3.117 +        SDL_SetError("Passed a NULL condition variable");
   3.118 +        return -1;
   3.119 +    }
   3.120 +
   3.121 +    /* If there are waiting threads not already signalled, then
   3.122 +       signal the condition and wait for the thread to respond.
   3.123 +     */
   3.124 +    SDL_LockMutex(cond->lock);
   3.125 +    if (cond->waiting > cond->signals) {
   3.126 +        int i, num_waiting;
   3.127 +
   3.128 +        num_waiting = (cond->waiting - cond->signals);
   3.129 +        cond->signals = cond->waiting;
   3.130 +        for (i = 0; i < num_waiting; ++i) {
   3.131 +            SDL_SemPost(cond->wait_sem);
   3.132 +        }
   3.133 +        /* Now all released threads are blocked here, waiting for us.
   3.134 +           Collect them all (and win fabulous prizes!) :-)
   3.135 +         */
   3.136 +        SDL_UnlockMutex(cond->lock);
   3.137 +        for (i = 0; i < num_waiting; ++i) {
   3.138 +            SDL_SemWait(cond->wait_done);
   3.139 +        }
   3.140 +    } else {
   3.141 +        SDL_UnlockMutex(cond->lock);
   3.142 +    }
   3.143 +
   3.144 +    return 0;
   3.145 +}
   3.146 +
   3.147 +/* Wait on the condition variable for at most 'ms' milliseconds.
   3.148 +   The mutex must be locked before entering this function!
   3.149 +   The mutex is unlocked during the wait, and locked again after the wait.
   3.150 +
   3.151 +Typical use:
   3.152 +
   3.153 +Thread A:
   3.154 +    SDL_LockMutex(lock);
   3.155 +    while ( ! condition ) {
   3.156 +        SDL_CondWait(cond, lock);
   3.157 +    }
   3.158 +    SDL_UnlockMutex(lock);
   3.159 +
   3.160 +Thread B:
   3.161 +    SDL_LockMutex(lock);
   3.162 +    ...
   3.163 +    condition = true;
   3.164 +    ...
   3.165 +    SDL_CondSignal(cond);
   3.166 +    SDL_UnlockMutex(lock);
   3.167 + */
   3.168 +extern "C"
   3.169 +int
   3.170 +SDL_CondWaitTimeout(SDL_cond * cond, SDL_mutex * mutex, Uint32 ms)
   3.171 +{
   3.172 +    int retval;
   3.173 +
   3.174 +    if (!cond) {
   3.175 +        SDL_SetError("Passed a NULL condition variable");
   3.176 +        return -1;
   3.177 +    }
   3.178 +
   3.179 +    /* Obtain the protection mutex, and increment the number of waiters.
   3.180 +       This allows the signal mechanism to only perform a signal if there
   3.181 +       are waiting threads.
   3.182 +     */
   3.183 +    SDL_LockMutex(cond->lock);
   3.184 +    ++cond->waiting;
   3.185 +    SDL_UnlockMutex(cond->lock);
   3.186 +
   3.187 +    /* Unlock the mutex, as is required by condition variable semantics */
   3.188 +    SDL_UnlockMutex(mutex);
   3.189 +
   3.190 +    /* Wait for a signal */
   3.191 +    if (ms == SDL_MUTEX_MAXWAIT) {
   3.192 +        retval = SDL_SemWait(cond->wait_sem);
   3.193 +    } else {
   3.194 +        retval = SDL_SemWaitTimeout(cond->wait_sem, ms);
   3.195 +    }
   3.196 +
   3.197 +    /* Let the signaler know we have completed the wait, otherwise
   3.198 +       the signaler can race ahead and get the condition semaphore
   3.199 +       if we are stopped between the mutex unlock and semaphore wait,
   3.200 +       giving a deadlock.  See the following URL for details:
   3.201 +       http://www-classic.be.com/aboutbe/benewsletter/volume_III/Issue40.html
   3.202 +     */
   3.203 +    SDL_LockMutex(cond->lock);
   3.204 +    if (cond->signals > 0) {
   3.205 +        /* If we timed out, we need to eat a condition signal */
   3.206 +        if (retval > 0) {
   3.207 +            SDL_SemWait(cond->wait_sem);
   3.208 +        }
   3.209 +        /* We always notify the signal thread that we are done */
   3.210 +        SDL_SemPost(cond->wait_done);
   3.211 +
   3.212 +        /* Signal handshake complete */
   3.213 +        --cond->signals;
   3.214 +    }
   3.215 +    --cond->waiting;
   3.216 +    SDL_UnlockMutex(cond->lock);
   3.217 +
   3.218 +    /* Lock the mutex, as is required by condition variable semantics */
   3.219 +    SDL_LockMutex(mutex);
   3.220 +
   3.221 +    return retval;
   3.222 +}
   3.223 +
   3.224 +/* Wait on the condition variable forever */
   3.225 +extern "C"
   3.226 +int
   3.227 +SDL_CondWait(SDL_cond * cond, SDL_mutex * mutex)
   3.228 +{
   3.229 +    return SDL_CondWaitTimeout(cond, mutex, SDL_MUTEX_MAXWAIT);
   3.230 +}
   3.231 +
   3.232 +/* vi: set ts=4 sw=4 expandtab: */
     4.1 --- a/src/thread/stdcpp/SDL_sysmutex.c	Thu Nov 22 23:03:56 2012 -0500
     4.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
     4.3 @@ -1,135 +0,0 @@
     4.4 -/*
     4.5 -  Simple DirectMedia Layer
     4.6 -  Copyright (C) 1997-2012 Sam Lantinga <slouken@libsdl.org>
     4.7 -
     4.8 -  This software is provided 'as-is', without any express or implied
     4.9 -  warranty.  In no event will the authors be held liable for any damages
    4.10 -  arising from the use of this software.
    4.11 -
    4.12 -  Permission is granted to anyone to use this software for any purpose,
    4.13 -  including commercial applications, and to alter it and redistribute it
    4.14 -  freely, subject to the following restrictions:
    4.15 -
    4.16 -  1. The origin of this software must not be misrepresented; you must not
    4.17 -     claim that you wrote the original software. If you use this software
    4.18 -     in a product, an acknowledgment in the product documentation would be
    4.19 -     appreciated but is not required.
    4.20 -  2. Altered source versions must be plainly marked as such, and must not be
    4.21 -     misrepresented as being the original software.
    4.22 -  3. This notice may not be removed or altered from any source distribution.
    4.23 -*/
    4.24 -#include "SDL_config.h"
    4.25 -
    4.26 -/* An implementation of mutexes using semaphores */
    4.27 -
    4.28 -#include "SDL_thread.h"
    4.29 -#include "SDL_systhread_c.h"
    4.30 -
    4.31 -
    4.32 -struct SDL_mutex
    4.33 -{
    4.34 -    int recursive;
    4.35 -    SDL_threadID owner;
    4.36 -    SDL_sem *sem;
    4.37 -};
    4.38 -
    4.39 -/* Create a mutex */
    4.40 -SDL_mutex *
    4.41 -SDL_CreateMutex(void)
    4.42 -{
    4.43 -    SDL_mutex *mutex;
    4.44 -
    4.45 -    /* Allocate mutex memory */
    4.46 -    mutex = (SDL_mutex *) SDL_malloc(sizeof(*mutex));
    4.47 -    if (mutex) {
    4.48 -        /* Create the mutex semaphore, with initial value 1 */
    4.49 -        mutex->sem = SDL_CreateSemaphore(1);
    4.50 -        mutex->recursive = 0;
    4.51 -        mutex->owner = 0;
    4.52 -        if (!mutex->sem) {
    4.53 -            SDL_free(mutex);
    4.54 -            mutex = NULL;
    4.55 -        }
    4.56 -    } else {
    4.57 -        SDL_OutOfMemory();
    4.58 -    }
    4.59 -    return mutex;
    4.60 -}
    4.61 -
    4.62 -/* Free the mutex */
    4.63 -void
    4.64 -SDL_DestroyMutex(SDL_mutex * mutex)
    4.65 -{
    4.66 -    if (mutex) {
    4.67 -        if (mutex->sem) {
    4.68 -            SDL_DestroySemaphore(mutex->sem);
    4.69 -        }
    4.70 -        SDL_free(mutex);
    4.71 -    }
    4.72 -}
    4.73 -
    4.74 -/* Lock the semaphore */
    4.75 -int
    4.76 -SDL_mutexP(SDL_mutex * mutex)
    4.77 -{
    4.78 -#if SDL_THREADS_DISABLED
    4.79 -    return 0;
    4.80 -#else
    4.81 -    SDL_threadID this_thread;
    4.82 -
    4.83 -    if (mutex == NULL) {
    4.84 -        SDL_SetError("Passed a NULL mutex");
    4.85 -        return -1;
    4.86 -    }
    4.87 -
    4.88 -    this_thread = SDL_ThreadID();
    4.89 -    if (mutex->owner == this_thread) {
    4.90 -        ++mutex->recursive;
    4.91 -    } else {
    4.92 -        /* The order of operations is important.
    4.93 -           We set the locking thread id after we obtain the lock
    4.94 -           so unlocks from other threads will fail.
    4.95 -         */
    4.96 -        SDL_SemWait(mutex->sem);
    4.97 -        mutex->owner = this_thread;
    4.98 -        mutex->recursive = 0;
    4.99 -    }
   4.100 -
   4.101 -    return 0;
   4.102 -#endif /* SDL_THREADS_DISABLED */
   4.103 -}
   4.104 -
   4.105 -/* Unlock the mutex */
   4.106 -int
   4.107 -SDL_mutexV(SDL_mutex * mutex)
   4.108 -{
   4.109 -#if SDL_THREADS_DISABLED
   4.110 -    return 0;
   4.111 -#else
   4.112 -    if (mutex == NULL) {
   4.113 -        SDL_SetError("Passed a NULL mutex");
   4.114 -        return -1;
   4.115 -    }
   4.116 -
   4.117 -    /* If we don't own the mutex, we can't unlock it */
   4.118 -    if (SDL_ThreadID() != mutex->owner) {
   4.119 -        SDL_SetError("mutex not owned by this thread");
   4.120 -        return -1;
   4.121 -    }
   4.122 -
   4.123 -    if (mutex->recursive) {
   4.124 -        --mutex->recursive;
   4.125 -    } else {
   4.126 -        /* The order of operations is important.
   4.127 -           First reset the owner so another thread doesn't lock
   4.128 -           the mutex and set the ownership before we reset it,
   4.129 -           then release the lock semaphore.
   4.130 -         */
   4.131 -        mutex->owner = 0;
   4.132 -        SDL_SemPost(mutex->sem);
   4.133 -    }
   4.134 -    return 0;
   4.135 -#endif /* SDL_THREADS_DISABLED */
   4.136 -}
   4.137 -
   4.138 -/* vi: set ts=4 sw=4 expandtab: */
     5.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     5.2 +++ b/src/thread/stdcpp/SDL_sysmutex.cpp	Thu Nov 22 23:12:06 2012 -0500
     5.3 @@ -0,0 +1,139 @@
     5.4 +/*
     5.5 +  Simple DirectMedia Layer
     5.6 +  Copyright (C) 1997-2012 Sam Lantinga <slouken@libsdl.org>
     5.7 +
     5.8 +  This software is provided 'as-is', without any express or implied
     5.9 +  warranty.  In no event will the authors be held liable for any damages
    5.10 +  arising from the use of this software.
    5.11 +
    5.12 +  Permission is granted to anyone to use this software for any purpose,
    5.13 +  including commercial applications, and to alter it and redistribute it
    5.14 +  freely, subject to the following restrictions:
    5.15 +
    5.16 +  1. The origin of this software must not be misrepresented; you must not
    5.17 +     claim that you wrote the original software. If you use this software
    5.18 +     in a product, an acknowledgment in the product documentation would be
    5.19 +     appreciated but is not required.
    5.20 +  2. Altered source versions must be plainly marked as such, and must not be
    5.21 +     misrepresented as being the original software.
    5.22 +  3. This notice may not be removed or altered from any source distribution.
    5.23 +*/
    5.24 +#include "SDL_config.h"
    5.25 +
    5.26 +/* An implementation of mutexes using semaphores */
    5.27 +
    5.28 +#include "SDL_thread.h"
    5.29 +#include "SDL_systhread_c.h"
    5.30 +
    5.31 +
    5.32 +struct SDL_mutex
    5.33 +{
    5.34 +    int recursive;
    5.35 +    SDL_threadID owner;
    5.36 +    SDL_sem *sem;
    5.37 +};
    5.38 +
    5.39 +/* Create a mutex */
    5.40 +extern "C"
    5.41 +SDL_mutex *
    5.42 +SDL_CreateMutex(void)
    5.43 +{
    5.44 +    SDL_mutex *mutex;
    5.45 +
    5.46 +    /* Allocate mutex memory */
    5.47 +    mutex = (SDL_mutex *) SDL_malloc(sizeof(*mutex));
    5.48 +    if (mutex) {
    5.49 +        /* Create the mutex semaphore, with initial value 1 */
    5.50 +        mutex->sem = SDL_CreateSemaphore(1);
    5.51 +        mutex->recursive = 0;
    5.52 +        mutex->owner = 0;
    5.53 +        if (!mutex->sem) {
    5.54 +            SDL_free(mutex);
    5.55 +            mutex = NULL;
    5.56 +        }
    5.57 +    } else {
    5.58 +        SDL_OutOfMemory();
    5.59 +    }
    5.60 +    return mutex;
    5.61 +}
    5.62 +
    5.63 +/* Free the mutex */
    5.64 +extern "C"
    5.65 +void
    5.66 +SDL_DestroyMutex(SDL_mutex * mutex)
    5.67 +{
    5.68 +    if (mutex) {
    5.69 +        if (mutex->sem) {
    5.70 +            SDL_DestroySemaphore(mutex->sem);
    5.71 +        }
    5.72 +        SDL_free(mutex);
    5.73 +    }
    5.74 +}
    5.75 +
    5.76 +/* Lock the semaphore */
    5.77 +extern "C"
    5.78 +int
    5.79 +SDL_mutexP(SDL_mutex * mutex)
    5.80 +{
    5.81 +#if SDL_THREADS_DISABLED
    5.82 +    return 0;
    5.83 +#else
    5.84 +    SDL_threadID this_thread;
    5.85 +
    5.86 +    if (mutex == NULL) {
    5.87 +        SDL_SetError("Passed a NULL mutex");
    5.88 +        return -1;
    5.89 +    }
    5.90 +
    5.91 +    this_thread = SDL_ThreadID();
    5.92 +    if (mutex->owner == this_thread) {
    5.93 +        ++mutex->recursive;
    5.94 +    } else {
    5.95 +        /* The order of operations is important.
    5.96 +           We set the locking thread id after we obtain the lock
    5.97 +           so unlocks from other threads will fail.
    5.98 +         */
    5.99 +        SDL_SemWait(mutex->sem);
   5.100 +        mutex->owner = this_thread;
   5.101 +        mutex->recursive = 0;
   5.102 +    }
   5.103 +
   5.104 +    return 0;
   5.105 +#endif /* SDL_THREADS_DISABLED */
   5.106 +}
   5.107 +
   5.108 +/* Unlock the mutex */
   5.109 +extern "C"
   5.110 +int
   5.111 +SDL_mutexV(SDL_mutex * mutex)
   5.112 +{
   5.113 +#if SDL_THREADS_DISABLED
   5.114 +    return 0;
   5.115 +#else
   5.116 +    if (mutex == NULL) {
   5.117 +        SDL_SetError("Passed a NULL mutex");
   5.118 +        return -1;
   5.119 +    }
   5.120 +
   5.121 +    /* If we don't own the mutex, we can't unlock it */
   5.122 +    if (SDL_ThreadID() != mutex->owner) {
   5.123 +        SDL_SetError("mutex not owned by this thread");
   5.124 +        return -1;
   5.125 +    }
   5.126 +
   5.127 +    if (mutex->recursive) {
   5.128 +        --mutex->recursive;
   5.129 +    } else {
   5.130 +        /* The order of operations is important.
   5.131 +           First reset the owner so another thread doesn't lock
   5.132 +           the mutex and set the ownership before we reset it,
   5.133 +           then release the lock semaphore.
   5.134 +         */
   5.135 +        mutex->owner = 0;
   5.136 +        SDL_SemPost(mutex->sem);
   5.137 +    }
   5.138 +    return 0;
   5.139 +#endif /* SDL_THREADS_DISABLED */
   5.140 +}
   5.141 +
   5.142 +/* vi: set ts=4 sw=4 expandtab: */
     6.1 --- a/src/thread/stdcpp/SDL_systhread.c	Thu Nov 22 23:03:56 2012 -0500
     6.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
     6.3 @@ -1,59 +0,0 @@
     6.4 -/*
     6.5 -  Simple DirectMedia Layer
     6.6 -  Copyright (C) 1997-2012 Sam Lantinga <slouken@libsdl.org>
     6.7 -
     6.8 -  This software is provided 'as-is', without any express or implied
     6.9 -  warranty.  In no event will the authors be held liable for any damages
    6.10 -  arising from the use of this software.
    6.11 -
    6.12 -  Permission is granted to anyone to use this software for any purpose,
    6.13 -  including commercial applications, and to alter it and redistribute it
    6.14 -  freely, subject to the following restrictions:
    6.15 -
    6.16 -  1. The origin of this software must not be misrepresented; you must not
    6.17 -     claim that you wrote the original software. If you use this software
    6.18 -     in a product, an acknowledgment in the product documentation would be
    6.19 -     appreciated but is not required.
    6.20 -  2. Altered source versions must be plainly marked as such, and must not be
    6.21 -     misrepresented as being the original software.
    6.22 -  3. This notice may not be removed or altered from any source distribution.
    6.23 -*/
    6.24 -#include "SDL_config.h"
    6.25 -
    6.26 -/* Thread management routines for SDL */
    6.27 -
    6.28 -#include "SDL_thread.h"
    6.29 -#include "../SDL_systhread.h"
    6.30 -
    6.31 -int
    6.32 -SDL_SYS_CreateThread(SDL_Thread * thread, void *args)
    6.33 -{
    6.34 -    SDL_SetError("Threads are not supported on this platform");
    6.35 -    return (-1);
    6.36 -}
    6.37 -
    6.38 -void
    6.39 -SDL_SYS_SetupThread(const char *name)
    6.40 -{
    6.41 -    return;
    6.42 -}
    6.43 -
    6.44 -SDL_threadID
    6.45 -SDL_ThreadID(void)
    6.46 -{
    6.47 -    return (0);
    6.48 -}
    6.49 -
    6.50 -int
    6.51 -SDL_SYS_SetThreadPriority(SDL_ThreadPriority priority)
    6.52 -{
    6.53 -    return (0);
    6.54 -}
    6.55 -
    6.56 -void
    6.57 -SDL_SYS_WaitThread(SDL_Thread * thread)
    6.58 -{
    6.59 -    return;
    6.60 -}
    6.61 -
    6.62 -/* vi: set ts=4 sw=4 expandtab: */
     7.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     7.2 +++ b/src/thread/stdcpp/SDL_systhread.cpp	Thu Nov 22 23:12:06 2012 -0500
     7.3 @@ -0,0 +1,66 @@
     7.4 +/*
     7.5 +  Simple DirectMedia Layer
     7.6 +  Copyright (C) 1997-2012 Sam Lantinga <slouken@libsdl.org>
     7.7 +
     7.8 +  This software is provided 'as-is', without any express or implied
     7.9 +  warranty.  In no event will the authors be held liable for any damages
    7.10 +  arising from the use of this software.
    7.11 +
    7.12 +  Permission is granted to anyone to use this software for any purpose,
    7.13 +  including commercial applications, and to alter it and redistribute it
    7.14 +  freely, subject to the following restrictions:
    7.15 +
    7.16 +  1. The origin of this software must not be misrepresented; you must not
    7.17 +     claim that you wrote the original software. If you use this software
    7.18 +     in a product, an acknowledgment in the product documentation would be
    7.19 +     appreciated but is not required.
    7.20 +  2. Altered source versions must be plainly marked as such, and must not be
    7.21 +     misrepresented as being the original software.
    7.22 +  3. This notice may not be removed or altered from any source distribution.
    7.23 +*/
    7.24 +#include "SDL_config.h"
    7.25 +
    7.26 +/* Thread management routines for SDL */
    7.27 +
    7.28 +extern "C" {
    7.29 +#include "SDL_thread.h"
    7.30 +#include "../SDL_systhread.h"
    7.31 +}
    7.32 +
    7.33 +extern "C"
    7.34 +int
    7.35 +SDL_SYS_CreateThread(SDL_Thread * thread, void *args)
    7.36 +{
    7.37 +    SDL_SetError("Threads are not supported on this platform");
    7.38 +    return (-1);
    7.39 +}
    7.40 +
    7.41 +extern "C"
    7.42 +void
    7.43 +SDL_SYS_SetupThread(const char *name)
    7.44 +{
    7.45 +    return;
    7.46 +}
    7.47 +
    7.48 +extern "C"
    7.49 +SDL_threadID
    7.50 +SDL_ThreadID(void)
    7.51 +{
    7.52 +    return (0);
    7.53 +}
    7.54 +
    7.55 +extern "C"
    7.56 +int
    7.57 +SDL_SYS_SetThreadPriority(SDL_ThreadPriority priority)
    7.58 +{
    7.59 +    return (0);
    7.60 +}
    7.61 +
    7.62 +extern "C"
    7.63 +void
    7.64 +SDL_SYS_WaitThread(SDL_Thread * thread)
    7.65 +{
    7.66 +    return;
    7.67 +}
    7.68 +
    7.69 +/* vi: set ts=4 sw=4 expandtab: */