Skip to content

Commit

Permalink
fix: add lock for ChineseLetterHelper
Browse files Browse the repository at this point in the history
add lock for ChineseLetterHelper

Log:
  • Loading branch information
Clauszy committed Feb 5, 2024
1 parent 8b044dc commit e0d0205
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 0 deletions.
9 changes: 9 additions & 0 deletions src/libgrand-search-daemon/utils/chineseletterhelper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ ChineseLetterHelper::ChineseLetterHelper()

bool ChineseLetterHelper::chinese2Pinyin(const QString &words, QString &result)
{
QReadLocker lk(&m_lock);

int ok = false;
for (int i = 0; i < words.length(); ++i) {
const uint key = words.at(i).unicode();
Expand All @@ -42,6 +44,13 @@ bool ChineseLetterHelper::chinese2Pinyin(const QString &words, QString &result)

void ChineseLetterHelper::initDict()
{
// fast check
if (m_inited)
return;

QWriteLocker lk(&m_lock);

// check again, m_inited may be setted to true in other thread.
if (m_inited)
return;

Expand Down
2 changes: 2 additions & 0 deletions src/libgrand-search-daemon/utils/chineseletterhelper.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

#include <QHash>
#include <QObject>
#include <QReadWriteLock>

#define Ch2PyIns GrandSearch::ChineseLetterHelper::instance()

Expand All @@ -24,6 +25,7 @@ class ChineseLetterHelper
void initDict();
volatile bool m_inited = false;
QHash<uint, QString> m_dict;
QReadWriteLock m_lock;
};

}
Expand Down

0 comments on commit e0d0205

Please sign in to comment.