Skip to content
This repository has been archived by the owner on Feb 11, 2021. It is now read-only.

Commit

Permalink
Browse files Browse the repository at this point in the history
Implemented an API for thread-local storage: SDL_TLSCreate(), SDL_TLS…
…Set(), SDL_TLSGet()
  • Loading branch information
slouken committed Jul 10, 2013
1 parent f728a26 commit 9b37546
Show file tree
Hide file tree
Showing 14 changed files with 617 additions and 155 deletions.
4 changes: 4 additions & 0 deletions VisualC/SDL/SDL_VS2008.vcproj
Expand Up @@ -1132,6 +1132,10 @@
RelativePath="..\..\src\thread\windows\SDL_systhread.c"
>
</File>
<File
RelativePath="..\..\src\thread\windows\SDL_systls.c"
>
</File>
<File
RelativePath="..\..\src\thread\SDL_systhread.h"
>
Expand Down
3 changes: 2 additions & 1 deletion VisualC/SDL/SDL_VS2010.vcxproj
Expand Up @@ -438,6 +438,7 @@
<ClCompile Include="..\..\src\power\windows\SDL_syspower.c" />
<ClCompile Include="..\..\src\thread\windows\SDL_syssem.c" />
<ClCompile Include="..\..\src\thread\windows\SDL_systhread.c" />
<ClCompile Include="..\..\src\thread\windows\SDL_systls.c" />
<ClCompile Include="..\..\src\timer\windows\SDL_systimer.c" />
<ClCompile Include="..\..\src\thread\SDL_thread.c" />
<ClCompile Include="..\..\src\timer\SDL_timer.c" />
Expand All @@ -462,4 +463,4 @@
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
</ImportGroup>
</Project>
</Project>
3 changes: 2 additions & 1 deletion VisualC/SDL/SDL_VS2012.vcxproj
Expand Up @@ -441,6 +441,7 @@
<ClCompile Include="..\..\src\power\windows\SDL_syspower.c" />
<ClCompile Include="..\..\src\thread\windows\SDL_syssem.c" />
<ClCompile Include="..\..\src\thread\windows\SDL_systhread.c" />
<ClCompile Include="..\..\src\thread\windows\SDL_systls.c" />
<ClCompile Include="..\..\src\timer\windows\SDL_systimer.c" />
<ClCompile Include="..\..\src\thread\SDL_thread.c" />
<ClCompile Include="..\..\src\timer\SDL_timer.c" />
Expand All @@ -466,4 +467,4 @@
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
</ImportGroup>
</Project>
</Project>
4 changes: 4 additions & 0 deletions Xcode-iOS/SDL/SDL.xcodeproj/project.pbxproj
Expand Up @@ -76,6 +76,7 @@
93CB792613FC5F5300BD3E05 /* SDL_uikitviewcontroller.m in Sources */ = {isa = PBXBuildFile; fileRef = 93CB792513FC5F5300BD3E05 /* SDL_uikitviewcontroller.m */; };
AA0AD06216647BBB00CE5896 /* SDL_gamecontroller.c in Sources */ = {isa = PBXBuildFile; fileRef = AA0AD06116647BBB00CE5896 /* SDL_gamecontroller.c */; };
AA0AD06516647BD400CE5896 /* SDL_gamecontroller.h in Headers */ = {isa = PBXBuildFile; fileRef = AA0AD06416647BD400CE5896 /* SDL_gamecontroller.h */; };
AA0F8495178D5F1A00823F9D /* SDL_systls.c in Sources */ = {isa = PBXBuildFile; fileRef = AA0F8494178D5F1A00823F9D /* SDL_systls.c */; };
AA126AD41617C5E7005ABC8F /* SDL_uikitmodes.h in Headers */ = {isa = PBXBuildFile; fileRef = AA126AD21617C5E6005ABC8F /* SDL_uikitmodes.h */; };
AA126AD51617C5E7005ABC8F /* SDL_uikitmodes.m in Sources */ = {isa = PBXBuildFile; fileRef = AA126AD31617C5E6005ABC8F /* SDL_uikitmodes.m */; };
AA628ADB159369E3005138DD /* SDL_rotate.c in Sources */ = {isa = PBXBuildFile; fileRef = AA628AD9159369E3005138DD /* SDL_rotate.c */; };
Expand Down Expand Up @@ -268,6 +269,7 @@
93CB792513FC5F5300BD3E05 /* SDL_uikitviewcontroller.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = SDL_uikitviewcontroller.m; sourceTree = "<group>"; };
AA0AD06116647BBB00CE5896 /* SDL_gamecontroller.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = SDL_gamecontroller.c; sourceTree = "<group>"; };
AA0AD06416647BD400CE5896 /* SDL_gamecontroller.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SDL_gamecontroller.h; sourceTree = "<group>"; };
AA0F8494178D5F1A00823F9D /* SDL_systls.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = SDL_systls.c; sourceTree = "<group>"; };
AA126AD21617C5E6005ABC8F /* SDL_uikitmodes.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SDL_uikitmodes.h; sourceTree = "<group>"; };
AA126AD31617C5E6005ABC8F /* SDL_uikitmodes.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = SDL_uikitmodes.m; sourceTree = "<group>"; };
AA628AD9159369E3005138DD /* SDL_rotate.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = SDL_rotate.c; sourceTree = "<group>"; };
Expand Down Expand Up @@ -851,6 +853,7 @@
FD99BA0A0DD52EDC00FB1D6B /* SDL_syssem.c */,
FD99BA0B0DD52EDC00FB1D6B /* SDL_systhread.c */,
FD99BA0C0DD52EDC00FB1D6B /* SDL_systhread_c.h */,
AA0F8494178D5F1A00823F9D /* SDL_systls.c */,
);
path = pthread;
sourceTree = "<group>";
Expand Down Expand Up @@ -1189,6 +1192,7 @@
AA704DD7162AA90A0076D1C1 /* SDL_dropevents.c in Sources */,
AABCC3951640643D00AB8930 /* SDL_uikitmessagebox.m in Sources */,
AA0AD06216647BBB00CE5896 /* SDL_gamecontroller.c in Sources */,
AA0F8495178D5F1A00823F9D /* SDL_systls.c in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down
8 changes: 8 additions & 0 deletions Xcode/SDL/SDL.xcodeproj/project.pbxproj
Expand Up @@ -398,6 +398,9 @@
A77E6EB4167AB0A90010E40B /* SDL_gamecontroller.h in Headers */ = {isa = PBXBuildFile; fileRef = A77E6EB3167AB0A90010E40B /* SDL_gamecontroller.h */; settings = {ATTRIBUTES = (Public, ); }; };
A77E6EB5167AB0A90010E40B /* SDL_gamecontroller.h in Headers */ = {isa = PBXBuildFile; fileRef = A77E6EB3167AB0A90010E40B /* SDL_gamecontroller.h */; };
AA0AD09D16648D1700CE5896 /* SDL_gamecontroller.c in Sources */ = {isa = PBXBuildFile; fileRef = BBFC088A164C6514003E6A99 /* SDL_gamecontroller.c */; };
AA0F8491178D5ECC00823F9D /* SDL_systls.c in Sources */ = {isa = PBXBuildFile; fileRef = AA0F8490178D5ECC00823F9D /* SDL_systls.c */; };
AA0F8492178D5ECC00823F9D /* SDL_systls.c in Sources */ = {isa = PBXBuildFile; fileRef = AA0F8490178D5ECC00823F9D /* SDL_systls.c */; };
AA0F8493178D5ECC00823F9D /* SDL_systls.c in Sources */ = {isa = PBXBuildFile; fileRef = AA0F8490178D5ECC00823F9D /* SDL_systls.c */; };
AA41F88014B8F1F500993C4F /* SDL_dropevents.c in Sources */ = {isa = PBXBuildFile; fileRef = 566CDE8E148F0AC200C5A9BB /* SDL_dropevents.c */; };
AA628ACA159367B7005138DD /* SDL_rotate.c in Sources */ = {isa = PBXBuildFile; fileRef = AA628AC8159367B7005138DD /* SDL_rotate.c */; };
AA628ACB159367B7005138DD /* SDL_rotate.c in Sources */ = {isa = PBXBuildFile; fileRef = AA628AC8159367B7005138DD /* SDL_rotate.c */; };
Expand Down Expand Up @@ -984,6 +987,7 @@
566CDE8D148F0AC200C5A9BB /* SDL_dropevents_c.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SDL_dropevents_c.h; sourceTree = "<group>"; };
566CDE8E148F0AC200C5A9BB /* SDL_dropevents.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = SDL_dropevents.c; sourceTree = "<group>"; };
A77E6EB3167AB0A90010E40B /* SDL_gamecontroller.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SDL_gamecontroller.h; sourceTree = "<group>"; };
AA0F8490178D5ECC00823F9D /* SDL_systls.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = SDL_systls.c; sourceTree = "<group>"; };
AA628AC8159367B7005138DD /* SDL_rotate.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = SDL_rotate.c; sourceTree = "<group>"; };
AA628AC9159367B7005138DD /* SDL_rotate.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SDL_rotate.h; sourceTree = "<group>"; };
AA628ACF159367F2005138DD /* SDL_x11xinput2.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = SDL_x11xinput2.c; sourceTree = "<group>"; };
Expand Down Expand Up @@ -1462,6 +1466,7 @@
04BDFE8112E6671800899322 /* SDL_syssem.c */,
04BDFE8212E6671800899322 /* SDL_systhread.c */,
04BDFE8312E6671800899322 /* SDL_systhread_c.h */,
AA0F8490178D5ECC00823F9D /* SDL_systls.c */,
);
path = pthread;
sourceTree = "<group>";
Expand Down Expand Up @@ -2403,6 +2408,7 @@
AA9E4093163BE51E007A2AD0 /* SDL_x11messagebox.c in Sources */,
AABCC38F164063D200AB8930 /* SDL_cocoamessagebox.m in Sources */,
AA0AD09D16648D1700CE5896 /* SDL_gamecontroller.c in Sources */,
AA0F8491178D5ECC00823F9D /* SDL_systls.c in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down Expand Up @@ -2519,6 +2525,7 @@
AA628AD2159367F2005138DD /* SDL_x11xinput2.c in Sources */,
AA9E4094163BE51E007A2AD0 /* SDL_x11messagebox.c in Sources */,
AABCC390164063D200AB8930 /* SDL_cocoamessagebox.m in Sources */,
AA0F8492178D5ECC00823F9D /* SDL_systls.c in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down Expand Up @@ -2635,6 +2642,7 @@
DB31406817554B71006C0E22 /* SDL_x11xinput2.c in Sources */,
DB31406917554B71006C0E22 /* SDL_x11messagebox.c in Sources */,
DB31406A17554B71006C0E22 /* SDL_cocoamessagebox.m in Sources */,
AA0F8493178D5ECC00823F9D /* SDL_systls.c in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down
25 changes: 19 additions & 6 deletions configure
Expand Up @@ -1511,7 +1511,7 @@ Optional Features:
--enable-sse use SSE assembly routines [[default=yes]]
--enable-sse2 use SSE2 assembly routines [[default=no]]
--enable-altivec use Altivec assembly routines [[default=yes]]
--enable-oss support the OSS audio API [[default=yes]]
--enable-oss support the OSS audio API [[default=maybe]]
--enable-alsa support the ALSA audio API [[default=yes]]
--disable-alsatest Do not try to compile and run a test Alsa program
--enable-alsa-shared dynamically load ALSA audio support [[default=yes]]
Expand Down Expand Up @@ -17535,9 +17535,21 @@ CheckOSS()
if test "${enable_oss+set}" = set; then :
enableval=$enable_oss;
else
enable_oss=yes
enable_oss=maybe
fi


# OpenBSD "has" OSS, but it's not really for app use. They want you to
# use sndio instead. So on there, we default to disabled. You can force
# it on if you really want, though.
if test x$enable_oss = xmaybe; then
enable_oss=yes
case "$host" in
*-*-openbsd*)
enable_oss=no;;
esac
fi

if test x$enable_audio = xyes -a x$enable_oss = xyes; then
{ $as_echo "$as_me:${as_lineno-$LINENO}: checking for OSS audio support" >&5
$as_echo_n "checking for OSS audio support... " >&6; }
Expand Down Expand Up @@ -21423,6 +21435,9 @@ $as_echo "$has_pthread_set_name_np" >&6; }
# We can fake these with semaphores and mutexes if necessary
SOURCES="$SOURCES $srcdir/src/thread/pthread/SDL_syscond.c"

# Thread local storage
SOURCES="$SOURCES $srcdir/src/thread/pthread/SDL_systls.c"

have_threads=yes
fi
fi
Expand Down Expand Up @@ -22206,7 +22221,7 @@ $as_echo "#define SDL_AUDIO_DRIVER_SUNAUDIO 1" >>confdefs.h
SOURCES="$SOURCES $srcdir/src/audio/sun/*.c"
have_audio=yes
;;
netbsd|openbsd)
netbsd) # Don't use this on OpenBSD, it's busted.

$as_echo "#define SDL_AUDIO_DRIVER_BSD 1" >>confdefs.h

Expand Down Expand Up @@ -22367,9 +22382,7 @@ $as_echo "#define SDL_POWER_WINDOWS 1" >>confdefs.h

$as_echo "#define SDL_THREAD_WINDOWS 1" >>confdefs.h

SOURCES="$SOURCES $srcdir/src/thread/windows/SDL_sysmutex.c"
SOURCES="$SOURCES $srcdir/src/thread/windows/SDL_syssem.c"
SOURCES="$SOURCES $srcdir/src/thread/windows/SDL_systhread.c"
SOURCES="$SOURCES $srcdir/src/thread/windows/*.c"
SOURCES="$SOURCES $srcdir/src/thread/generic/SDL_syscond.c"
have_threads=yes
fi
Expand Down
8 changes: 5 additions & 3 deletions configure.in
Expand Up @@ -657,6 +657,7 @@ AC_HELP_STRING([--enable-oss], [support the OSS audio API [[default=maybe]]]),
case "$host" in
*-*-openbsd*)
enable_oss=no;;
esac
fi

if test x$enable_audio = xyes -a x$enable_oss = xyes; then
Expand Down Expand Up @@ -2019,6 +2020,9 @@ AC_HELP_STRING([--enable-pthread-sem], [use pthread semaphores [[default=yes]]])
# We can fake these with semaphores and mutexes if necessary
SOURCES="$SOURCES $srcdir/src/thread/pthread/SDL_syscond.c"

# Thread local storage
SOURCES="$SOURCES $srcdir/src/thread/pthread/SDL_systls.c"

have_threads=yes
fi
fi
Expand Down Expand Up @@ -2470,9 +2474,7 @@ AC_HELP_STRING([--enable-render-d3d], [enable the Direct3D render driver [[defau
# Set up files for the thread library
if test x$enable_threads = xyes; then
AC_DEFINE(SDL_THREAD_WINDOWS, 1, [ ])
SOURCES="$SOURCES $srcdir/src/thread/windows/SDL_sysmutex.c"
SOURCES="$SOURCES $srcdir/src/thread/windows/SDL_syssem.c"
SOURCES="$SOURCES $srcdir/src/thread/windows/SDL_systhread.c"
SOURCES="$SOURCES $srcdir/src/thread/windows/*.c"
SOURCES="$SOURCES $srcdir/src/thread/generic/SDL_syscond.c"
have_threads=yes
fi
Expand Down
61 changes: 61 additions & 0 deletions include/SDL_thread.h
Expand Up @@ -32,6 +32,7 @@
#include "SDL_error.h"

/* Thread synchronization primitives */
#include "SDL_atomic.h"
#include "SDL_mutex.h"

