Skip to content

Fix race conditions when initializing static variables. (Fix for issue #181) #183

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 9, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions include/lucene++/Synchronize.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@
#ifndef SYNCHRONIZE_H
#define SYNCHRONIZE_H

#include <atomic>
#include <boost/thread/recursive_mutex.hpp>
#include <boost/thread/mutex.hpp>
#include "Lucene.h"

namespace Lucene {
Expand Down Expand Up @@ -60,6 +62,21 @@ class LPPAPI SyncLock {
void lock(int32_t timeout);
};


#define LUCENE_RUN_ONCE(Command) \
do { \
static std::atomic<bool> RUN_ONCE_hasRun = {}; \
if (!RUN_ONCE_hasRun) { \
static boost::mutex RUN_ONCE_mutex; \
boost::mutex::scoped_lock RUN_ONCE_lock(RUN_ONCE_mutex); \
if (!RUN_ONCE_hasRun) { \
Command; \
RUN_ONCE_hasRun = true; \
} \
} \
} while(0)


}

#endif
4 changes: 2 additions & 2 deletions src/contrib/analyzers/common/analysis/ar/ArabicAnalyzer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -89,11 +89,11 @@ ArabicAnalyzer::~ArabicAnalyzer() {

const HashSet<String> ArabicAnalyzer::getDefaultStopSet() {
static HashSet<String> stopSet;
if (!stopSet) {
LUCENE_RUN_ONCE(
String stopWords(UTF8_TO_STRING(DEFAULT_STOPWORD_FILE));
Collection<String> words(StringUtils::split(stopWords, L"\n"));
stopSet = HashSet<String>::newInstance(words.begin(), words.end());
}
);
return stopSet;
}

Expand Down
8 changes: 4 additions & 4 deletions src/contrib/analyzers/common/analysis/ar/ArabicStemmer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ ArabicStemmer::~ArabicStemmer() {

const Collection<String> ArabicStemmer::prefixes() {
static Collection<String> _prefixes;
if (!_prefixes) {
LUCENE_RUN_ONCE(
_prefixes = Collection<String>::newInstance();
_prefixes.add(String(L"") + ALEF + LAM);
_prefixes.add(String(L"") + WAW + ALEF + LAM);
Expand All @@ -36,13 +36,13 @@ const Collection<String> ArabicStemmer::prefixes() {
_prefixes.add(String(L"") + FEH + ALEF + LAM);
_prefixes.add(String(L"") + LAM + LAM);
_prefixes.add(String(L"") + WAW);
}
);
return _prefixes;
}

const Collection<String> ArabicStemmer::suffixes() {
static Collection<String> _suffixes;
if (!_suffixes) {
LUCENE_RUN_ONCE(
_suffixes = Collection<String>::newInstance();
_suffixes.add(String(L"") + HEH + ALEF);
_suffixes.add(String(L"") + ALEF + NOON);
Expand All @@ -54,7 +54,7 @@ const Collection<String> ArabicStemmer::suffixes() {
_suffixes.add(String(L"") + HEH);
_suffixes.add(String(L"") + TEH_MARBUTA);
_suffixes.add(String(L"") + YEH);
}
);
return _suffixes;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,9 @@ BrazilianAnalyzer::~BrazilianAnalyzer() {

const HashSet<String> BrazilianAnalyzer::getDefaultStopSet() {
static HashSet<String> stopSet;
if (!stopSet) {
LUCENE_RUN_ONCE(
stopSet = HashSet<String>::newInstance(_BRAZILIAN_STOP_WORDS, _BRAZILIAN_STOP_WORDS + SIZEOF_ARRAY(_BRAZILIAN_STOP_WORDS));
}
);
return stopSet;
}

Expand Down
4 changes: 2 additions & 2 deletions src/contrib/analyzers/common/analysis/cjk/CJKAnalyzer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@ CJKAnalyzer::~CJKAnalyzer() {

const HashSet<String> CJKAnalyzer::getDefaultStopSet() {
static HashSet<String> stopSet;
if (!stopSet) {
LUCENE_RUN_ONCE(
stopSet = HashSet<String>::newInstance(_STOP_WORDS, _STOP_WORDS + SIZEOF_ARRAY(_STOP_WORDS));
}
);
return stopSet;
}

Expand Down
4 changes: 2 additions & 2 deletions src/contrib/analyzers/common/analysis/cz/CzechAnalyzer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,11 +86,11 @@ CzechAnalyzer::~CzechAnalyzer() {

const HashSet<String> CzechAnalyzer::getDefaultStopSet() {
static HashSet<String> stopSet;
if (!stopSet) {
LUCENE_RUN_ONCE(
String stopWords(UTF8_TO_STRING(_CZECH_STOP_WORDS));
Collection<String> words(StringUtils::split(stopWords, L"\n"));
stopSet = HashSet<String>::newInstance(words.begin(), words.end());
}
);
return stopSet;
}

Expand Down
4 changes: 2 additions & 2 deletions src/contrib/analyzers/common/analysis/de/GermanAnalyzer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,9 @@ GermanAnalyzer::~GermanAnalyzer() {

const HashSet<String> GermanAnalyzer::getDefaultStopSet() {
static HashSet<String> stopSet;
if (!stopSet) {
LUCENE_RUN_ONCE(
stopSet = HashSet<String>::newInstance(_GERMAN_STOP_WORDS, _GERMAN_STOP_WORDS + SIZEOF_ARRAY(_GERMAN_STOP_WORDS));
}
);
return stopSet;
}

Expand Down
4 changes: 2 additions & 2 deletions src/contrib/analyzers/common/analysis/el/GreekAnalyzer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,11 @@ GreekAnalyzer::~GreekAnalyzer() {

const HashSet<String> GreekAnalyzer::getDefaultStopSet() {
static HashSet<String> stopSet;
if (!stopSet) {
LUCENE_RUN_ONCE(
String stopWords(UTF8_TO_STRING(_GREEK_STOP_WORDS));
Collection<String> words(StringUtils::split(stopWords, L"\n"));
stopSet = HashSet<String>::newInstance(words.begin(), words.end());
}
);
return stopSet;
}

Expand Down
4 changes: 2 additions & 2 deletions src/contrib/analyzers/common/analysis/fa/PersianAnalyzer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -205,11 +205,11 @@ PersianAnalyzer::~PersianAnalyzer() {

const HashSet<String> PersianAnalyzer::getDefaultStopSet() {
static HashSet<String> stopSet;
if (!stopSet) {
LUCENE_RUN_ONCE(
String stopWords(UTF8_TO_STRING(DEFAULT_STOPWORD_FILE));
Collection<String> words(StringUtils::split(stopWords, L"\n"));
stopSet = HashSet<String>::newInstance(words.begin(), words.end());
}
);
return stopSet;
}

Expand Down
4 changes: 2 additions & 2 deletions src/contrib/analyzers/common/analysis/fr/FrenchAnalyzer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,9 @@ FrenchAnalyzer::~FrenchAnalyzer() {

const HashSet<String> FrenchAnalyzer::getDefaultStopSet() {
static HashSet<String> stoptable;
if (!stoptable) {
LUCENE_RUN_ONCE(
stoptable = HashSet<String>::newInstance(_FRENCH_STOP_WORDS, _FRENCH_STOP_WORDS + SIZEOF_ARRAY(_FRENCH_STOP_WORDS));
}
);
return stoptable;
}

Expand Down
28 changes: 15 additions & 13 deletions src/contrib/analyzers/common/analysis/fr/FrenchStemmer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -124,40 +124,42 @@ void FrenchStemmer::step1() {

bool FrenchStemmer::step2a() {
static Collection<String> search;
if (!search) {
static const wchar_t* _search[] = {
static const wchar_t* _search[] = {
L"\x00eemes", L"\x00eetes", L"iraIent", L"irait", L"irais", L"irai", L"iras", L"ira",
L"irent", L"iriez", L"irez", L"irions", L"irons", L"iront", L"issaIent",
L"issais", L"issantes", L"issante", L"issants", L"issant", L"issait",
L"issais", L"issions", L"issons", L"issiez", L"issez", L"issent", L"isses",
L"isse", L"ir", L"is", L"\x00eet", L"it", L"ies", L"ie", L"i"
};
};

LUCENE_RUN_ONCE(
search = Collection<String>::newInstance(_search, _search + SIZEOF_ARRAY(_search));
}
);

return deleteFromIfTestVowelBeforeIn(RV, search, false, RV);
}

void FrenchStemmer::step2b() {
static Collection<String> suffix;
if (!suffix) {
static const wchar_t* _suffix[] = {
static const wchar_t* _suffix[] = {
L"eraIent", L"erais", L"erait", L"erai", L"eras", L"erions", L"eriez",
L"erons", L"eront", L"erez", L"\x00e8rent", L"era", L"\x00e9es", L"iez", L"\x00e9e", L"\x00e9s",
L"er", L"ez", L"\x00e9"
};
};
LUCENE_RUN_ONCE(
suffix = Collection<String>::newInstance(_suffix, _suffix + SIZEOF_ARRAY(_suffix));
}
);
deleteFrom(RV, suffix);

static Collection<String> search;
if (!search) {
static const wchar_t* _search[] = {
static const wchar_t* _search[] = {
L"assions", L"assiez", L"assent", L"asses", L"asse", L"aIent", L"antes",
L"aIent", L"Aient", L"ante", L"\x00e2mes", L"\x00e2tes", L"ants", L"ant", L"ait",
L"a\x00eet", L"ais", L"Ait", L"A\x00eet", L"Ais", L"\x00e2t", L"as", L"ai", L"Ai", L"a"
};
search = Collection<String>::newInstance(_search, _search + SIZEOF_ARRAY(_search));
}
};
LUCENE_RUN_ONCE(
search = Collection<String>::newInstance(_search, _search + SIZEOF_ARRAY(_search));
);
deleteButSuffixFrom(RV, search, L"e", true);

deleteFrom(R2, newCollection<String>(L"ions"));
Expand Down
4 changes: 2 additions & 2 deletions src/contrib/analyzers/common/analysis/nl/DutchAnalyzer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,9 @@ void DutchAnalyzer::initialize() {

const HashSet<String> DutchAnalyzer::getDefaultStopSet() {
static HashSet<String> stoptable;
if (!stoptable) {
LUCENE_RUN_ONCE(
stoptable = HashSet<String>::newInstance(_DUTCH_STOP_WORDS, _DUTCH_STOP_WORDS + SIZEOF_ARRAY(_DUTCH_STOP_WORDS));
}
);
return stoptable;
}

Expand Down
4 changes: 2 additions & 2 deletions src/contrib/analyzers/common/analysis/ru/RussianAnalyzer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,11 +77,11 @@ RussianAnalyzer::~RussianAnalyzer() {

const HashSet<String> RussianAnalyzer::getDefaultStopSet() {
static HashSet<String> stopSet;
if (!stopSet) {
LUCENE_RUN_ONCE(
String stopWords(UTF8_TO_STRING(DEFAULT_STOPWORD_FILE));
Collection<String> words(StringUtils::split(stopWords, L"\n"));
stopSet = HashSet<String>::newInstance(words.begin(), words.end());
}
);
return stopSet;
}

Expand Down
Loading