diff --git a/runtime/Cpp/runtime/src/Recognizer.cpp b/runtime/Cpp/runtime/src/Recognizer.cpp index a1a328c174..fb74e298a1 100644 --- a/runtime/Cpp/runtime/src/Recognizer.cpp +++ b/runtime/Cpp/runtime/src/Recognizer.cpp @@ -30,6 +30,11 @@ using namespace antlr4::internal; std::map> Recognizer::_tokenTypeMapCache; std::map, std::map> Recognizer::_ruleIndexMapCache; +namespace { + // The caches above are static, so they need a lock shared by all recognizers. + Mutex cacheMutex; +} + Recognizer::Recognizer() { InitializeInstanceFields(); _proxListener.addErrorListener(&ConsoleErrorListener::INSTANCE); @@ -41,7 +46,7 @@ Recognizer::~Recognizer() { std::map Recognizer::getTokenTypeMap() { const dfa::Vocabulary& vocabulary = getVocabulary(); - UniqueLock lck(_mutex); + UniqueLock lck(cacheMutex); std::map result; auto iterator = _tokenTypeMapCache.find(&vocabulary); if (iterator != _tokenTypeMapCache.end()) { @@ -71,7 +76,7 @@ std::map Recognizer::getRuleIndexMap() { throw "The current recognizer does not provide a list of rule names."; } - UniqueLock lck(_mutex); + UniqueLock lck(cacheMutex); std::map result; auto iterator = _ruleIndexMapCache.find(ruleNames); if (iterator != _ruleIndexMapCache.end()) { diff --git a/runtime/Cpp/runtime/tests/RecognizerTest.cpp b/runtime/Cpp/runtime/tests/RecognizerTest.cpp new file mode 100644 index 0000000000..b78b0e85e4 --- /dev/null +++ b/runtime/Cpp/runtime/tests/RecognizerTest.cpp @@ -0,0 +1,81 @@ +#include +#include +#include +#include +#include +#include +#include +#include + +#include "gtest/gtest.h" +#include "IntStream.h" +#include "Recognizer.h" +#include "Vocabulary.h" +#include "atn/ATN.h" +#include "atn/ATNType.h" + +namespace antlr4 { +namespace { + + // Each instance has its own vocabulary and rule names, so every call misses + // the shared caches in Recognizer and inserts a new entry. + class TestRecognizer final : public Recognizer { + public: + explicit TestRecognizer(size_t id) + : _ruleNames({"rule" + std::to_string(id)}), + _vocabulary({"", "'a'"}, {"", "A"}), + _atn(atn::ATNType::PARSER, 1) {} + + const std::vector& getRuleNames() const override { return _ruleNames; } + const dfa::Vocabulary& getVocabulary() const override { return _vocabulary; } + std::string getGrammarFileName() const override { return "Test.g4"; } + const atn::ATN& getATN() const override { return _atn; } + IntStream* getInputStream() override { return nullptr; } + void setInputStream(IntStream*) override {} + TokenFactory* getTokenFactory() override { return nullptr; } + + private: + std::vector _ruleNames; + dfa::Vocabulary _vocabulary; + atn::ATN _atn; + }; + + TEST(RecognizerTest, CachesAreSharedAcrossThreads) { + constexpr size_t kThreads = 8; + constexpr size_t kPerThread = 2000; + + std::vector> recognizers; + for (size_t i = 0; i < kThreads * kPerThread; ++i) { + recognizers.push_back(std::make_unique(i)); + } + + std::atomic start{false}; + std::vector threads; + for (size_t t = 0; t < kThreads; ++t) { + threads.emplace_back([&recognizers, &start, t] { + while (!start.load()) { + std::this_thread::yield(); + } + for (size_t i = 0; i < kPerThread; ++i) { + TestRecognizer& recognizer = *recognizers[t * kPerThread + i]; + recognizer.getTokenTypeMap(); + recognizer.getRuleIndexMap(); + } + }); + } + start.store(true); + for (auto& thread : threads) { + thread.join(); + } + + for (size_t i = 0; i < recognizers.size(); ++i) { + std::map tokenTypes = recognizers[i]->getTokenTypeMap(); + EXPECT_EQ(tokenTypes.at("A"), 1u); + EXPECT_EQ(tokenTypes.at("'a'"), 1u); + std::map ruleIndexes = recognizers[i]->getRuleIndexMap(); + EXPECT_EQ(ruleIndexes.at("rule" + std::to_string(i)), 0u); + } + } + +} // namespace +} // namespace antlr4