Skip to content
Open
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
9 changes: 7 additions & 2 deletions runtime/Cpp/runtime/src/Recognizer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@ using namespace antlr4::internal;
std::map<const dfa::Vocabulary*, std::map<std::string_view, size_t>> Recognizer::_tokenTypeMapCache;
std::map<std::vector<std::string>, std::map<std::string, size_t>> 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);
Expand All @@ -41,7 +46,7 @@ Recognizer::~Recognizer() {
std::map<std::string_view, size_t> Recognizer::getTokenTypeMap() {
const dfa::Vocabulary& vocabulary = getVocabulary();

UniqueLock<Mutex> lck(_mutex);
UniqueLock<Mutex> lck(cacheMutex);
std::map<std::string_view, size_t> result;
auto iterator = _tokenTypeMapCache.find(&vocabulary);
if (iterator != _tokenTypeMapCache.end()) {
Expand Down Expand Up @@ -71,7 +76,7 @@ std::map<std::string, size_t> Recognizer::getRuleIndexMap() {
throw "The current recognizer does not provide a list of rule names.";
}

UniqueLock<Mutex> lck(_mutex);
UniqueLock<Mutex> lck(cacheMutex);
std::map<std::string, size_t> result;
auto iterator = _ruleIndexMapCache.find(ruleNames);
if (iterator != _ruleIndexMapCache.end()) {
Expand Down
81 changes: 81 additions & 0 deletions runtime/Cpp/runtime/tests/RecognizerTest.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
#include <atomic>
#include <cstddef>
#include <map>
#include <memory>
#include <string>
#include <string_view>
#include <thread>
#include <vector>

#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<std::string>& 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<CommonToken>* getTokenFactory() override { return nullptr; }

private:
std::vector<std::string> _ruleNames;
dfa::Vocabulary _vocabulary;
atn::ATN _atn;
};

TEST(RecognizerTest, CachesAreSharedAcrossThreads) {
constexpr size_t kThreads = 8;
constexpr size_t kPerThread = 2000;

std::vector<std::unique_ptr<TestRecognizer>> recognizers;
for (size_t i = 0; i < kThreads * kPerThread; ++i) {
recognizers.push_back(std::make_unique<TestRecognizer>(i));
}

std::atomic<bool> start{false};
std::vector<std::thread> 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<std::string_view, size_t> tokenTypes = recognizers[i]->getTokenTypeMap();
EXPECT_EQ(tokenTypes.at("A"), 1u);
EXPECT_EQ(tokenTypes.at("'a'"), 1u);
std::map<std::string, size_t> ruleIndexes = recognizers[i]->getRuleIndexMap();
EXPECT_EQ(ruleIndexes.at("rule" + std::to_string(i)), 0u);
}
}

} // namespace
} // namespace antlr4
Loading