From 38b76bb7c63f397c5164cf4619da88706e73ff43 Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Fri, 26 Nov 2021 19:03:46 +0100 Subject: [PATCH] Make global SWIG_CException::PendingException thread-local This allows different threads to use the same library concurrently without corrupting each other's exceptions. Note that this requires C++11 support for wrapper compilation too. If this really turns out to be a problem, we could always just #define thread_local as compiler-specific equivalent such as "__thread" for gcc or "__declspec(thread)" for MSVC, which are available since forever. --- Lib/c/cexcept.swg | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Lib/c/cexcept.swg b/Lib/c/cexcept.swg index 7fb6252c4..3a317d65e 100644 --- a/Lib/c/cexcept.swg +++ b/Lib/c/cexcept.swg @@ -40,7 +40,7 @@ public: private: friend void SWIG_CException_Raise(int code, const char* msg); - static SWIG_CException* PendingException; + static thread_local SWIG_CException* PendingException; SWIG_CException(int code, const char* msg) : code(code), msg(strdup(msg)) { } ~SWIG_CException() { free(const_cast(msg)); } @@ -53,7 +53,7 @@ private: // This part is implementation only and doesn't need to be seen by SWIG. %{ #ifndef SWIG_CException_DEFINED -SWIG_CException *SWIG_CException::PendingException = 0; +thread_local SWIG_CException *SWIG_CException::PendingException = 0; SWIGEXPORTC void SWIG_CException_Raise(int code, const char* msg) { delete SWIG_CException::PendingException;