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.
This commit is contained in:
Vadim Zeitlin 2021-11-26 19:03:46 +01:00
commit 38b76bb7c6

View file

@ -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<char*>(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;