WinRT: added a skeleton SDL backend for C++11-based threads
authorDavid Ludwig
Thu, 22 Nov 2012 23:03:56 -0500
changeset 83564d85eba58f0a
parent 8355 f73480bf3e55
child 8357 8d788bb003f2
WinRT: added a skeleton SDL backend for C++11-based threads
VisualC/SDL/SDL_VS2012_WinRT.vcxproj
VisualC/tests/testthread/WinRT/Assets/Logo.png
VisualC/tests/testthread/WinRT/Assets/SmallLogo.png
VisualC/tests/testthread/WinRT/Assets/SplashScreen.png
VisualC/tests/testthread/WinRT/Assets/StoreLogo.png
VisualC/tests/testthread/WinRT/Package.appxmanifest
VisualC/tests/testthread/WinRT/testthread_VS2012_WinRT.vcxproj
VisualC/tests/testthread/WinRT/testthread_VS2012_WinRT_TemporaryKey.pfx
include/SDL_config_windowsrt.h
src/thread/SDL_thread_c.h
src/thread/stdcpp/SDL_syscond.c
src/thread/stdcpp/SDL_sysmutex.c
src/thread/stdcpp/SDL_sysmutex_c.h
src/thread/stdcpp/SDL_systhread.c
src/thread/stdcpp/SDL_systhread_c.h
     1.1 --- a/VisualC/SDL/SDL_VS2012_WinRT.vcxproj	Thu Nov 22 22:36:34 2012 -0500
     1.2 +++ b/VisualC/SDL/SDL_VS2012_WinRT.vcxproj	Thu Nov 22 23:03:56 2012 -0500
     1.3 @@ -89,11 +89,11 @@
     1.4      <ClCompile Include="..\..\src\stdlib\SDL_qsort.c" />
     1.5      <ClCompile Include="..\..\src\stdlib\SDL_stdlib.c" />
     1.6      <ClCompile Include="..\..\src\stdlib\SDL_string.c" />
     1.7 -    <ClCompile Include="..\..\src\thread\generic\SDL_syscond.c" />
     1.8 -    <ClCompile Include="..\..\src\thread\generic\SDL_sysmutex.c" />
     1.9      <ClCompile Include="..\..\src\thread\generic\SDL_syssem.c" />
    1.10 -    <ClCompile Include="..\..\src\thread\generic\SDL_systhread.c" />
    1.11      <ClCompile Include="..\..\src\thread\SDL_thread.c" />
    1.12 +    <ClCompile Include="..\..\src\thread\stdcpp\SDL_syscond.c" />
    1.13 +    <ClCompile Include="..\..\src\thread\stdcpp\SDL_sysmutex.c" />
    1.14 +    <ClCompile Include="..\..\src\thread\stdcpp\SDL_systhread.c" />
    1.15      <ClCompile Include="..\..\src\timer\dummy\SDL_systimer.c" />
    1.16      <ClCompile Include="..\..\src\timer\SDL_timer.c" />
    1.17      <ClCompile Include="..\..\src\video\dummy\SDL_nullevents.c" />
    1.18 @@ -253,10 +253,10 @@
    1.19      <ClInclude Include="..\..\src\SDL_error_c.h" />
    1.20      <ClInclude Include="..\..\src\SDL_fatal.h" />
    1.21      <ClInclude Include="..\..\src\SDL_hints_c.h" />
    1.22 -    <ClInclude Include="..\..\src\thread\generic\SDL_sysmutex_c.h" />
    1.23 -    <ClInclude Include="..\..\src\thread\generic\SDL_systhread_c.h" />
    1.24      <ClInclude Include="..\..\src\thread\SDL_systhread.h" />
    1.25      <ClInclude Include="..\..\src\thread\SDL_thread_c.h" />
    1.26 +    <ClInclude Include="..\..\src\thread\stdcpp\SDL_sysmutex_c.h" />
    1.27 +    <ClInclude Include="..\..\src\thread\stdcpp\SDL_systhread_c.h" />
    1.28      <ClInclude Include="..\..\src\timer\SDL_timer_c.h" />
    1.29      <ClInclude Include="..\..\src\video\dummy\SDL_nullevents_c.h" />
    1.30      <ClInclude Include="..\..\src\video\dummy\SDL_nullframebuffer_c.h" />
     2.1 Binary file VisualC/tests/testthread/WinRT/Assets/Logo.png has changed
     3.1 Binary file VisualC/tests/testthread/WinRT/Assets/SmallLogo.png has changed
     4.1 Binary file VisualC/tests/testthread/WinRT/Assets/SplashScreen.png has changed
     5.1 Binary file VisualC/tests/testthread/WinRT/Assets/StoreLogo.png has changed
     6.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     6.2 +++ b/VisualC/tests/testthread/WinRT/Package.appxmanifest	Thu Nov 22 23:03:56 2012 -0500
     6.3 @@ -0,0 +1,42 @@
     6.4 +<?xml version="1.0" encoding="utf-8"?>
     6.5 +<Package xmlns="http://schemas.microsoft.com/appx/2010/manifest">
     6.6 +
     6.7 +  <Identity Name="ca3178f1-b2b2-43bf-97dd-28ee1b7d32c5"
     6.8 +            Publisher="CN=David"
     6.9 +            Version="1.0.0.0" />
    6.10 +
    6.11 +  <Properties>
    6.12 +    <DisplayName>testthread_VS2012_WinRT</DisplayName>
    6.13 +    <PublisherDisplayName>David</PublisherDisplayName>
    6.14 +    <Logo>Assets\StoreLogo.png</Logo>
    6.15 +  </Properties>
    6.16 +
    6.17 +  <Prerequisites>
    6.18 +    <OSMinVersion>6.2.1</OSMinVersion>
    6.19 +    <OSMaxVersionTested>6.2.1</OSMaxVersionTested>
    6.20 +  </Prerequisites>
    6.21 +
    6.22 +  <Resources>
    6.23 +    <Resource Language="x-generate"/>
    6.24 +  </Resources>
    6.25 +
    6.26 +  <Applications>
    6.27 +    <Application Id="App"
    6.28 +        Executable="$targetnametoken$.exe"
    6.29 +        EntryPoint="testthread_VS2012_WinRT.App">
    6.30 +        <VisualElements
    6.31 +            DisplayName="testthread_VS2012_WinRT"
    6.32 +            Logo="Assets\Logo.png"
    6.33 +            SmallLogo="Assets\SmallLogo.png"
    6.34 +            Description="testthread_VS2012_WinRT"
    6.35 +            ForegroundText="light"
    6.36 +            BackgroundColor="#464646">
    6.37 +            <DefaultTile ShowName="allLogos" />
    6.38 +            <SplashScreen Image="Assets\SplashScreen.png" />
    6.39 +        </VisualElements>
    6.40 +    </Application>
    6.41 +  </Applications>
    6.42 +  <Capabilities>
    6.43 +    <Capability Name="internetClient" />
    6.44 +  </Capabilities>
    6.45 +</Package>
    6.46 \ No newline at end of file
     7.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     7.2 +++ b/VisualC/tests/testthread/WinRT/testthread_VS2012_WinRT.vcxproj	Thu Nov 22 23:03:56 2012 -0500
     7.3 @@ -0,0 +1,159 @@
     7.4 +<?xml version="1.0" encoding="utf-8"?>
     7.5 +<Project DefaultTargets="Build" ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
     7.6 +  <ItemGroup Label="ProjectConfigurations">
     7.7 +    <ProjectConfiguration Include="Debug|Win32">
     7.8 +      <Configuration>Debug</Configuration>
     7.9 +      <Platform>Win32</Platform>
    7.10 +    </ProjectConfiguration>
    7.11 +    <ProjectConfiguration Include="Release|Win32">
    7.12 +      <Configuration>Release</Configuration>
    7.13 +      <Platform>Win32</Platform>
    7.14 +    </ProjectConfiguration>
    7.15 +    <ProjectConfiguration Include="Debug|x64">
    7.16 +      <Configuration>Debug</Configuration>
    7.17 +      <Platform>x64</Platform>
    7.18 +    </ProjectConfiguration>
    7.19 +    <ProjectConfiguration Include="Release|x64">
    7.20 +      <Configuration>Release</Configuration>
    7.21 +      <Platform>x64</Platform>
    7.22 +    </ProjectConfiguration>
    7.23 +    <ProjectConfiguration Include="Debug|ARM">
    7.24 +      <Configuration>Debug</Configuration>
    7.25 +      <Platform>ARM</Platform>
    7.26 +    </ProjectConfiguration>
    7.27 +    <ProjectConfiguration Include="Release|ARM">
    7.28 +      <Configuration>Release</Configuration>
    7.29 +      <Platform>ARM</Platform>
    7.30 +    </ProjectConfiguration>
    7.31 +  </ItemGroup>
    7.32 +  <PropertyGroup Label="Globals">
    7.33 +    <ProjectGuid>{a8705bee-d01d-46a4-b2ab-feedfb5fdd11}</ProjectGuid>
    7.34 +    <RootNamespace>testthread_VS2012_WinRT</RootNamespace>
    7.35 +    <DefaultLanguage>en-US</DefaultLanguage>
    7.36 +    <MinimumVisualStudioVersion>11.0</MinimumVisualStudioVersion>
    7.37 +    <AppContainerApplication>true</AppContainerApplication>
    7.38 +  </PropertyGroup>
    7.39 +  <Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
    7.40 +  <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
    7.41 +    <ConfigurationType>Application</ConfigurationType>
    7.42 +    <UseDebugLibraries>true</UseDebugLibraries>
    7.43 +    <PlatformToolset>v110</PlatformToolset>
    7.44 +  </PropertyGroup>
    7.45 +  <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|ARM'" Label="Configuration">
    7.46 +    <ConfigurationType>Application</ConfigurationType>
    7.47 +    <UseDebugLibraries>true</UseDebugLibraries>
    7.48 +    <PlatformToolset>v110</PlatformToolset>
    7.49 +  </PropertyGroup>
    7.50 +  <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration">
    7.51 +    <ConfigurationType>Application</ConfigurationType>
    7.52 +    <UseDebugLibraries>true</UseDebugLibraries>
    7.53 +    <PlatformToolset>v110</PlatformToolset>
    7.54 +  </PropertyGroup>
    7.55 +  <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
    7.56 +    <ConfigurationType>Application</ConfigurationType>
    7.57 +    <UseDebugLibraries>false</UseDebugLibraries>
    7.58 +    <WholeProgramOptimization>true</WholeProgramOptimization>
    7.59 +    <PlatformToolset>v110</PlatformToolset>
    7.60 +  </PropertyGroup>
    7.61 +  <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|ARM'" Label="Configuration">
    7.62 +    <ConfigurationType>Application</ConfigurationType>
    7.63 +    <UseDebugLibraries>false</UseDebugLibraries>
    7.64 +    <WholeProgramOptimization>true</WholeProgramOptimization>
    7.65 +    <PlatformToolset>v110</PlatformToolset>
    7.66 +  </PropertyGroup>
    7.67 +  <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration">
    7.68 +    <ConfigurationType>Application</ConfigurationType>
    7.69 +    <UseDebugLibraries>false</UseDebugLibraries>
    7.70 +    <WholeProgramOptimization>true</WholeProgramOptimization>
    7.71 +    <PlatformToolset>v110</PlatformToolset>
    7.72 +  </PropertyGroup>
    7.73 +  <Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
    7.74 +  <ImportGroup Label="ExtensionSettings">
    7.75 +  </ImportGroup>
    7.76 +  <ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
    7.77 +    <Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
    7.78 +  </ImportGroup>
    7.79 +  <ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
    7.80 +    <Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
    7.81 +  </ImportGroup>
    7.82 +  <ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|ARM'">
    7.83 +    <Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
    7.84 +  </ImportGroup>
    7.85 +  <ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|ARM'">
    7.86 +    <Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
    7.87 +  </ImportGroup>
    7.88 +  <ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
    7.89 +    <Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
    7.90 +  </ImportGroup>
    7.91 +  <ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
    7.92 +    <Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
    7.93 +  </ImportGroup>
    7.94 +  <PropertyGroup Label="UserMacros" />
    7.95 +  <PropertyGroup>
    7.96 +    <PackageCertificateKeyFile>testthread_VS2012_WinRT_TemporaryKey.pfx</PackageCertificateKeyFile>
    7.97 +  </PropertyGroup>
    7.98 +  <ItemDefinitionGroup>
    7.99 +    <Link>
   7.100 +      <AdditionalDependencies>d2d1.lib; d3d11.lib; dxgi.lib; ole32.lib; windowscodecs.lib; dwrite.lib; %(AdditionalDependencies)</AdditionalDependencies>
   7.101 +    </Link>
   7.102 +    <ClCompile>
   7.103 +      <PrecompiledHeaderFile>pch.h</PrecompiledHeaderFile>
   7.104 +      <PrecompiledHeaderOutputFile>$(IntDir)pch.pch</PrecompiledHeaderOutputFile>
   7.105 +      <AdditionalIncludeDirectories>$(ProjectDir);$(IntermediateOutputPath);$(ProjectDir)..\..\..\..\include;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
   7.106 +      <DisableSpecificWarnings>4453</DisableSpecificWarnings>
   7.107 +    </ClCompile>
   7.108 +  </ItemDefinitionGroup>
   7.109 +  <ItemDefinitionGroup Condition="'$(Configuration)'=='Release'">
   7.110 +    <ClCompile>
   7.111 +      <PreprocessorDefinitions>NDEBUG;%(PreprocessorDefinitions)</PreprocessorDefinitions>
   7.112 +      <PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Release|ARM'">NotUsing</PrecompiledHeader>
   7.113 +      <PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">NotUsing</PrecompiledHeader>
   7.114 +      <PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Release|x64'">NotUsing</PrecompiledHeader>
   7.115 +      <CompileAsWinRT Condition="'$(Configuration)|$(Platform)'=='Release|ARM'">false</CompileAsWinRT>
   7.116 +      <CompileAsWinRT Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">false</CompileAsWinRT>
   7.117 +      <CompileAsWinRT Condition="'$(Configuration)|$(Platform)'=='Release|x64'">false</CompileAsWinRT>
   7.118 +    </ClCompile>
   7.119 +  </ItemDefinitionGroup>
   7.120 +  <ItemDefinitionGroup Condition="'$(Configuration)'=='Debug'">
   7.121 +    <ClCompile>
   7.122 +      <PreprocessorDefinitions>_DEBUG;%(PreprocessorDefinitions)</PreprocessorDefinitions>
   7.123 +      <PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Debug|ARM'">NotUsing</PrecompiledHeader>
   7.124 +      <PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">NotUsing</PrecompiledHeader>
   7.125 +      <PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">NotUsing</PrecompiledHeader>
   7.126 +      <CompileAsWinRT Condition="'$(Configuration)|$(Platform)'=='Debug|ARM'">false</CompileAsWinRT>
   7.127 +      <CompileAsWinRT Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">false</CompileAsWinRT>
   7.128 +      <CompileAsWinRT Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">false</CompileAsWinRT>
   7.129 +    </ClCompile>
   7.130 +  </ItemDefinitionGroup>
   7.131 +  <ItemGroup>
   7.132 +    <Image Include="Assets\Logo.png" />
   7.133 +    <Image Include="Assets\SmallLogo.png" />
   7.134 +    <Image Include="Assets\StoreLogo.png" />
   7.135 +    <Image Include="Assets\SplashScreen.png" />
   7.136 +  </ItemGroup>
   7.137 +  <ItemGroup>
   7.138 +    <AppxManifest Include="Package.appxmanifest">
   7.139 +      <SubType>Designer</SubType>
   7.140 +    </AppxManifest>
   7.141 +    <None Include="testthread_VS2012_WinRT_TemporaryKey.pfx" />
   7.142 +  </ItemGroup>
   7.143 +  <ItemGroup>
   7.144 +    <ClCompile Include="..\..\..\..\src\main\windowsrt\SDL_winrt_main.cpp">
   7.145 +      <CompileAsWinRT Condition="'$(Configuration)|$(Platform)'=='Debug|ARM'">true</CompileAsWinRT>
   7.146 +      <CompileAsWinRT Condition="'$(Configuration)|$(Platform)'=='Release|ARM'">true</CompileAsWinRT>
   7.147 +      <CompileAsWinRT Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">true</CompileAsWinRT>
   7.148 +      <CompileAsWinRT Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">true</CompileAsWinRT>
   7.149 +      <CompileAsWinRT Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">true</CompileAsWinRT>
   7.150 +      <CompileAsWinRT Condition="'$(Configuration)|$(Platform)'=='Release|x64'">true</CompileAsWinRT>
   7.151 +    </ClCompile>
   7.152 +    <ClCompile Include="..\..\..\..\test\testthread.c" />
   7.153 +  </ItemGroup>
   7.154 +  <ItemGroup>
   7.155 +    <ProjectReference Include="..\..\..\SDL\SDL_VS2012_WinRT.vcxproj">
   7.156 +      <Project>{aeaea3a2-d4e6-45b1-8ec6-53d84287fc14}</Project>
   7.157 +    </ProjectReference>
   7.158 +  </ItemGroup>
   7.159 +  <Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
   7.160 +  <ImportGroup Label="ExtensionTargets">
   7.161 +  </ImportGroup>
   7.162 +</Project>
   7.163 \ No newline at end of file
     8.1 Binary file VisualC/tests/testthread/WinRT/testthread_VS2012_WinRT_TemporaryKey.pfx has changed
     9.1 --- a/include/SDL_config_windowsrt.h	Thu Nov 22 22:36:34 2012 -0500
     9.2 +++ b/include/SDL_config_windowsrt.h	Thu Nov 22 23:03:56 2012 -0500
     9.3 @@ -158,9 +158,9 @@
     9.4  #define SDL_LOADSO_WINDOWS	1
     9.5  
     9.6  /* Enable various threading systems */
     9.7 -// TODO, WinRT: get threads working on WinRT
     9.8 -#define SDL_THREADS_DISABLED    1
     9.9 +//#define SDL_THREADS_DISABLED    1
    9.10  //#define SDL_THREAD_WINDOWS	1
    9.11 +#define SDL_THREAD_STDCPP   1
    9.12  
    9.13  /* Enable various timer systems */
    9.14  // TODO, WinRT: look into getting SDL's pre-WinRT timers working.
    10.1 --- a/src/thread/SDL_thread_c.h	Thu Nov 22 22:36:34 2012 -0500
    10.2 +++ b/src/thread/SDL_thread_c.h	Thu Nov 22 23:03:56 2012 -0500
    10.3 @@ -36,6 +36,8 @@
    10.4  #include "windows/SDL_systhread_c.h"
    10.5  #elif SDL_THREAD_NDS
    10.6  #include "nds/SDL_systhread_c.h"
    10.7 +#elif SDL_THREAD_STDCPP
    10.8 +#include "stdcpp/SDL_systhread_c.h"
    10.9  #else
   10.10  #error Need thread implementation for this platform
   10.11  #include "generic/SDL_systhread_c.h"
    11.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    11.2 +++ b/src/thread/stdcpp/SDL_syscond.c	Thu Nov 22 23:03:56 2012 -0500
    11.3 @@ -0,0 +1,223 @@
    11.4 +/*
    11.5 +  Simple DirectMedia Layer
    11.6 +  Copyright (C) 1997-2012 Sam Lantinga <slouken@libsdl.org>
    11.7 +
    11.8 +  This software is provided 'as-is', without any express or implied
    11.9 +  warranty.  In no event will the authors be held liable for any damages
   11.10 +  arising from the use of this software.
   11.11 +
   11.12 +  Permission is granted to anyone to use this software for any purpose,
   11.13 +  including commercial applications, and to alter it and redistribute it
   11.14 +  freely, subject to the following restrictions:
   11.15 +
   11.16 +  1. The origin of this software must not be misrepresented; you must not
   11.17 +     claim that you wrote the original software. If you use this software
   11.18 +     in a product, an acknowledgment in the product documentation would be
   11.19 +     appreciated but is not required.
   11.20 +  2. Altered source versions must be plainly marked as such, and must not be
   11.21 +     misrepresented as being the original software.
   11.22 +  3. This notice may not be removed or altered from any source distribution.
   11.23 +*/
   11.24 +#include "SDL_config.h"
   11.25 +
   11.26 +/* An implementation of condition variables using semaphores and mutexes */
   11.27 +/*
   11.28 +   This implementation borrows heavily from the BeOS condition variable
   11.29 +   implementation, written by Christopher Tate and Owen Smith.  Thanks!
   11.30 + */
   11.31 +
   11.32 +#include "SDL_thread.h"
   11.33 +
   11.34 +struct SDL_cond
   11.35 +{
   11.36 +    SDL_mutex *lock;
   11.37 +    int waiting;
   11.38 +    int signals;
   11.39 +    SDL_sem *wait_sem;
   11.40 +    SDL_sem *wait_done;
   11.41 +};
   11.42 +
   11.43 +/* Create a condition variable */
   11.44 +SDL_cond *
   11.45 +SDL_CreateCond(void)
   11.46 +{
   11.47 +    SDL_cond *cond;
   11.48 +
   11.49 +    cond = (SDL_cond *) SDL_malloc(sizeof(SDL_cond));
   11.50 +    if (cond) {
   11.51 +        cond->lock = SDL_CreateMutex();
   11.52 +        cond->wait_sem = SDL_CreateSemaphore(0);
   11.53 +        cond->wait_done = SDL_CreateSemaphore(0);
   11.54 +        cond->waiting = cond->signals = 0;
   11.55 +        if (!cond->lock || !cond->wait_sem || !cond->wait_done) {
   11.56 +            SDL_DestroyCond(cond);
   11.57 +            cond = NULL;
   11.58 +        }
   11.59 +    } else {
   11.60 +        SDL_OutOfMemory();
   11.61 +    }
   11.62 +    return (cond);
   11.63 +}
   11.64 +
   11.65 +/* Destroy a condition variable */
   11.66 +void
   11.67 +SDL_DestroyCond(SDL_cond * cond)
   11.68 +{
   11.69 +    if (cond) {
   11.70 +        if (cond->wait_sem) {
   11.71 +            SDL_DestroySemaphore(cond->wait_sem);
   11.72 +        }
   11.73 +        if (cond->wait_done) {
   11.74 +            SDL_DestroySemaphore(cond->wait_done);
   11.75 +        }
   11.76 +        if (cond->lock) {
   11.77 +            SDL_DestroyMutex(cond->lock);
   11.78 +        }
   11.79 +        SDL_free(cond);
   11.80 +    }
   11.81 +}
   11.82 +
   11.83 +/* Restart one of the threads that are waiting on the condition variable */
   11.84 +int
   11.85 +SDL_CondSignal(SDL_cond * cond)
   11.86 +{
   11.87 +    if (!cond) {
   11.88 +        SDL_SetError("Passed a NULL condition variable");
   11.89 +        return -1;
   11.90 +    }
   11.91 +
   11.92 +    /* If there are waiting threads not already signalled, then
   11.93 +       signal the condition and wait for the thread to respond.
   11.94 +     */
   11.95 +    SDL_LockMutex(cond->lock);
   11.96 +    if (cond->waiting > cond->signals) {
   11.97 +        ++cond->signals;
   11.98 +        SDL_SemPost(cond->wait_sem);
   11.99 +        SDL_UnlockMutex(cond->lock);
  11.100 +        SDL_SemWait(cond->wait_done);
  11.101 +    } else {
  11.102 +        SDL_UnlockMutex(cond->lock);
  11.103 +    }
  11.104 +
  11.105 +    return 0;
  11.106 +}
  11.107 +
  11.108 +/* Restart all threads that are waiting on the condition variable */
  11.109 +int
  11.110 +SDL_CondBroadcast(SDL_cond * cond)
  11.111 +{
  11.112 +    if (!cond) {
  11.113 +        SDL_SetError("Passed a NULL condition variable");
  11.114 +        return -1;
  11.115 +    }
  11.116 +
  11.117 +    /* If there are waiting threads not already signalled, then
  11.118 +       signal the condition and wait for the thread to respond.
  11.119 +     */
  11.120 +    SDL_LockMutex(cond->lock);
  11.121 +    if (cond->waiting > cond->signals) {
  11.122 +        int i, num_waiting;
  11.123 +
  11.124 +        num_waiting = (cond->waiting - cond->signals);
  11.125 +        cond->signals = cond->waiting;
  11.126 +        for (i = 0; i < num_waiting; ++i) {
  11.127 +            SDL_SemPost(cond->wait_sem);
  11.128 +        }
  11.129 +        /* Now all released threads are blocked here, waiting for us.
  11.130 +           Collect them all (and win fabulous prizes!) :-)
  11.131 +         */
  11.132 +        SDL_UnlockMutex(cond->lock);
  11.133 +        for (i = 0; i < num_waiting; ++i) {
  11.134 +            SDL_SemWait(cond->wait_done);
  11.135 +        }
  11.136 +    } else {
  11.137 +        SDL_UnlockMutex(cond->lock);
  11.138 +    }
  11.139 +
  11.140 +    return 0;
  11.141 +}
  11.142 +
  11.143 +/* Wait on the condition variable for at most 'ms' milliseconds.
  11.144 +   The mutex must be locked before entering this function!
  11.145 +   The mutex is unlocked during the wait, and locked again after the wait.
  11.146 +
  11.147 +Typical use:
  11.148 +
  11.149 +Thread A:
  11.150 +    SDL_LockMutex(lock);
  11.151 +    while ( ! condition ) {
  11.152 +        SDL_CondWait(cond, lock);
  11.153 +    }
  11.154 +    SDL_UnlockMutex(lock);
  11.155 +
  11.156 +Thread B:
  11.157 +    SDL_LockMutex(lock);
  11.158 +    ...
  11.159 +    condition = true;
  11.160 +    ...
  11.161 +    SDL_CondSignal(cond);
  11.162 +    SDL_UnlockMutex(lock);
  11.163 + */
  11.164 +int
  11.165 +SDL_CondWaitTimeout(SDL_cond * cond, SDL_mutex * mutex, Uint32 ms)
  11.166 +{
  11.167 +    int retval;
  11.168 +
  11.169 +    if (!cond) {
  11.170 +        SDL_SetError("Passed a NULL condition variable");
  11.171 +        return -1;
  11.172 +    }
  11.173 +
  11.174 +    /* Obtain the protection mutex, and increment the number of waiters.
  11.175 +       This allows the signal mechanism to only perform a signal if there
  11.176 +       are waiting threads.
  11.177 +     */
  11.178 +    SDL_LockMutex(cond->lock);
  11.179 +    ++cond->waiting;
  11.180 +    SDL_UnlockMutex(cond->lock);
  11.181 +
  11.182 +    /* Unlock the mutex, as is required by condition variable semantics */
  11.183 +    SDL_UnlockMutex(mutex);
  11.184 +
  11.185 +    /* Wait for a signal */
  11.186 +    if (ms == SDL_MUTEX_MAXWAIT) {
  11.187 +        retval = SDL_SemWait(cond->wait_sem);
  11.188 +    } else {
  11.189 +        retval = SDL_SemWaitTimeout(cond->wait_sem, ms);
  11.190 +    }
  11.191 +
  11.192 +    /* Let the signaler know we have completed the wait, otherwise
  11.193 +       the signaler can race ahead and get the condition semaphore
  11.194 +       if we are stopped between the mutex unlock and semaphore wait,
  11.195 +       giving a deadlock.  See the following URL for details:
  11.196 +       http://www-classic.be.com/aboutbe/benewsletter/volume_III/Issue40.html
  11.197 +     */
  11.198 +    SDL_LockMutex(cond->lock);
  11.199 +    if (cond->signals > 0) {
  11.200 +        /* If we timed out, we need to eat a condition signal */
  11.201 +        if (retval > 0) {
  11.202 +            SDL_SemWait(cond->wait_sem);
  11.203 +        }
  11.204 +        /* We always notify the signal thread that we are done */
  11.205 +        SDL_SemPost(cond->wait_done);
  11.206 +
  11.207 +        /* Signal handshake complete */
  11.208 +        --cond->signals;
  11.209 +    }
  11.210 +    --cond->waiting;
  11.211 +    SDL_UnlockMutex(cond->lock);
  11.212 +
  11.213 +    /* Lock the mutex, as is required by condition variable semantics */
  11.214 +    SDL_LockMutex(mutex);
  11.215 +
  11.216 +    return retval;
  11.217 +}
  11.218 +
  11.219 +/* Wait on the condition variable forever */
  11.220 +int
  11.221 +SDL_CondWait(SDL_cond * cond, SDL_mutex * mutex)
  11.222 +{
  11.223 +    return SDL_CondWaitTimeout(cond, mutex, SDL_MUTEX_MAXWAIT);
  11.224 +}
  11.225 +
  11.226 +/* vi: set ts=4 sw=4 expandtab: */
    12.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    12.2 +++ b/src/thread/stdcpp/SDL_sysmutex.c	Thu Nov 22 23:03:56 2012 -0500
    12.3 @@ -0,0 +1,135 @@
    12.4 +/*
    12.5 +  Simple DirectMedia Layer
    12.6 +  Copyright (C) 1997-2012 Sam Lantinga <slouken@libsdl.org>
    12.7 +
    12.8 +  This software is provided 'as-is', without any express or implied
    12.9 +  warranty.  In no event will the authors be held liable for any damages
   12.10 +  arising from the use of this software.
   12.11 +
   12.12 +  Permission is granted to anyone to use this software for any purpose,
   12.13 +  including commercial applications, and to alter it and redistribute it
   12.14 +  freely, subject to the following restrictions:
   12.15 +
   12.16 +  1. The origin of this software must not be misrepresented; you must not
   12.17 +     claim that you wrote the original software. If you use this software
   12.18 +     in a product, an acknowledgment in the product documentation would be
   12.19 +     appreciated but is not required.
   12.20 +  2. Altered source versions must be plainly marked as such, and must not be
   12.21 +     misrepresented as being the original software.
   12.22 +  3. This notice may not be removed or altered from any source distribution.
   12.23 +*/
   12.24 +#include "SDL_config.h"
   12.25 +
   12.26 +/* An implementation of mutexes using semaphores */
   12.27 +
   12.28 +#include "SDL_thread.h"
   12.29 +#include "SDL_systhread_c.h"
   12.30 +
   12.31 +
   12.32 +struct SDL_mutex
   12.33 +{
   12.34 +    int recursive;
   12.35 +    SDL_threadID owner;
   12.36 +    SDL_sem *sem;
   12.37 +};
   12.38 +
   12.39 +/* Create a mutex */
   12.40 +SDL_mutex *
   12.41 +SDL_CreateMutex(void)
   12.42 +{
   12.43 +    SDL_mutex *mutex;
   12.44 +
   12.45 +    /* Allocate mutex memory */
   12.46 +    mutex = (SDL_mutex *) SDL_malloc(sizeof(*mutex));
   12.47 +    if (mutex) {
   12.48 +        /* Create the mutex semaphore, with initial value 1 */
   12.49 +        mutex->sem = SDL_CreateSemaphore(1);
   12.50 +        mutex->recursive = 0;
   12.51 +        mutex->owner = 0;
   12.52 +        if (!mutex->sem) {
   12.53 +            SDL_free(mutex);
   12.54 +            mutex = NULL;
   12.55 +        }
   12.56 +    } else {
   12.57 +        SDL_OutOfMemory();
   12.58 +    }
   12.59 +    return mutex;
   12.60 +}
   12.61 +
   12.62 +/* Free the mutex */
   12.63 +void
   12.64 +SDL_DestroyMutex(SDL_mutex * mutex)
   12.65 +{
   12.66 +    if (mutex) {
   12.67 +        if (mutex->sem) {
   12.68 +            SDL_DestroySemaphore(mutex->sem);
   12.69 +        }
   12.70 +        SDL_free(mutex);
   12.71 +    }
   12.72 +}
   12.73 +
   12.74 +/* Lock the semaphore */
   12.75 +int
   12.76 +SDL_mutexP(SDL_mutex * mutex)
   12.77 +{
   12.78 +#if SDL_THREADS_DISABLED
   12.79 +    return 0;
   12.80 +#else
   12.81 +    SDL_threadID this_thread;
   12.82 +
   12.83 +    if (mutex == NULL) {
   12.84 +        SDL_SetError("Passed a NULL mutex");
   12.85 +        return -1;
   12.86 +    }
   12.87 +
   12.88 +    this_thread = SDL_ThreadID();
   12.89 +    if (mutex->owner == this_thread) {
   12.90 +        ++mutex->recursive;
   12.91 +    } else {
   12.92 +        /* The order of operations is important.
   12.93 +           We set the locking thread id after we obtain the lock
   12.94 +           so unlocks from other threads will fail.
   12.95 +         */
   12.96 +        SDL_SemWait(mutex->sem);
   12.97 +        mutex->owner = this_thread;
   12.98 +        mutex->recursive = 0;
   12.99 +    }
  12.100 +
  12.101 +    return 0;
  12.102 +#endif /* SDL_THREADS_DISABLED */
  12.103 +}
  12.104 +
  12.105 +/* Unlock the mutex */
  12.106 +int
  12.107 +SDL_mutexV(SDL_mutex * mutex)
  12.108 +{
  12.109 +#if SDL_THREADS_DISABLED
  12.110 +    return 0;
  12.111 +#else
  12.112 +    if (mutex == NULL) {
  12.113 +        SDL_SetError("Passed a NULL mutex");
  12.114 +        return -1;
  12.115 +    }
  12.116 +
  12.117 +    /* If we don't own the mutex, we can't unlock it */
  12.118 +    if (SDL_ThreadID() != mutex->owner) {
  12.119 +        SDL_SetError("mutex not owned by this thread");
  12.120 +        return -1;
  12.121 +    }
  12.122 +
  12.123 +    if (mutex->recursive) {
  12.124 +        --mutex->recursive;
  12.125 +    } else {
  12.126 +        /* The order of operations is important.
  12.127 +           First reset the owner so another thread doesn't lock
  12.128 +           the mutex and set the ownership before we reset it,
  12.129 +           then release the lock semaphore.
  12.130 +         */
  12.131 +        mutex->owner = 0;
  12.132 +        SDL_SemPost(mutex->sem);
  12.133 +    }
  12.134 +    return 0;
  12.135 +#endif /* SDL_THREADS_DISABLED */
  12.136 +}
  12.137 +
  12.138 +/* vi: set ts=4 sw=4 expandtab: */
    13.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    13.2 +++ b/src/thread/stdcpp/SDL_sysmutex_c.h	Thu Nov 22 23:03:56 2012 -0500
    13.3 @@ -0,0 +1,22 @@
    13.4 +/*
    13.5 +  Simple DirectMedia Layer
    13.6 +  Copyright (C) 1997-2012 Sam Lantinga <slouken@libsdl.org>
    13.7 +
    13.8 +  This software is provided 'as-is', without any express or implied
    13.9 +  warranty.  In no event will the authors be held liable for any damages
   13.10 +  arising from the use of this software.
   13.11 +
   13.12 +  Permission is granted to anyone to use this software for any purpose,
   13.13 +  including commercial applications, and to alter it and redistribute it
   13.14 +  freely, subject to the following restrictions:
   13.15 +
   13.16 +  1. The origin of this software must not be misrepresented; you must not
   13.17 +     claim that you wrote the original software. If you use this software
   13.18 +     in a product, an acknowledgment in the product documentation would be
   13.19 +     appreciated but is not required.
   13.20 +  2. Altered source versions must be plainly marked as such, and must not be
   13.21 +     misrepresented as being the original software.
   13.22 +  3. This notice may not be removed or altered from any source distribution.
   13.23 +*/
   13.24 +#include "SDL_config.h"
   13.25 +/* vi: set ts=4 sw=4 expandtab: */
    14.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    14.2 +++ b/src/thread/stdcpp/SDL_systhread.c	Thu Nov 22 23:03:56 2012 -0500
    14.3 @@ -0,0 +1,59 @@
    14.4 +/*
    14.5 +  Simple DirectMedia Layer
    14.6 +  Copyright (C) 1997-2012 Sam Lantinga <slouken@libsdl.org>
    14.7 +
    14.8 +  This software is provided 'as-is', without any express or implied
    14.9 +  warranty.  In no event will the authors be held liable for any damages
   14.10 +  arising from the use of this software.
   14.11 +
   14.12 +  Permission is granted to anyone to use this software for any purpose,
   14.13 +  including commercial applications, and to alter it and redistribute it
   14.14 +  freely, subject to the following restrictions:
   14.15 +
   14.16 +  1. The origin of this software must not be misrepresented; you must not
   14.17 +     claim that you wrote the original software. If you use this software
   14.18 +     in a product, an acknowledgment in the product documentation would be
   14.19 +     appreciated but is not required.
   14.20 +  2. Altered source versions must be plainly marked as such, and must not be
   14.21 +     misrepresented as being the original software.
   14.22 +  3. This notice may not be removed or altered from any source distribution.
   14.23 +*/
   14.24 +#include "SDL_config.h"
   14.25 +
   14.26 +/* Thread management routines for SDL */
   14.27 +
   14.28 +#include "SDL_thread.h"
   14.29 +#include "../SDL_systhread.h"
   14.30 +
   14.31 +int
   14.32 +SDL_SYS_CreateThread(SDL_Thread * thread, void *args)
   14.33 +{
   14.34 +    SDL_SetError("Threads are not supported on this platform");
   14.35 +    return (-1);
   14.36 +}
   14.37 +
   14.38 +void
   14.39 +SDL_SYS_SetupThread(const char *name)
   14.40 +{
   14.41 +    return;
   14.42 +}
   14.43 +
   14.44 +SDL_threadID
   14.45 +SDL_ThreadID(void)
   14.46 +{
   14.47 +    return (0);
   14.48 +}
   14.49 +
   14.50 +int
   14.51 +SDL_SYS_SetThreadPriority(SDL_ThreadPriority priority)
   14.52 +{
   14.53 +    return (0);
   14.54 +}
   14.55 +
   14.56 +void
   14.57 +SDL_SYS_WaitThread(SDL_Thread * thread)
   14.58 +{
   14.59 +    return;
   14.60 +}
   14.61 +
   14.62 +/* vi: set ts=4 sw=4 expandtab: */
    15.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    15.2 +++ b/src/thread/stdcpp/SDL_systhread_c.h	Thu Nov 22 23:03:56 2012 -0500
    15.3 @@ -0,0 +1,26 @@
    15.4 +/*
    15.5 +  Simple DirectMedia Layer
    15.6 +  Copyright (C) 1997-2012 Sam Lantinga <slouken@libsdl.org>
    15.7 +
    15.8 +  This software is provided 'as-is', without any express or implied
    15.9 +  warranty.  In no event will the authors be held liable for any damages
   15.10 +  arising from the use of this software.
   15.11 +
   15.12 +  Permission is granted to anyone to use this software for any purpose,
   15.13 +  including commercial applications, and to alter it and redistribute it
   15.14 +  freely, subject to the following restrictions:
   15.15 +
   15.16 +  1. The origin of this software must not be misrepresented; you must not
   15.17 +     claim that you wrote the original software. If you use this software
   15.18 +     in a product, an acknowledgment in the product documentation would be
   15.19 +     appreciated but is not required.
   15.20 +  2. Altered source versions must be plainly marked as such, and must not be
   15.21 +     misrepresented as being the original software.
   15.22 +  3. This notice may not be removed or altered from any source distribution.
   15.23 +*/
   15.24 +#include "SDL_config.h"
   15.25 +
   15.26 +/* For a thread handle, use a void pointer to a std::thread */
   15.27 +typedef void * SYS_ThreadHandle;
   15.28 +
   15.29 +/* vi: set ts=4 sw=4 expandtab: */