From beaffdda716e2063d1112cb09956d44d948f40b5 Mon Sep 17 00:00:00 2001 From: Chris Robinson Date: Fri, 30 Dec 2022 20:56:37 -0800 Subject: Use a simple spinlock to protect the current global context This will be much for efficient than a recursive mutex, given the amount of contention will be very low. --- alc/context.cpp | 8 ++++++++ 1 file changed, 8 insertions(+) (limited to 'alc/context.cpp') diff --git a/alc/context.cpp b/alc/context.cpp index 906a160e..f9aec221 100644 --- a/alc/context.cpp +++ b/alc/context.cpp @@ -84,6 +84,7 @@ constexpr ALchar alExtList[] = } // namespace +std::atomic ALCcontext::sGlobalContextLock{false}; std::atomic ALCcontext::sGlobalContext{nullptr}; thread_local ALCcontext *ALCcontext::sLocalContext{nullptr}; @@ -203,7 +204,14 @@ bool ALCcontext::deinit() ALCcontext *origctx{this}; if(sGlobalContext.compare_exchange_strong(origctx, nullptr)) + { + while(sGlobalContextLock.load()) { + /* Wait to make sure another thread didn't get the context and is + * trying to increment its refcount. + */ + } dec_ref(); + } bool ret{}; /* First make sure this context exists in the device's list. */ -- cgit v1.2.3