Skip to content

Commit

Permalink
JStringManager: thread safe
Browse files Browse the repository at this point in the history
  • Loading branch information
jafl committed May 22, 2024
1 parent a659a81 commit 0bc453c
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 5 deletions.
10 changes: 8 additions & 2 deletions libjcore/code/JStringManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -110,11 +110,11 @@ JStringManager::Get
const
{
const JString* s;
if (GetItem(JString(id, JString::kNoCopy), &s))
if (GetItem(id, &s))
{
assert( s != nullptr );
}
else if (Contains(theMissingStringKey)) // ok to leak memory, because it should never happen
else if (Contains(theMissingStringKey)) // ok to leak memory, because it should never happen in release
{
std::cerr << "missing string id: " << id << std::endl;

Expand Down Expand Up @@ -206,8 +206,10 @@ JSubstitute*
JStringManager::GetReplaceEngine()
const
{
itsReplaceMutex.lock();
itsReplaceEngine->Reset();
itsReplaceEngine->IgnoreUnrecognized(true);
itsReplaceMutex.unlock();
return itsReplaceEngine;
}

Expand All @@ -228,6 +230,8 @@ JStringManager::Replace
)
const
{
itsReplaceMutex.lock();

JSubstitute* r = GetReplaceEngine();

const JSize count = size/(2*sizeof(JUtf8Byte*));
Expand All @@ -237,6 +241,8 @@ JStringManager::Replace
}

r->Substitute(str);

itsReplaceMutex.unlock();
}

/******************************************************************************
Expand Down
7 changes: 4 additions & 3 deletions libjcore/code/JStringManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,10 @@ class JStringManager : public JStringPtrMap<JString>

private:

JString itsBCP47Locale;
JSubstitute* itsReplaceEngine;
static bool thePseudotranslationFlag;
JString itsBCP47Locale;
JSubstitute* itsReplaceEngine;
mutable std::recursive_mutex itsReplaceMutex;
static bool thePseudotranslationFlag;

private:

Expand Down

0 comments on commit 0bc453c

Please sign in to comment.