aboutsummaryrefslogtreecommitdiffstats
path: root/alc/context.h
diff options
context:
space:
mode:
authorChris Robinson <[email protected]>2021-12-20 10:27:39 -0800
committerChris Robinson <[email protected]>2021-12-20 10:27:39 -0800
commit633c332deee0500b85927906be1084606a286ac9 (patch)
tree31d178782d6ed65163c4f9f945ccace31a1c0ad1 /alc/context.h
parenta88803f21e1b44633de1aa5e068df8e9f371c2c2 (diff)
Work around a MinGW thread_local bug
MinGW-w64 generates bad code when accessing extern thread_local objects. Wrapper functions are used to ensure it only accesses them from the same place they're defined. This unfortunately adds a bit of overhead for what should be a relatively simple thing. These functions are inlined for non-MinGW targets, avoiding the overhead on non-affected targets.
Diffstat (limited to 'alc/context.h')
-rw-r--r--alc/context.h15
1 files changed, 15 insertions, 0 deletions
diff --git a/alc/context.h b/alc/context.h
index d0afbdd9..87754235 100644
--- a/alc/context.h
+++ b/alc/context.h
@@ -131,8 +131,10 @@ struct ALCcontext : public al::intrusive_ref<ALCcontext>, ContextBase {
/* Process-wide current context */
static std::atomic<ALCcontext*> sGlobalContext;
+private:
/* Thread-local current context. */
static thread_local ALCcontext *sLocalContext;
+
/* Thread-local context handling. This handles attempting to release the
* context which may have been left current when the thread is destroyed.
*/
@@ -143,6 +145,19 @@ struct ALCcontext : public al::intrusive_ref<ALCcontext>, ContextBase {
};
static thread_local ThreadCtx sThreadContext;
+public:
+ /* HACK: MinGW generates bad code when accessing an extern thread_local
+ * object. Add a wrapper function for it that only accesses it where it's
+ * defined.
+ */
+#ifdef __MINGW32__
+ static ALCcontext *getThreadContext() noexcept;
+ static void setThreadContext(ALCcontext *context) noexcept;
+#else
+ static ALCcontext *getThreadContext() noexcept { return sLocalContext; }
+ static void setThreadContext(ALCcontext *context) noexcept { sThreadContext.set(context); }
+#endif
+
/* Default effect that applies to sources that don't have an effect on send 0. */
static ALeffect sDefaultEffect;