Skip to content

Commit

Permalink
Work around a thread_local bug in MinGW with the WIN32 threading model
Browse files Browse the repository at this point in the history
  • Loading branch information
mniip committed May 12, 2018
1 parent 1963713 commit 99f7258
Showing 1 changed file with 21 additions and 3 deletions.
24 changes: 21 additions & 3 deletions src/common/String.cpp
Expand Up @@ -296,15 +296,19 @@ String numberString(numberChars);

static thread_local struct LocaleImpl
{
std::basic_stringstream<char> stream;
std::basic_stringstream<wchar_t> wstream;
std::basic_stringstream<char> &stream;
std::basic_stringstream<wchar_t> &wstream;

LocaleImpl()
LocaleImpl():
stream(*new std::basic_stringstream<char>()),
wstream(*new std::basic_stringstream<wchar_t>())
{
stream.imbue(std::locale::classic());
wstream.imbue(std::locale::classic());
}

~LocaleImpl();

inline void PrepareStream(ByteStringBuilder &b)
{
stream.flags(b.flags);
Expand Down Expand Up @@ -373,6 +377,20 @@ static thread_local struct LocaleImpl
}
LocaleImpl;

// This specific implementation of destruction seems to help against the
// currently broken WIN32 threading model on MinGW. The real destructor is
// called by the runtime as cdecl but the compiled code expects thiscall.
static destroyLocaleImpl()
{
delete &LocaleImpl.stream;
delete &LocaleImpl.wstream;
}

LocaleImpl::~LocaleImpl()
{
destroyLocaleImpl();
}

ByteString ByteStringBuilder::Build() const
{
return ByteString(buffer.begin(), buffer.end());
Expand Down

0 comments on commit 99f7258

Please sign in to comment.