@@ -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-
5244void 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
6363void 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
206206bool 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
0 commit comments