#include "begin_code.h"
Expand All @@ -47,6 +48,9 @@ typedef struct SDL_Thread SDL_Thread;
/* The SDL thread ID */
typedef unsigned long SDL_threadID;

/* Thread local storage ID */
typedef int SDL_TLSID;

/* The SDL thread priority
*
* Note: On many systems you require special privileges to set high priority.
Expand Down Expand Up @@ -166,6 +170,63 @@ extern DECLSPEC int SDLCALL SDL_SetThreadPriority(SDL_ThreadPriority priority);
*/
extern DECLSPEC void SDLCALL SDL_WaitThread(SDL_Thread * thread, int *status);

/**
* \brief Create an identifier that is globally visible to all threads but refers to data that is thread-specific.
*
* \return The newly created thread local storage identifier, or 0 on error
*
* \code
* static SDL_SpinLock tls_lock;
* static SDL_TLSID thread_local_storage;
*
* void SetMyThreadData(void *value)
* {
* if (!thread_local_storage) {
* SDL_AtomicLock(&tls_lock);
* if (!thread_local_storage) {
* thread_local_storage = SDL_TLSCreate();
* }
* SDL_AtomicUnLock(&tls_lock);
* }
* SDL_TLSSet(thread_local_storage, value);
* }
*
* void *GetMyThreadData(void)
* {
* return SDL_TLSGet(thread_local_storage);
* }
* \endcode
*
* \sa SDL_TLSGet()
* \sa SDL_TLSSet()
*/
extern DECLSPEC SDL_TLSID SDLCALL SDL_TLSCreate();

/**
* \brief Get the value associated with a thread local storage ID for the current thread.
*
* \param id The thread local storage ID
*
* \return The value associated with the ID for the current thread, or NULL if no value has been set.
*
* \sa SDL_TLSCreate()
* \sa SDL_TLSSet()
*/
extern DECLSPEC void * SDLCALL SDL_TLSGet(SDL_TLSID id);

/**
* \brief Set the value associated with a thread local storage ID for the current thread.
*
* \param id The thread local storage ID
* \param value The value to associate with the ID for the current thread
*
* \return 0 on success, -1 on error
*
* \sa SDL_TLSCreate()
* \sa SDL_TLSGet()
*/
extern DECLSPEC int SDLCALL SDL_TLSSet(SDL_TLSID id, const void *value);


/* Ends C function definitions when using C++ */
#ifdef __cplusplus
Expand Down

0 comments on commit 9b37546

Please sign in to comment.