Skip to content

Commit 4e0bf47

Browse files
committed
Compile with C++23 standard, use std::atomic<std::shared_ptr<T>> in bpp-lsp ProgramPool snapshots
1 parent 8815ba9 commit 4e0bf47

File tree

3 files changed

+18
-20
lines changed

3 files changed

+18
-20
lines changed

mk/config.mk

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ ANTLR4_RUNTIME_LIBRARY = $(shell (find /usr -name libantlr4-runtime.a || find /u
55

66
# C++ CONFIG
77
CXX = g++
8-
CXXFLAGS = -std=gnu++17 -O2 -s -Wall -MMD -MP
8+
CXXFLAGS = -std=gnu++23 -O2 -s -Wall -MMD -MP
99
INCLUDEFLAGS = -isystem $(ANTLR4_HEADERS)
1010

1111
# Can we parse debian/changelog?

src/lsp/ProgramPool.cpp

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -41,14 +41,6 @@ void ProgramPool::_remove_oldest_program() {
4141
_remove_program(0);
4242
}
4343

44-
const ProgramPool::Snapshot& ProgramPool::load_snapshot() const {
45-
return *std::atomic_load_explicit(&_snapshot, std::memory_order_acquire);
46-
}
47-
48-
void ProgramPool::_set_snapshot(std::shared_ptr<const Snapshot> new_snapshot) {
49-
std::atomic_store_explicit(&_snapshot, new_snapshot, std::memory_order_release);
50-
}
51-
5244
void ProgramPool::update_snapshot() {
5345
auto new_snapshot = std::make_shared<Snapshot>();
5446
{
@@ -57,7 +49,15 @@ void ProgramPool::update_snapshot() {
5749
new_snapshot->program_indices_snapshot = program_indices;
5850
new_snapshot->open_files_snapshot = open_files;
5951
}
60-
_set_snapshot(new_snapshot); // Update the snapshot atomically
52+
snapshot.store(new_snapshot, std::memory_order_release);
53+
}
54+
55+
ProgramPool::Snapshot ProgramPool::load_snapshot() const {
56+
auto current_snapshot = snapshot.load(std::memory_order_acquire);
57+
if (current_snapshot == nullptr) {
58+
return ProgramPool::Snapshot{}; // return a default-constructed copy
59+
}
60+
return *current_snapshot; // Return a **copy** of the pointed-to Snapshot
6161
}
6262

6363
void ProgramPool::_remove_program(size_t index) {
@@ -162,10 +162,10 @@ std::shared_ptr<bpp::bpp_program> ProgramPool::get_program(const std::string& fi
162162
// Skip getting a lock on the pool's main state, since we're not modifying it
163163
// Instead, read from the snapshot
164164
// And REFUSE to modify the pool if the file isn't already in it
165-
Snapshot snapshot = load_snapshot();
166-
auto it = snapshot.program_indices_snapshot.find(file_path);
167-
if (it != snapshot.program_indices_snapshot.end()) {
168-
return snapshot.programs_snapshot[it->second];
165+
Snapshot snapshot_copy = load_snapshot();
166+
auto it = snapshot_copy.program_indices_snapshot.find(file_path);
167+
if (it != snapshot_copy.program_indices_snapshot.end()) {
168+
return snapshot_copy.programs_snapshot[it->second];
169169
} else {
170170
return nullptr;
171171
}
@@ -205,8 +205,8 @@ std::shared_ptr<bpp::bpp_program> ProgramPool::get_program(const std::string& fi
205205

206206
bool ProgramPool::has_program(const std::string& file_path) {
207207
// Scan the SNAPSHOT for the program
208-
Snapshot snapshot = load_snapshot();
209-
const auto& program_indices_snapshot = snapshot.program_indices_snapshot;
208+
Snapshot snapshot_copy = load_snapshot();
209+
const auto& program_indices_snapshot = snapshot_copy.program_indices_snapshot;
210210
return program_indices_snapshot.find(file_path) != program_indices_snapshot.end();
211211
}
212212

src/lsp/ProgramPool.h

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,7 @@ class ProgramPool {
4747
std::unordered_map<std::string, size_t> program_indices_snapshot; // Snapshot of the current program indices
4848
std::unordered_map<std::string, bool> open_files_snapshot; // Snapshot of the current open files
4949
};
50-
std::shared_ptr<const Snapshot> _snapshot = std::make_shared<Snapshot>(); // DO NOT access directly
51-
// Use the following member functions instead:
52-
const Snapshot& load_snapshot() const;
53-
void _set_snapshot(std::shared_ptr<const Snapshot> new_snapshot);
50+
std::atomic<std::shared_ptr<Snapshot>> snapshot; // Atomic snapshot for thread-safe access
5451

5552
bool utf16_mode = false; // Whether to use UTF-16 mode for character counting
5653

@@ -65,6 +62,7 @@ class ProgramPool {
6562
bool suppress_warnings = false;
6663

6764
void update_snapshot();
65+
Snapshot load_snapshot() const;
6866
public:
6967
explicit ProgramPool(size_t max_programs = 10);
7068

0 commit comments

Comments
 (0)